D. Sill
D. Sill

Reputation: 307

Setting Angle tick labels of JFreeChart Polar Chart in JavaFX Application

I'm trying to use JFreeChart's Polar Chart inside a JavaFX application. So far I've been able to create the chart and get it to plot the data.

I'm now trying to set the angular tick labels to show -90 degrees to +90 rather than 0 to 360.

Firstly, I've created an XYDataSet of the Angles and Values I need. Then I create a JFreeChart, get the plot from this, set various properties, and then use a ChartViewer to display it. As you can see below.

XYDataSet data = createDataSet();
final JFreeChart chart = ChartFactory.createPolarChart("myTitle",dataSet,false,false,false);
final PolarPlot plot = (PolarPlot) chart.getPlot();

plot.setAngleOffset(0);
plot.setCounterClockwise(true);

final DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) plot.getRenderer();
renderer.setShapesVisible(false);
renderer.setConnectFirstAndLastPoint(false);

ChartViewer viewer = new ChartViewer(chart);

I can then add this viewer as a child to a JavaFX pane to be displayed.

After some research online I found a method to override the tick labels of a polar plot using something like below;

final PolarPlot plot = new PolarPlot() {
        @Override 
        protected List refreshAngleTicks(){
            List ticks = new ArrayList();
            ticks.add(new NumberTick(-90, "-90", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
            ticks.add(new NumberTick(-60, "-60", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
            ticks.add(new NumberTick(-30, "-30", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
            ticks.add(new NumberTick(ZERO, "0", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
            ticks.add(new NumberTick(30, "30", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
            ticks.add(new NumberTick(60, "60", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
            ticks.add(new NumberTick(90, "90", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
            return ticks;

        }
    };

Is there a way I can use this when I am not creating a new PolarPlot, and instead, am getting it from a chart.getPlot() method?

If you need any further information please just ask.

As a side note, if there is an easy way to remove the entire left side of the polar plot (ie have a D shape rather than a full circle, please let me know. Not critical, but while we're on the subject I thought I'd throw it in!)

Thanks in Advance.

Darren.

Upvotes: 2

Views: 533

Answers (1)

D. Sill
D. Sill

Reputation: 307

Thanks to trashgod with the pointer to a similar question I was able to fix this.

Basically I now create an axis, being careful to import org.jfree.chart.axis.ValueAxis and org.jfree.chart.axis.NumberAxis, rather than the JavaFX ones, and create a renderer and PolarPlot manually, rather than using the ChartFactory.createPolarChart() method, as seen below. This allows me to override the refreshAngleTicks() method when I create the PolarPlot.

ValueAxis ax = new NumberAxis();
ax.setTickLabelsVisible(false);
DefaultPolarItemRenderer renderer = new DefaultPolarItemRenderer();
PolarPlot plot = new PolarPlot(dataset, ax, renderer){
    @Override
    protected List refreshAngleTicks(){
        List ticks = new ArrayList();
        ticks.add(new NumberTick(-90, "-90", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
        .....etc....
        return ticks;
    }
};
plot.setAngleOffset(0);
plot.setCounterClockwise(true);

renderer.setShapesVisible(false);

JFreeChart chart = new JFreeChart("my title", JFreeChart.DEFAULT_TITLE_FONT, plot, false);

I can then display this chart using the same ChartViewer as before.

Hope this helps anyone else in a similar situation!

Upvotes: 2

Related Questions