TimeSeries: how can I render fill under the line (or, appear to be an area graph)
I am graphing CPU load average over time (5s intervals). Traditionally this is done as a solid filled area graph rather than a single floating line, for example ...
It seems a very simple request, but I've not been able to find the solution.
My one remaining avenue is to subclass the default renderer .. but I keep thinking there has to be something simpler.
Y开发者_运维知识库our clues & other pointers would be most welcome.
M.
Maybe I am a little bit late, but I have a possible solution for you. jfreechart allows to make these charts.
Since you need for CPU load chart, the best type of chart is TimeSeriesChart. The best dataset using for this chart is TimeSeriesCollection. In the picture your timeseries collection contains 2 timeseries.
The main solution is to use 2 renderers with 2 separate datasets. The first render (that displays solid field area) should be XYAreaRenderer. The second (that displays horizontal line) should be XYLineAndShapeRenderer.
Here is an example how we can use it:
JFreeChart chart = ChartFactory.createTimeSeriesChart(chartTitle,
axisXTitle, axisYTitle, dataset, true, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
XYAreaRenderer renderer = new XYAreaRenderer();
renderer.setSeriesVisible(0, true);
renderer.setSeriesVisible(1, false);
plot.setRenderer(0, renderer);
// this part is important, we should create
// the new dataset object exactly the same as original
TimeSeriesCollection newDataset = null;
if (plot.getDataset(0) instanceof TimeSeriesCollection) {
newDataset = (TimeSeriesCollection)
((TimeSeriesCollection) plot.getDataset()).clone();
}
plot.setDataset(1, newDataset);
// add new XYLineAndShapeRenderer to display capacity
XYLineAndShapeRenderer lineAndShapeRenderer = new XYLineAndShapeRenderer(true, false);
lineAndShapeRenderer.setSeriesVisible(0, false);
lineAndShapeRenderer.setSeriesVisible(1, true);
plot.setRenderer(1, lineAndShapeRenderer);
Of course you can customize these renderers as you want (such as set custom colors and so on). Please, see more information at this interface: XYItemRenderer.
Also, you can customize the chart (plot). Look here: Plot.
You are probably looking for an org.jfree.chart.renderer.AreaRenderer
. Several examles are shown in the java-web-start sample demo under Area Charts. Alternatively, an XYBarRenderer
makes a nice histogram.
精彩评论