我的JFreechart存在问题,因为每次刷新后系列颜色都会改变颜色。
我尝试设置系列颜色如下,但这没有帮助
plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(1, Color.GREEN);
我也试过
plot.getRenderer().setSeriesPaint(1, Color.GREEN);
plot.getRenderer().setSeriesPaint(2, Color.BLUE);
问题听起来与以下问题非常相似,但我有一个类别数据集(有两个系列)
Previous Stack Overflow Question
刷新发生在这里,但我不相信这是问题
private void passStingR(StringBuilder retVal) throws BadLocationException, Exception {
int getinitialscrollpos;
getinitialscrollpos = getinitialscrollPosition(scrollSystemDemand);
try {
CategoryPlot plot = (CategoryPlot) chartPanel.getChart().getPlot();//get plot of graph
//System.out.print("Plot Type" + plot);
ValueAxis rangeAxis = plot.getRangeAxis();//get range of axis of graph
System.out.print("Range " + rangeAxis);//get range lower and upper as an array
mapSystemDemand.put(1, rangeAxis);
} catch(Exception e) {
System.out.print("Error has occured");
} finally {
////////////////refresh data////
Document docR = null;
docR = loadXMLFromString(retVal.toString());//pull in the XML data into a new doc
populate1R(docR);
///////////////refresh data/////
tableSystemDemand.getTableHeader().setReorderingAllowed(false);//prevent user from changing column order now at refresh level
SimpleDateFormat time_formatterR = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current_time_strR = time_formatterR.format(System.currentTimeMillis());
updatetFieldDG.setText(current_time_strR);
middlePanel.add(scrollSystemDemand,BorderLayout.WEST);
scrollSystemDemand.getVerticalScrollBar().setValue(getinitialscrollpos);
System.out.println("End" + getinitialscrollpos);
getTableData(tableSystemDemand);
head(tableSystemDemand);
//revalidate();
createGraphGUI();
if (mapSystemDemand.get(1) != null) {
System.out.print("get map" + mapSystemDemand.get(1));
CategoryPlot plot = (CategoryPlot) chartPanel.getChart().getPlot();
plot.setRangeAxis(mapSystemDemand.get(1));//get range of axis of graph
}
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
chartPanel.setBorder(BorderFactory.createTitledBorder("Demand Refresh TimeDate Stamp " + dateFormat.format(cal.getTime())));
revalidate();
}
图表创建在这里:
private JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createLineChart(
"FD", // chart title
"DT", // domain axis label
"Q", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // the plot orientation
true, // legend
true, // tooltips
false // urls
);
chart.getTitle().setPaint(Color.BLACK);
chart.getTitle().setFont(new Font("Tahoma", Font.PLAIN, 15));
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//System.out.print(plot.getDatasetCount());
LegendTitle legend = chart.getLegend();
legend.setPosition(RectangleEdge.RIGHT);
legend.setItemFont(new Font("Tahoma", Font.PLAIN, 12));
plot.setBackgroundPaint(Color.BLACK);
plot.setRangeGridlinePaint(Color.WHITE);
plot.setDomainGridlinePaint(Color.WHITE);//y axis grid line
plot.setDomainGridlinesVisible(true);//y axis grid line true
plot.getDomainAxis().setLowerMargin(0.0);//places category axis starting right up to the vertical axis
plot.getDomainAxis().setUpperMargin(0.0);//places end of category axis right up to the end of the axis
plot.getDomainAxis().setCategoryMargin(0.0);
plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(20));
Font font3 = new Font("Tahoma", Font.PLAIN, 8);
plot.getDomainAxis().setTickLabelFont(font3);
plot.getRangeAxis().setTickLabelFont(font3);
plot.getDomainAxis().setLabelFont(font3);
plot.getRangeAxis().setLabelFont(font3);
// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// customise the renderer...
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(1, Color.GREEN);
plot.getRenderer().setSeriesPaint(1, Color.GREEN);
plot.getRenderer().setSeriesPaint(2, Color.BLUE);
//plot.getRendererForDataset(plot.getDataset(2)).setSeriesPaint(2, Color.RED);
//plot.getRendererForDataset(plot.getDataset(1)).setSeriesPaint(1, Color.RED);
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setFillPaint(Color.white);
chartPanel.setHorizontalAxisTrace(true);
chartPanel.setVerticalAxisTrace(true);
revalidate();
return chart;
}
另外一种方法是根据传奇中物品的名称来设定系列的颜色吗?