0
votes

I am trying to create a stack interval bar chart for a particular period. Below is my data set

private static DefaultIntervalCategoryDataset createDataSetForCountryStackBar() {

String[] seriesKeys = {"Pre polluted", "Post polluted"}
String[] categoryKeys = {"Thames"};

Number[][] starts = {{1987},{1985}};
Number[][] ends = {{1985},{1983}};

DefaultIntervalCategoryDataset dataset =

      new DefaultIntervalCategoryDataset(seriesKeys,categoryKeys,starts,ends);




    return dataset;
}

......

IntervalBarRenderer renderer = new IntervalBarRenderer();

CategoryPlot plot = new CategoryPlot(dataset,domainAxis,valueAxis1,renderer);

chart = new JFreeChart("DefaultIntervalCategory Demo",plot);   

I want to use the same data set with the stacked bar renderer. I have tried using it but it's value always start with the least value...and it generate stack bar from period 1980...till 1985 and 1985 - 1987

How to implement stack bar with interval data set without starting from the minimum value ?

3

3 Answers

0
votes

Looks ok to me if you set the Range

CategoryPlot plot = (CategoryPlot) chart.getPlot();

ValueAxis rageAxis = plot.getRangeAxis();
rageAxis.setRange(new Range(1980, 1990));

IntervalBarRenderer renderer = new IntervalBarRenderer();
plot.setRenderer(renderer);

enter image description here

0
votes

I got the solution for my requirement. I created multiple dataset/interval bar renderer. First dataset and renderer are set at index position 0 which is default and the other dataset/renderer is set at index position 1. This gives an effect of a stack bar using interval bar renderer.

0
votes

Jfree chart does not give any appropriate apis to make stacked interval bar charts. You have appropriate apis to make interval bar charts, as demonstrate in an answer above. But the intervals listed above they are not stacked.

To make a stacked interval bar chart along the Y axis, the only way I found to do this is quite dirty and hacky and therefor, I am opting for using interval bar charts instead, even if this is less than ideal.

To make a stacked interval bar chart what you can do is.

(a) Say you two data series Data Series 1 - Travel Time of an edge Data Series 2 - Wait Time at end of an edge

(b) You would have intervals of the form:

double [] [] lowerBoundInterval = { {startTravelTimeEdge1, StartStravelTimeEdge2}, {startSleepingAtEdge1, startSleepingAtEdge2} }

(c) You would have the same two time series with an upper bound

double [] [] upperBoundInterval = { {endTravelTimeEdge1, endTravelTimeEdge2}, {endSleepingAtEdge1, endSleepTimeEdge2} }

Now if you try to use the ChartFactory.createStackedBarChart, what will happen to you is that JfreeChart will treat each interval (tralveTime) and (sleepTime) as full length bars that it stacks together with the length of the end time. And your bars will start from zero. So you will not see any interval at all. You will something that does not make any sense for what you want in fact to represent.

In order to work around this limitation, what I found out to "kind of work" - but I do not use it because it is too dirty for my taste, is the following.

You can create a third data series, that I call Invisible artificial data series.

This data series corresponds to your lower bound value of the other two data series that you want to stack together. In the example I am giving, the startTravelTimeOnEdge(i) corresponds to the values we want to copy over to our artiticial data series.

After that, you need to turn the real data series you care about, data seris sizes. In this case, this means we will compute interval data series of the from: [0 - (Endtralve-timeEdge1 - StarttravelTimeEdge1)]. Why? Because the first invisible aritficial time series ensures the relevant start series start being stacked together from the current time point. And by making relevant intervals become lengths, you paint a region of chart that correponds to what you want to see painted.

To make the invisible time series be invisible you will want to tune your GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer() E.g.

renderer.setSeriesPaint(0, new Color(0, 0, 0, 0));

Something like above.

What this then does, is that you have a first dummy aritifial time series that serves the purpose of moving the first time series you are about to be painted at the right height.

NOTE: To be perfectly clear what I meant with the hacky arithmetic of the data series. I will illustrate what the hacky data series could look like.

Your lower bound and upper bound time series become:

      double [] [] lowerBoundInterval = {
 // atificial invisible data series
 {startTravelTimeEdge1, StartStravelTimeEdge2},
 // start travel time series
 {0, 0},
 // start sleep time series
 {0, 0} }


        double [] [] upperBoundInterval = { 
// atificial invisible data series
 {startTravelTimeEdge1, StartStravelTimeEdge2}
,
{endTravelTimeEdge1 - startTravelTimeEdge1, endTravelTimeEdge2 - startTravelTimeEdge2}, {endSleepingAtEdge1 - startSleepingAtEdge1, endSleepTimeEdge2 - startSleepTimeEdge2} }

With the above example, I am sure you get the picture of the logic behind this hacking of jfree chart apis.

To summarize The solution is: (A) Invent a new useless aritficial time series to move the bars up for the data series you care about. And make the bar of the artificial data series become invisible (b) Turn your data series from the real intervals that they represent into the length that they represent when stacked

Paing the first data series invisible, so that you make a gap in you stack. Let your interval range data series be colored.

And you get your interval data series.

This is too much work and nobody having to maintain code for rendering an interval based stack chart will like to look at such code. So, I would recommend that if you are allowed, use the pure interval charts that are not stacked.enter image description here You get more data along the X for the same category ... but in terms of maintenance cost it is much lower and the readability of the cahrt ... perhaps it is not as intuitive, but it is not that far off in any case.

THe following image illustrates a stacked invterval bar chart using this approach.