不同图表数据集的颜色不会更改

时间:2018-08-27 13:06:17

标签: java charts jfreechart

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的形状都不同。现在我有相反的问题。形状保持不变,但颜色不变。

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

chart

正如您现在所看到的,我不能分开哪个是非常关键的,不是鞭itch的。

1 个答案:

答案 0 :(得分:1)

发现只需要一个dataSet,所有系列都应添加到dataSet中。然后使用setSeriesPaint的第一个参数来配置系列的颜色。由于未提供jfreeChart的版本,因此我使用1.0.12。已对代码进行了编辑以进行测试。

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);
    }

通过上述方法生成的图像 enter image description here