Java需要一些但不是所有行的stepRender行

时间:2012-10-23 04:17:37

标签: java jfreechart renderer

我有一个JFreeChart,上面画着几条不同的线条。我需要将一些行更改为步骤行。我知道我需要使用CategoryStepRenderer但是在实现它时遇到了麻烦。我附上了以下用于更改线条颜色的代码。我希望能够以类似的方式完成步骤。我发现的所有方法都涉及改变所有的线条。感谢

private void updateChartColor(ArrayList mlColor,ArrayList mlLine){

    XYPlot plot=(XYPlot)chart.getPlot();
    XYLineAndShapeRenderer renderer=(XYLineAndShapeRenderer)plot.getRenderer();
    for (int i = 0; i < mlColor.size(); i++) {
        renderer.setSeriesPaint(i, mlColor.get(i));         
    }
}

1 个答案:

答案 0 :(得分:1)

JFreeChart允许您为每个绘图添加多个渲染器。您可以将XYStepRendererXYLineAndShapeRenderer合并,此示例改编自JFreeChart演示

enter image description here

import java.awt.BasicStroke;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class XY2RendererDemo extends ApplicationFrame {

    public XY2RendererDemo(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 300));
        setContentPane(chartPanel);
    }
    private static JFreeChart createChart(XYDataset[] dataset) {
        JFreeChart chart = ChartFactory.createXYLineChart(
            "XYStepRenderer & XYLineAndShapeRenderer",
            "X",
            "Y",
            dataset[0],
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainPannable(true);
        plot.setRangePannable(true);

        {
            XYStepRenderer renderer = new XYStepRenderer();
            renderer.setBaseShapesVisible(true);
            renderer.setSeriesStroke(0, new BasicStroke(2.0f));
            renderer.setSeriesStroke(1, new BasicStroke(2.0f));
            renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
            renderer.setDefaultEntityRadius(6);
            plot.setRenderer(renderer);
        }
        {
            XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
            plot.setDataset(1, dataset[1]);
            plot.setRenderer(1, renderer2);
        }

        return chart;
    }

    private static XYDataset[] createDataset() {
        XYSeries series1 = new XYSeries("Series 1");
        series1.add(1.0, 3.0);
        series1.add(2.0, 4.0);
        series1.add(3.0, 2.0);
        series1.add(6.0, 3.0);
        XYSeries series2 = new XYSeries("Series 2");
        series2.add(1.0, 7.0);
        series2.add(2.0, 6.0);
        series2.add(3.0, 9.0);
        series2.add(4.0, 5.0);
        series2.add(6.0, 4.0);
        XYSeriesCollection dataset1 = new XYSeriesCollection();
        dataset1.addSeries(series1);
        XYSeriesCollection dataset2 = new XYSeriesCollection();
        dataset2.addSeries(series2);
        return new XYDataset[]{dataset1,dataset2};
    }

    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        ChartPanel panel = new ChartPanel(chart);
        panel.setMouseWheelEnabled(true);
        return panel;
    }

    public static void main(String[] args) {
        XY2RendererDemo demo = new XY2RendererDemo(
            "JFreeChart: Using 2 Renderers");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

在此示例中,我将使用XYStepRenderer替换plots初始渲染器,然后为第二个XYLineAndShapeRenderer添加第二个XYDataset。一个更简单的方法是只添加第二个渲染器,因为ChartFactory#createXYLineChart()默认会创建一个XYLineAndShapeRenderer