How to draw a diagonal line dynamically

The requirement is to draw a diagonal line whose co-ordinates move along with the ‘cell’. As the cell grows or moves, the line must follow accordingly.

Explanation: The idea is to record the results to add a diagonal line to in an array and then at the end of the band (eg. Section footer) when everything is positioned correctly, add the diagonal line over the top. This ensures the line is always added last and the size follows the cell size.

Below are the steps:

1/ Put this at Function definitions :

// this is where items that need diagonals added are remembered
var diagonals = [];

// add an item to the diagonals array
function addDiagonal(item) {
  diagonals.push(item);
}

// build all the diagonals (for this parent - usually a band) and reset the diagonals to empty
function buildDiagonals(parent) {
  for (var i=0;i<diagonals.length;i++) {
    var line = buildDiagonal(diagonals[i]);
    parent.addLogicalElement(line);
  }
  diagonals = [];
}

// build one diagonal line with the same dimensions as the item it will be shown on top of
// note that you can change line colour, thickness etc. here
function buildDiagonal(item) {
  var line = new Packages.com.elixirtech.report2.logical.model.Line();
  line.setTop(item.getTop());
  line.setLeft(item.getLeft());
  line.setWidth(item.getWidth());
  line.setHeight(item.getHeight());
  line.setLineWidth(20);
  line.setLineColor("Black");
  line.setLineStyle("Solid");
  line.setDivisor(1);
  line.setX1(1);
  line.setY1(0);
  line.setX2(0);
  line.setY2(1);
  return line;
}

2/ Then the field we want to put the diagonal over has this onRenderEnd:

     addDiagonal(result);

3/ And the band that contains the fields with diagonals has this onRenderEnd :

    buildDiagonals(result);

The Sample.rml is available for download for your references.

Please change the following file format from .txt to .rml
Sample.txt (8.6 KB)