获得不同的系列,以获得相同的颜色

时间:2012-03-19 00:51:47

标签: java swing user-interface jfreechart

我有两个属于同一数据集的XYSeries。第一个系列必须仅显示形状,而第二个系列必须仅显示线条。两者必须具有相同的颜色。有办法吗?

提供一段代码以便更好地理解:

XYSeries series = new XYSeries("S1", false); // autosort disabled
    for(int i = 0; i < xValues.length; ++i)
        series.add(xValues[i], yValues[i]);

    XYSeries series1 = new XYSeries("S2", false);
    for(int i = 0; i < xValues.length; ++i)
        series1.add(yValues[i], xValues[i]);

    XYSeriesCollection dataset = new XYSeriesCollection();

    dataset.addSeries(series);

    dataset.addSeries(series1);

    // create a chart with title, axis labels, tooltips and maybe a legend
    JFreeChart chart = ChartFactory.createScatterPlot(title, xLabel, yLabel, dataset, 
            PlotOrientation.VERTICAL, legend, true, false);

    XYPlot plot = (XYPlot) chart.getPlot();
    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();

    renderer.setSeriesLinesVisible(0, true);
    renderer.setSeriesShapesVisible(1, false);
    renderer.setSeriesLinesVisible(1, true);

1 个答案:

答案 0 :(得分:3)

根据此example,我编辑了以下行以获取下面的图片。

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyPlot.getRenderer();
renderer.setSeriesLinesVisible(1, true);
renderer.setSeriesShapesVisible(1, false);
renderer.setSeriesPaint(1, Color.blue);

附录:要获得一对匹配的颜色,您可以覆盖getItemPaint() herehere

enter image description here