Reputation: 6783
I have a requirement to show time-series data as layered bar chart. Is it possible with JFreeChart? Any pointers would be really helpful.
The data would be a list of: (TS, X1, X2), where I've to plot X1 for a given Timestamp (TS) and X2 would basically serve as the label for the given value of X1.
Edit: Also, for the same TS, there might exist different X1 values. The idea is to denote all these X1 values as layered bars against the same TS.
Here's somewhat of an example of what I want:
(so instead of category, I'll have TS in X-axis)
Upvotes: 1
Views: 754
Reputation: 7268
It sounds like you want a BarChart (with x-axis determined by time) with the bars labelled with their values. You don't need to add a new data series for the labels, but modify the rendering of the plot.
Here's a simple example:
public class LabelledBarChartTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(10.0, "Series", new Integer(2010));
dataset.addValue(20.0, "Series", new Integer(2011));
dataset.addValue(30.0, "Series", new Integer(2012));
JFreeChart chart = ChartFactory.createBarChart(null,null,null,dataset,
PlotOrientation.VERTICAL,true,true,false);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryItemRenderer renderer = plot.getRenderer();
// label the points
NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(2);
CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator(
StandardXYItemLabelGenerator.DEFAULT_ITEM_LABEL_FORMAT, format, format);
renderer.setBaseItemLabelGenerator(generator);
renderer.setBaseItemLabelsVisible(true);
frame.setContentPane(new ChartPanel(chart));
frame.pack();
frame.setVisible(true);
}
}
Credit where credit is due - I got the labelling example from this example.
Upvotes: 2