4

I am currently facing a problem using the JFreeChart. I require 2 domain axes which are related, eg:

|
|
|
|
|
|
|_________|_________ |____________(first axis)
8:00 - 9:00 9:01 - 10:00 10:01 - 11:00


|______|________|(second axis below the first axis)
Phase 1 Phase 2

That is to say, Phase 1 actually corresponds to 8:00 to 8:45 (on 1st axis) and Phase 2 8:46 to 9:20.

I am able to get some degree of success with the following codes

            private ChartPanel createChart(CategoryDataset dataset, CategoryDataset phaseDataset){
    JFreeChart chart = ChartFactory.createBarChart(
        "",
        "",
        "Numbers", 
dataset, 
PlotOrientation.Vertical, 
true, 
true, 
false);

CategoryPlot plot = chart.getCategoryPlot();

NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
rangeAxis.setStandTickUnits(NumberAxis.createIntegerTickUnits());

CategoryAxis domainAxis = plot.getDomainAxis();
// some paint settings to be done to domainAxis

CategoryAxis axis2 = new CategoryAxis()

plot.setDomainAxis(1, axis2);
plot.setDataset(1, phaseDataset);
plot.mapDatasetToRangeAxis(1,1);
plot.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);

ChartPanel panel = new ChartPanel(chart);

return panel;
}

My tick labels for the 2nd axis(for phases) seem to be on top rather that at the bottom.

How am I able to achieve this? Sorry for the crappy drawings, low rep to attach images...

Another question - how am I able to set the ranges for CategoryAxis? I wish for all categories to appear in my chart, but it seems like if there are no data in the category, it will not appear?

Back to design basics? Am I using the correct domain axis for this?

@EDIT I noticed that the second axis I added was not able to display ticks, the 2nd set of dataset actually appeared on the first axis...

0

1 Answer 1

4

I also had this problem and after a lengthy, tiresome search through the API I managed to find a solution. Here's a code fragment from a chart I had to construct that has 3 x-axes stacked on top of one another:

xyPlot.setDomainAxis(0, xAxis1);
xyPlot.setDomainAxis(1, xAxis2);
xyPlot.setDomainAxis(2, xAxis3);
xyPlot.setRangeAxis(0, yAxis);

xyPlot.mapDatasetToDomainAxis(0, 0);
xyPlot.mapDatasetToRangeAxis(0, 0);

xyPlot.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
xyPlot.setDomainAxisLocation(2, AxisLocation.BOTTOM_OR_LEFT);

Its a little late as an answer but hopefully this will save someone else the laborious effort I went through to find a fix!

1
  • 1
    Oh wow. Yeah, JFreeChart is incredibly powerful, but so underdocumented beyond the bare API docs. I've seen so many bad ways to accomplish this; thank you for bringing us the right way! Aug 21, 2019 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.