Java JFreeChart:设置 XY 图表的线条颜色 - 4 个系列,2 个数据集,双轴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21427762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:41:16  来源:igfitidea点击:

JFreeChart: set line colors for XY Chart - 4 series, 2 datasets, dual axes

javajfreechart

提问by CL22

I can't seem to set individual line colors for all four lines. When I use the lines:

我似乎无法为所有四行设置单独的线条颜色。当我使用这些行时:

plot.getRenderer().setSeriesPaint(0, new Color(0x00, 0xFF, 0x00));
plot.getRenderer().setSeriesPaint(1, new Color(0x00, 0x00, 0x00));

(In the code below), it applies the first line to the FIRST series in BOTH datasets, and the second line to the SECOND series in BOTH datasets.

(在下面的代码中),它将第一行应用于 BOTH 数据集中的 FIRST 系列,并将第二行应用于 BOTH 数据集中的 SECOND 系列。

How can I set a different color for all 4 lines?

如何为所有 4 行设置不同的颜色?

Thanks!

谢谢!

private JFreeChart createXYLineChart(String title) {
    XYDataset dataset1 = createXYVoltageDataset();
    XYDataset dataset2 = createXYCurrentDataset();

    JFreeChart chart = ChartFactory.createXYLineChart("Profile", "Set Current", "Voltage", null);
    XYPlot plot = (XYPlot) chart.getPlot(); 
    plot.setDataset(0, dataset1);
    plot.setDataset(1, dataset2);

    plot.setRangeAxis(1, new NumberAxis("Actual Current")); 
    plot.mapDatasetToRangeAxis(1, 1); //2nd dataset to 2nd y-axi

    plot.setBackgroundPaint(new Color(0xFF, 0xFF, 0xFF));
    plot.setDomainGridlinePaint(new Color(0x00, 0x00, 0xff));
    plot.setRangeGridlinePaint(new Color(0xff, 0x00, 0x00));

    plot.getRenderer().setSeriesPaint(0, new Color(0x00, 0xFF, 0x00));
    plot.getRenderer().setSeriesPaint(1, new Color(0x00, 0x00, 0x00));
    //plot.getRenderer().setSeriesPaint(2, new Color(0xFF, 0x00, 0x00)); // Does nothing
    //plot.getRenderer().setSeriesPaint(3, new Color(0x00, 0x00, 0xFF)); // Does nothing
    //plot.getRenderer(1).setSeriesPaint(3, new Color(0x00, 0x00, 0xFF)); // Null pointer exceptiopn

    return chart;
}

private  XYDataset createXYVoltageDataset() {
    final XYSeries s1 = new XYSeries("Min Voltage");
    final XYSeries s2 = new XYSeries("Max Voltage");
    for (int i = 0; i < profile.getNumSteps(); i++) s1.add(i, profile.getStepMinVoltage(i));
    for (int i = 0; i < profile.getNumSteps(); i++) s2.add(i, profile.getStepMaxVoltage(i));
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(s1);
    dataset.addSeries(s2);
    return dataset;
}
private  XYDataset createXYCurrentDataset() {
    final XYSeries s1 = new XYSeries("Min Current");
    final XYSeries s2 = new XYSeries("Max Current");
    for (int i = 0; i < profile.getNumSteps(); i++){
        s1.add(i, profile.getStepMinCurrent(i));
    }
    for (int i = 0; i < profile.getNumSteps(); i++) s2.add(i, profile.getStepMaxCurrent(i));
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(s1);
    dataset.addSeries(s2);
    return dataset;
}

回答by Jannis Alexakis

I′d create my own plot and two renderers, with newinstead of chart.getPlot()or plot.getRenderer(). I′ll try to give an example similar to your code so you see what I mean ; you′ll have to adjust it to suit your needs:

我会创建我自己的情节和两个渲染器,new而不是chart.getPlot()plot.getRenderer()。我将尝试给出一个类似于您的代码的示例,以便您明白我的意思;您必须对其进行调整以满足您的需求:

 private JFreeChart createChart(String title) {
    XYDataset dataset1 = createDataset1();
    XYDataset dataset2 = createDataset2();

    XYBarRenderer renderer1 = new XYBarRenderer(0.20000000000000001D);
    renderer1.setSeriesPaint(0, Color.BLUE);
    renderer1.setSeriesPaint(1, Color.red);
    DateAxis domainAxis = new DateAxis("Date");
    NumberAxis valueAxis = new NumberAxis("Value");
    XYPlot plot = new XYPlot(dataset1, domainAxis, valueAxis, renderer1);

    StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
    renderer2.setSeriesPaint(0, Color.CYAN);
    renderer2.setSeriesPaint(1, Color.YELLOW);

    plot.setDataset(1, dataset2);
    plot.setRenderer(1, renderer2);

    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);

    return chart;
}

EDIT: By the way, if you use JFreeChart often, I′d recommend buying the guide. Apart from the PDF which is pretty useful, you get the source for all examples in the demo center, and that is invaluable.

编辑:顺便说一下,如果您经常使用 JFreeChart,我建议您购买指南。除了非常有用的 PDF 之外,您还可以在演示中心获得所有示例的源代码,这是非常宝贵的。

回答by CL22

This was my final solution:

这是我的最终解决方案:

XYDataset dataset1 = createXYVoltageDataset();
XYDataset dataset2 = createXYCurrentDataset();

XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
r1.setSeriesPaint(0, new Color(0xff, 0xff, 0x00)); 
r1.setSeriesPaint(1, new Color(0x00, 0xff, 0xff)); 
r1.setSeriesShapesVisible(0,  false);
r1.setSeriesShapesVisible(1,  false);

XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer();
r2.setSeriesPaint(0, new Color(0xff, 0x00, 0x00)); 
r2.setSeriesPaint(1, new Color(0x00, 0xff, 0x00)); 
r2.setSeriesShapesVisible(0,  false);
r2.setSeriesShapesVisible(1,  false);

JFreeChart chart = ChartFactory.createXYLineChart("Profile", "Set Current", "Voltage", null);
XYPlot plot = (XYPlot) chart.getPlot(); 

plot.setDataset(0, dataset1);
plot.setRenderer(0, r1);

plot.setDataset(1, dataset2);
plot.setRenderer(1, r2);

plot.setRangeAxis(1, new NumberAxis("Actual Current")); 
plot.mapDatasetToRangeAxis(1, 1); //2nd dataset to 2nd y-axi

plot.setBackgroundPaint(new Color(0xFF, 0xFF, 0xFF));
plot.setDomainGridlinePaint(new Color(0x00, 0x00, 0xff));
plot.setRangeGridlinePaint(new Color(0xff, 0x00, 0x00));

return chart;