Help with JFreeChart overlay

这一生的挚爱 提交于 2019-11-29 14:51:46

问题


I'm having an issue with a JFreeChart overlaid graph. I'm using JFreeChart 1.0.13. What I am trying to do seems like it was easier to do in earlier versions of JFreeChart?

The graph shows a line chart and a bar chart. The data range plotted by the line chart for the Y axis is in the 0-100 range, and the axis for the bar chart is in the 0-5 range.

Individually, when I lay out each chart and paint it, they look great. Here's an example:

Bar Chart

Line Chart

But when I overlay them, the bar chart gets scaled down to look incredibly useless... presumably because the scales of the two datasets are so different.

Example:

What I really want is to split the series data for the two datasets, and display the 0-100 range for the line chart on the left hand Y axis and to have the bar chart displayed full size as in my first example below, but have the scale 0-5 displayed on the right hand Y axis side of the graph.

To create the graph, I am first creating the bar chart using an XYSeriesCollection, adding the data and creating the plot...

XYSeriesCollection histogramDataset= new XYSeriesCollection();
XYSeries xy= new XYSeries("Temp");
xy.add(100,0.0);
xy.add(101,0.3769791404875597);
histogramDataset.addSeries(xy);
...
final NumberAxis xAxis = new NumberAxis("Temperature C");
xAxis.setAutoRangeIncludesZero(false);
final ValueAxis yAxis = new NumberAxis("Percent Time above Temperature");
final XYItemRenderer renderer = new XYBarRenderer();

final XYPlot plot = new XYPlot((XYDataset) histogramDataset, xAxis, yAxis, renderer);

Then I create the line chart in a similar way and add the second series to the plot...

final XYSeries xy = new XYSeries("First");
final XYDataset xySeriesData = new XYSeriesCollection();
final XYItemRenderer xyLineRenderer = new XYLineAndShapeRenderer();
xyLineRenderer.setSeriesShape(0, new Line2D.Double(0.0, 0.0, 0.0, 0.0));
xyLineRenderer.setSeriesStroke(0, new BasicStroke(4.5f));
xy.add(100,100.0);
xy.add(101,100.0);
xyseriesData.add(xy);
plot.setDataset(1, xySeriesData);
plot.setRenderer(1, xyLineRenderer);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

My suspicion is that somehow each graph needs to be it's own plot and combined together. Can anyone help? What I am going for here is something like this:

Except I don't want the bar chart to be a background image. The X axis should be the same, and the Y axis should be on the right hand side with the proper scale that allows the graph to be shown full size.

Any/all replies are appreciated...


回答1:


I see you're adding a second data set and renderer to your plot, but you're forcing them to use the same range axis. Instead, give the plot a new NumberAxis for the range of the second data set.

As an aside, don't overlook the axis related example in the JFreeChart Demo, and don't underestimate the value of the JFreeChart Developer Guide.



来源:https://stackoverflow.com/questions/3702083/help-with-jfreechart-overlay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!