使用JFreeChart为两个系列设置不同的y轴

时间:2015-04-07 14:46:23

标签: java jfreechart

我正在使用JFreeChart使用折线图绘制两个数据系列(XYSeries)。 复杂因素是其中一个数据系列的y值通常远高于我的第二个数据系列的y值(假设第一个系列的y值大小为数百万的数量级,而第二系列具有数百个数量级的y值。我的第一个数据集中存在高值导致绘图的范围使得我的第二个数据集的y值不再可理解。

在绘图中添加第二个y轴,这样我的数据系列都使用自己的y轴,可以解决这个问题。有谁知道如何用JFreeChart做到这一点?

完整性的当前代码:

XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("series1");
XYSeries series2 = new XYSeries("series2");

// Here is my code to fill series1 and series2 with data    

dataset.addSeries(series1);
dataset.addSeries(series2);

JFreeChart chart = ChartFactory.createXYLineChart(
    "title", "x-axis title", "y-axis title", dataset, PlotOrientation.VERTICAL, true, true, false
);
chart.getXYPlot().setRenderer(new XYSplineRenderer());

2 个答案:

答案 0 :(得分:9)

您可以手动创建JFreeChart对象,而不是使用ChartFactory。首先生成数据集和Plot,将每个数据集设置为索引。然后,您可以使用必要的轴和渲染器自定义绘图。以下是使用具有两个数据集的虚拟数据执行此操作的示例,每个数据集具有不同的y值:

    //create the series - add some dummy data
    XYSeries series1 = new XYSeries("series1");
    XYSeries series2 = new XYSeries("series2");
    series1.add(1000, 1000);
    series1.add(1150, 1150);
    series1.add(1250, 1250);

    series2.add(1000, 111250);
    series2.add(1150, 211250);
    series2.add(1250, 311250);

    //create the datasets
    XYSeriesCollection dataset1 = new XYSeriesCollection();
    XYSeriesCollection dataset2 = new XYSeriesCollection();
    dataset1.addSeries(series1);
    dataset2.addSeries(series2);

    //construct the plot
    XYPlot plot = new XYPlot();
    plot.setDataset(0, dataset1);
    plot.setDataset(1, dataset2);

    //customize the plot with renderers and axis
    plot.setRenderer(0, new XYSplineRenderer());//use default fill paint for first series
    XYSplineRenderer splinerenderer = new XYSplineRenderer();
    splinerenderer.setSeriesFillPaint(0, Color.BLUE);
    plot.setRenderer(1, splinerenderer);
    plot.setRangeAxis(0, new NumberAxis("Series 1"));
    plot.setRangeAxis(1, new NumberAxis("Series 2"));
    plot.setDomainAxis(new NumberAxis("X Axis"));

    //Map the data to the appropriate axis
    plot.mapDatasetToRangeAxis(0, 0);
    plot.mapDatasetToRangeAxis(1, 1);   

    //generate the chart
    JFreeChart chart = new JFreeChart("MyPlot", getFont(), plot, true);
    chart.setBackgroundPaint(Color.WHITE);
    JPanel chartPanel = new ChartPanel(chart);

答案 1 :(得分:0)

我已对上述解决方案进行了调整,以与最新版本的JFreeChart(2019年1月29日)一起使用。

如果正确导入了JFreeChart库,它将正确运行。我有pom.xml这样的导入设置

 <dependency>
        <groupId>org.jfree</groupId>
        <artifactId>jfreechart</artifactId>
        <version>1.0.19</version>
 </dependency>

这是完整的代码段:

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;

class GraphTest2 {
    // driver that actually runs the thing
    public static void main(String[] args) {
        SwingUtilities.invokeLater( () -> {
            LineChartEx ex = new LineChartEx();
            ex.setVisible(true);
        });
    }
}

class LineChartEx extends JFrame {
    public LineChartEx() {
        initUI();
    }

    private void initUI() {
        //create the series - add some dummy data
        XYSeries series1 = new XYSeries("series1");
        XYSeries series2 = new XYSeries("series2");
        series1.add(1000, 1000);
        series1.add(1150, 1150);
        series1.add(1250, 1250);

        series2.add(1000, 111250);
        series2.add(1150, 211250);
        series2.add(1250, 311250);

        //create the datasets
        XYSeriesCollection dataset1 = new XYSeriesCollection();
        XYSeriesCollection dataset2 = new XYSeriesCollection();
        dataset1.addSeries(series1);
        dataset2.addSeries(series2);

        //construct the plot
        XYPlot plot = new XYPlot();
        plot.setDataset(0, dataset1);
        plot.setDataset(1, dataset2);

        //customize the plot with renderers and axis
        plot.setRenderer(0, new XYSplineRenderer());//use default fill paint for first series
        XYSplineRenderer splinerenderer = new XYSplineRenderer();
        splinerenderer.setSeriesFillPaint(0, Color.BLUE);
        plot.setRenderer(1, splinerenderer);
        plot.setRangeAxis(0, new NumberAxis("Series 1"));
        plot.setRangeAxis(1, new NumberAxis("Series 2"));
        plot.setDomainAxis(new NumberAxis("X Axis"));

        //Map the data to the appropriate axis
        plot.mapDatasetToRangeAxis(0, 0);
        plot.mapDatasetToRangeAxis(1, 1);

        //generate the chart
        JFreeChart chart = new JFreeChart("MyPlot", getFont(), plot, true);
        chart.setBackgroundPaint(Color.WHITE);
        //JPanel jpanel = new ChartPanel(chart);


        // NEW PART THAT MAKES IT WORK
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        chartPanel.setBackground(Color.white);
        add(chartPanel);

        pack();
        setTitle("Line chart");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }