如何在折线图中显示系列的值。
我当前的代码
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;
}
任何人都可以告诉我们如何在这个
中显示价值答案 0 :(得分:1)
为了运行您的代码我创建了一个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
中看到一些输出:
答案 1 :(得分:0)
enter image description here //单击以显示图形
import java.text.DecimalFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.data.category.DefaultCategoryDataset;
public class GraficaLineaChart {
public static void main(String[] args) {
//Create the object dataset to put the values to draw the line
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(6, "2019", "Alex");
dataset.setValue(7, "2020", "Alex");
dataset.setValue(8, "2019", "Carmen");
dataset.setValue(5, "2020", "Carmen");
dataset.setValue(12, "2019", "Tony");
dataset.setValue(9, "2020", "Tony");
//Create the chart object for the type Line Chart. Tip: the second field "true" is very important to the draw the value top the line
JFreeChart chart = ChartFactory.createLineChart( "Promedio de calificaciones 2019-2020", "Alumnos", "Calificaciones", dataset, PlotOrientation.VERTICAL, true, true, false);
CategoryPlot plot = chart.getCategoryPlot();
//Create of the renderer object to the draw point and the value
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
//Define the format to the value to the draw
DecimalFormat decimalformat1 = new DecimalFormat("##");
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1));
renderer.setItemLabelsVisible(true);
renderer.setSeriesVisible(true);
//Show of the graph in the desktop
ChartFrame frame = new ChartFrame("Ejemplo
Grafica de Lineas", chart);
frame.pack();
frame.setVisible(true);
}
}