Java Need stepRender lines for some but not all lines

末鹿安然 提交于 2019-12-24 17:55:58

问题


I have a JFreeChart that has several different lines plotted on it. I need to change some of the lines to step lines. I know I need to use the CategoryStepRenderer but am having trouble implementing it. I attached the following code that I use to change the color of the lines. I would like to be able to do the step line in a similar way. All of the ways I have found involve changing all of the lines. Thanks

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:


JFreeChart allows you to have more than one Renderer per plot. You can combine an XYStepRenderer with a XYLineAndShapeRenderer, this example is adapted from the 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);
    }

}

In this example I'm replacing theplots initial Renderer with a XYStepRenderer and the then adding a second XYLineAndShapeRenderer for the second XYDataset. A simpler method is to just add a second Renderer as ChartFactory#createXYLineChart() will create a XYLineAndShapeRenderer by default.



来源:https://stackoverflow.com/questions/13023502/java-need-steprender-lines-for-some-but-not-all-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!