java - 不同图表数据集的颜色不会改变
问题描述
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 的形状都不一样。现在我有相反的问题。形状不变,但颜色不变。
这是我的桌子现在的样子:
正如你现在所看到的,我无法区分哪一个是非常关键的,哪一个不是。
解决方案
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);
}
推荐阅读
- excel - 在 Excel 中分组并显示计数
- python - 奇点python ValueError:需要超过1个值才能解包
- node.js - 如何读取括在尖括号内的http请求查询
- php - 对将执行查询的可自由访问的路由进行 AJAX 调用时的安全问题
- vue.js - Vue + MSAL2.x + Azure B2C 配置文件编辑
- python - 使用 Python,绘制 2 天的盘中股票数据
- java - 如何将数据表作为场景大纲示例的一部分传递
- mysql - 是否有内置选项可以显示“相似”的行与一个不同的行?
- javascript - 使用带有 Yup 的正则表达式来验证仅包含扩展拉丁字母和至少 2 个单词的全名
- c# - 管理来自多个云提供商的资源