【问题标题】:How to add series label to the line graph in java?如何在java中的折线图中添加系列标签?
【发布时间】:2021-01-10 11:52:47
【问题描述】:

我想为图表添加标签,例如

我得到的是

我无法将这些系列名称添加到上面的图表中(使用 excel 绘制)。
代码在这里


        // Population in 2005

        Random rand = new Random();

        // Generate random integers in range 0 to 999
        //int rand_int1 = rand.nextInt(1000);
        /*for(int i=0;i<12;++i){
            dataset.addValue(status(code.getText(),dater(i)), "Status", monthname(i));
            }*/
        for(int i=0;i<12;++i){
        dataset.addValue(2500+rand.nextInt(1000), "USA", String.valueOf(2000+i));
        dataset.addValue(2000+rand.nextInt(1000), "India", String.valueOf(2000+i));
        dataset.addValue(1500+rand.nextInt(1000), "China", String.valueOf(2000+i));
        }

        JFreeChart barChart = ChartFactory.createLineChart(analysisof(code.getText()), "Year", "Amount", dataset, PlotOrientation.VERTICAL, false, true, false);
        CategoryPlot barchrt = barChart.getCategoryPlot();
        barchrt.setRangeGridlinePaint(Color.orange);
        ChartPanel barpanel = new ChartPanel(barChart);
        panelchart.removeAll();
        panelchart.add(barpanel,BorderLayout.CENTER);
        panelchart.validate();
        this.repaint();

【问题讨论】:

  • 这两个图是不同的数据,但标签像第一个图,即 series1,series2,series3。它希望在第二个图中使用 java 的这个系列标签。
  • ChartFactory 中将legend 参数设置为true 时会发生什么?
  • 它工作正常。谢谢@trashgod

标签: java graph frame jfreechart linegraph


【解决方案1】:

正如here 所建议的那样,您选择的ChartFactory 中指定的legend 参数将不变地传递给factory's JFreeChart 构造函数,作为“指定是否需要图例的标志。”将该值设置为 true 会在图表中添加一个 LegendTitle

JFreeChart barChart = ChartFactory.createLineChart(
    …
    PlotOrientation.VERTICAL,
    true, // legend
    true, // tooltips
    false // urls
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    相关资源
    最近更新 更多