1

i have a lot of charts, each in a different JInternalFrame:

enter image description here

But the horizontal axis should be aligned to the same point(maybe the red line). The problem is, that the space for the label is set automatically by jFreechart.

So i tried to find a solution for multiline ticklabels. I found this:

  int optionsCount = state.getStatusOptions().toArray().length;
  String[] grade = new String[optionsCount + 1];
  grade[0] = "";
  for (int x = 1; x < optionsCount + 1; x++) {

         //grade[x] ="blaa"+x;//state.getStatusOptions().get(x - 1);
         //grade[x]="1.line\n2.line\n3.line";
           grade[x] = newLineString(state.getStatusOptions().get(x - 1), 5);
  }
  // grade[1]="1.line\n2.line";



  SymbolAxis rangeAxis;

  rangeAxis = new SymbolAxis("", grade){

                        @Override
                        protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
                             Rectangle2D l = super.getLabelEnclosure(g2, edge);
                            l.setRect(l.getX(),l.getY(),l.getWidth()*0.5,l.getHeight());
                            return l;
                        }



             @Override
             protected AxisState drawTickMarksAndLabels(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge) {
                 AxisState state = new AxisState(cursor);

                 if (isAxisLineVisible()) {
                     drawAxisLine(g2, cursor, dataArea, edge);
                 }

                 double ol = getTickMarkOutsideLength();
                 double il = getTickMarkInsideLength();

                 List ticks = refreshTicks(g2, state, dataArea, edge);
                 state.setTicks(ticks);
                 g2.setFont(getTickLabelFont());
                 Iterator iterator = ticks.iterator();

                 // remember the max number of lines used in any label
                 int maxLinesUsed = 0;

                 while (iterator.hasNext()) {
                     ValueTick tick = (ValueTick) iterator.next();
                     if (isTickLabelsVisible()) {
                         g2.setPaint(getTickLabelPaint());
                         float[] anchorPoint = calculateAnchorPoint(tick, cursor, dataArea, edge);


                         g2.draw(plotArea);
                         g2.setPaint(Color.green);
                         g2.draw(dataArea);
                         g2.setPaint(getTickLabelPaint());
                         // split by "\n" and draw text in a new line for each result
                         String tickText = tick.getText();
                         int line = 1;
                         for (String tickTextLine : tickText.split("\n")) {
                             float x = anchorPoint[0];
                             // one row down...
                             float y = anchorPoint[1] + line * g2.getFont().getSize();
                             TextUtilities.drawRotatedString(tickTextLine, g2, x, y, tick.getTextAnchor(), tick.getAngle(), tick
                                     .getRotationAnchor());
                             line++;
                         }
                         // if we used more lines than any time before remember it
                         if (line > maxLinesUsed) {
                             maxLinesUsed = line;
                         }
                     }

                     if (isTickMarksVisible() && tick.getTickType().equals(TickType.MAJOR)) {
                         float xx = (float) valueToJava2D(tick.getValue(), dataArea, edge);
                         Line2D mark = null;
                         g2.setStroke(getTickMarkStroke());
                         g2.setPaint(getTickMarkPaint());
                         if (edge == RectangleEdge.LEFT) {
                             mark = new Line2D.Double(cursor - ol, xx, cursor + il, xx);
                         } else if (edge == RectangleEdge.RIGHT) {
                             mark = new Line2D.Double(cursor + ol, xx, cursor - il, xx);
                         } else if (edge == RectangleEdge.TOP) {
                             mark = new Line2D.Double(xx, cursor - ol, xx, cursor + il);
                         } else if (edge == RectangleEdge.BOTTOM) {
                             mark = new Line2D.Double(xx, cursor + ol, xx, cursor - il);
                         }
                         g2.draw(mark);
                     }
                 }

                 // need to work out the space used by the tick labels...
                 // so we can update the cursor...
                 // patched using maxLinesUsed => we need more space because of multiple lines
                 double used = 0.0;
                 if (isTickLabelsVisible()) {
                     if (edge == RectangleEdge.LEFT) {
                         used += findMaximumTickLabelWidth(ticks, g2, plotArea, isVerticalTickLabels()) * maxLinesUsed;
                         state.cursorLeft(used);
                     } else if (edge == RectangleEdge.RIGHT) {
                         used = findMaximumTickLabelWidth(ticks, g2, plotArea, isVerticalTickLabels()) * maxLinesUsed;
                         state.cursorRight(used);
                     } else if (edge == RectangleEdge.TOP) {
                         used = findMaximumTickLabelHeight(ticks, g2, plotArea, isVerticalTickLabels()) * maxLinesUsed;
                         state.cursorUp(used);
                     } else if (edge == RectangleEdge.BOTTOM) {
                         used = findMaximumTickLabelHeight(ticks, g2, plotArea, isVerticalTickLabels()) * maxLinesUsed;
                         state.cursorDown(used);
                     }
                 }

                 return state;


             }

         };

As you can see in the picture above, the new line function works, but the spacing for the labels does not work. I tried to override the getLabelEnclosure method, but its given string is just "".

Does anyone know a solution for my problem. Either the multiline or an other way to align the charts?

thanks!

Peet Zahut
  • 43
  • 1
  • 7

1 Answers1

0

Override the reserveSpace function and in the place where the height is calculated modify it with the number of rows you need:

Rectangle2D labelEnclosure = getLabelEnclosure(g2, edge);
if (RectangleEdge.isTopOrBottom(edge)) {
    double labelHeight = labelEnclosure.getHeight();
    space.add(labelHeight + **YOUR_NUMBER_OF_ROWS** * tickLabelHeight, edge);
} else if (RectangleEdge.isLeftOrRight(edge)) {
    double labelWidth = labelEnclosure.getWidth();
    space.add(labelWidth + tickLabelWidth, edge);
}

return space;
ALabrosk
  • 300
  • 1
  • 3
  • 12