Labels for XYDotRenderer dots

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mattrk
Posts: 1
Joined: Tue Jun 16, 2020 3:27 pm
antibot: No, of course not.

Labels for XYDotRenderer dots

Post by mattrk » Wed Jun 17, 2020 10:02 am

Hello,

A brief note for those looking for dot-specific labels when using XYDotRenderer...

For some reason the capability to make labels visible does not appear to be in XYDotRenderer.

A rapid temporary work around is as follows:

Code: Select all

/*
 * !!!!!! TEMPORARY WORK AROUND. USES CODE COPIED FROM JFreeChart (LGPL)
 */
public class XYDotRendererWithLabels extends XYDotRenderer {

    public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
                         XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
                         int series, int item, CrosshairState crosshairState, int pass) {
        super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis,
                dataset, series, item, crosshairState, pass);

        PlotOrientation orientation = plot.getOrientation();
        double x = dataset.getXValue(series, item);
        double y = dataset.getYValue(series, item);
        RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
        RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
        double transX1 = domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
        double transY1 = rangeAxis.valueToJava2D(y, dataArea, yAxisLocation);
        if(isItemLabelVisible(series, item)) {
            drawItemLabel(g2, orientation, dataset, series, item, transX1, transY1, (y < 0.0));
        }
    }
}
I hope this helps someone. It would be useful if this feature was in the main code base (in my opinion only).

Regards, Matthew

Locked