如何在折线图中显示系列值。
我当前的代码
public void renderChart(String variation, OutputStream stream) throws Exception {
boolean rotate = "rotate".equals(variation); // add ?variation=rotate to the URL to rotate the chart
JFreeChart chart = generateChart(rotate);
ChartUtilities.writeChartAsPNG(stream, chart, 750, 400);
}
private JFreeChart generateChart(boolean rotate) {
DefaultCategoryDataset data = ChartData.getDataset();
JFreeChart chart = ChartFactory.createLineChart("example graph", // title
"x-axis", // x-axis label
"y-axis", // y-axis label
data, rotate ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, true, // legend displayed
true, // tooltips displayed
false); // no URLs*/
CategoryPlot plot = (CategoryPlot) chart.getPlot();
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
DecimalFormat decimalformat1 = new DecimalFormat("##");
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1));
renderer.setItemLabelsVisible(true);
renderer.setSeriesVisible(true);
return chart;
}
谁能告诉我如何在此显示值
最佳答案
为了运行您的代码,我创建了一个 main
方法,并创建了一个创建一些示例数据的方法 ChartData.getDataset()
。而且你的代码运行得很好。
所以我猜你的问题是你没有我刚才提到的这两种方法。
以下是创建简单测试数据集的方法:
class ChartData {
public static DefaultCategoryDataset getDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1, "Foo", "A");
dataset.addValue(10, "Foo", "B");
dataset.addValue(5, "Foo", "C");
dataset.addValue(2, "Bar", "A");
dataset.addValue(3, "Bar", "B");
dataset.addValue(8, "Bar", "C");
return dataset;
}
}
这是一个 main
方法:
public class JFreeChartSnippet {
//// ////
// Copy your code from your question here //
//// ////
public static void main(String[] args) throws Exception {
new JFreeChartSnippet().renderChart("rotate", new FileOutputStream("foobar.png"));
}
}
现在您应该在文件 foobar.png
中看到一些输出:
关于java - JFreeChart 以折线图显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23242433/