1

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

私人无效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));         
    }
}
4

1 回答 1

1

JFreeChart 允许您在每个绘图中拥有多个渲染器。您可以将 aXYStepRenderer与 a结合使用XYLineAndShapeRenderer,此示例改编自 JFreeChart Demos

在此处输入图像描述

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);
    }

}

在此示例中,我将 theplots initial Renderer 替换为 a XYStepRenderer,然后为 second 添加第二XYLineAndShapeRendererXYDataset。一个更简单的方法是添加第二个 Renderer,因为 ChartFactory#createXYLineChart()XYLineAndShapeRenderer默认会创建一个。

于 2012-10-23T07:56:39.363 回答