我似乎无法为所有四行设置单独的线条颜色。当我使用这些行时:
plot.getRenderer().setSeriesPaint(0, new Color(0x00, 0xFF, 0x00));
plot.getRenderer().setSeriesPaint(1, new Color(0x00, 0x00, 0x00));
(在下面的代码中),它将第一行应用于BOTH数据集中的FIRST系列,将第二行应用于BOTH数据集中的SECOND系列。
如何为所有4条线设置不同的颜色?
谢谢!
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;
}
答案 0 :(得分:5)
这是我的最终解决方案:
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;
答案 1 :(得分:1)
我创建自己的情节和两个渲染器,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;
}
编辑:顺便说一句,如果您经常使用JFreeChart,我建议您购买指南。除了非常有用的PDF之外,您还可以获得演示中心所有示例的来源,这是非常宝贵的。