首页 > 解决方案 > 不同图表数据集的颜色不会改变

问题描述

XYSeriesCollection dataset = new XYSeriesCollection();

XYSeries validKoordinates = new XYSeries("Not dangerous");
for (SimulationResult sr : validSR.values()) {
     validKoordinates.add(sr.getMinTTC(), sr.getMinTHW());  //x,y
}

dataset.addSeries(validKoordinates);
JFreeChart chart = chart = ChartFactory.createScatterPlot(
            "Distribution of THW and TTC Values",
            "TTC", "THW", dataset);

//Changes background color
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(new Color(255, 228, 196));
plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(0, Color.GRAY);

XYSeriesCollection dataset2 = new XYSeriesCollection();
XYSeries warningKoordinates = new XYSeries("Very critical");
for (SimulationResult sr : criticalSR.values()) {
    warningKoordinates.add(sr.getMinTTC(), sr.getMinTHW());  //x,y
}
dataset2.addSeries(warningKoordinates);

plot.setDataset(1, dataset2);
plot.getRendererForDataset(plot.getDataset(1)).setSeriesPaint(0, Color.RED);

所以我有两个不同的数据集(数据集 1 和数据集 2)。每个数据集包含不同的值。我的目标是将数据集 1 的颜色更改为灰色,将第二个数据集的颜色更改为红色。(但形状应该相同)。一开始我只有一个带有 2 个 XYSeries 的数据集。当时的问题是每个 XYSeries 的形状都不一样。现在我有相反的问题。形状不变,但颜色不变。

这是我的桌子现在的样子:

图表

正如你现在所看到的,我无法区分哪一个是非常关键的,哪一个不是。

标签: javachartsjfreechart

解决方案


It is found that only one dataSet is required, all series should be added to the dataSet. And using the first parameter of setSeriesPaint to configure color of the series. Since the version of jfreeChart is not provide, I used 1.0.12. The code is edited for testing.

public static void main(String[] args) throws IOException {
    XYSeriesCollection dataset = new XYSeriesCollection();
    
    int[][] sr = new int[3][2];
    for (int i = 0; i < sr.length; i++) {
        sr[i][0] = i + 3;
        sr[i][1] = 2 * (i + 1) + 1;
    }
    XYSeries validKoordinates = new XYSeries("Not dangerous");
    XYSeries warningKoordinates = new XYSeries("Very critical");
    System.out.println(sr.length);
    for (int i = 0; i < sr.length; i++) {
        validKoordinates.add(sr[i][0], sr[i][1]);
    }
    for (int i = 0; i < sr.length; i++) {
        warningKoordinates.add(sr[i][0]+1, sr[i][1]+1);
    }
    // Add dataset for all series
    dataset.addSeries(validKoordinates);
    dataset.addSeries(warningKoordinates);
    // Only reference one dataset
    JFreeChart chart = ChartFactory.createScatterPlot("Distribution of THW and TTC Values", "TTC", "THW", dataset,
            PlotOrientation.HORIZONTAL, true, true, true);
    XYPlot plot = (XYPlot) chart.getPlot();

    plot.setBackgroundPaint(new Color(255, 228, 196));
    // Change the index of setSeriesPaint to set color
    plot.getRendererForDataset(dataset).setSeriesPaint(0, Color.GRAY);
    plot.getRendererForDataset(dataset).setSeriesPaint(1, Color.RED);
    
    BufferedImage objBufferedImage = chart.createBufferedImage(600, 800);
    ByteArrayOutputStream bas = new ByteArrayOutputStream();
    try {
        ImageIO.write(objBufferedImage, "png", bas);
    } catch (IOException e) {
        e.printStackTrace();
    }

    byte[] byteArray = bas.toByteArray();
    InputStream in = new ByteArrayInputStream(byteArray);
    BufferedImage image = ImageIO.read(in);
    File outputfile = new File("testimage.png");
    ImageIO.write(image, "png", outputfile);
}

The image generated by the above method enter image description here


推荐阅读