I was wondering if it is possible to plot the dataset itself along with the graph while generating a jpg file.
To illustrate my question:
a. What I need:

b. What I've done:

My source code:
- Code: Select all
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class LineChartDemo1 {
private static final long serialVersionUID = 7032394551486104761L;
public CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(755.29, "Revenue", "01-JUL-2010");
dataset.addValue(305.92, "Revenue", "02-JUL-2010");
dataset.addValue(102.00, "Revenue", "03-JUL-2010");
dataset.addValue(437.47, "Revenue", "04-JUL-2010");
dataset.addValue(269.66, "Revenue", "05-JUL-2010");
dataset.addValue(1503.76, "Revenue", "06-JUL-2010");
return dataset;
}
public JFreeChart createChart(final CategoryDataset dataset) {
JFreeChart chart = ChartFactory.createLineChart(
"Revenue - Past 30 Days",
"Date",
"Revenue",
dataset,
PlotOrientation.VERTICAL,
false,
false,
false);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinePaint(Color.black);
return chart;
}
public void saveChart(JFreeChart chart) {
try {
ChartUtilities.saveChartAsJPEG(new File("test.jpg"), chart, 800, 400);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(final String[] args) {
LineChartDemo1 demo = new LineChartDemo1();
demo.saveChart(demo.createChart(demo.createDataset()));
}
}
I did search for the same in the forums and a lot on Google. But my query contains the term 'dataset' which brings up a lot of unrelated (in my context) as all pages which have a code snippet have dataset in them.
So I'd appreciate any pointers in this regard. Is this doable? If yes, how?
Thanks in advance for your time.
