JFreeChart水平图 [英] JFreeChart Horizontal Charts

查看:66
本文介绍了JFreeChart水平图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CombinedDomainXYPlot绘制图表.我还有另一个要求,我需要水平显示两个图表.

I'm using CombinedDomainXYPlot to plot the charts. I have another requirement where, I need to show the two charts horizontally.

目前,我只有一张图表.我需要的是,第一行需要两个图表. 如图表1图表2

Currently i'm having only one chart. what i need is, i need two charts in the first row. like Chart1 Chart2

代码:

CombinedDomainXYPlot plot = new CombinedDomainXYPlot();
plot.add(getChart1(), 2);
plot.add(getChart2(), 2);

第一行只给出一个图表.另一排中的第二个chart2.

It is giving only one chart in the first row. and second chart2 in the another row.

有什么办法可以将这两张图表做成一行?

Is there any way I can make these two charts into single row?

实际上,我希望它像您的 ThermometerDemo 示例一样.为此,您使用了JPanel,但是在这里我使用的是JFrame.

Actually I wanted it like your ThermometerDemo example. For that you have used JPanel, but here I'm using JFrame.

推荐答案

我希望它像您的 ThermometerDemo 示例.

I wanted it like your ThermometerDemo example.

基于此示例,以下代码将两个面板添加到GridLayout(1, 0).每个面板都包含自己的图表和控制面板.

Based on this example, the code below adds two panels to a GridLayout(1, 0). Each panel includes it's own chart and control panel.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/a/20243624/230513
 * @see https://stackoverflow.com/q/11870416/230513
 */
public class CombinedPlot {

    private static final int MAX = 3;
    private static final Random rand = new Random();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                init();
            }
        });
    }

    private static void init() {
        JFrame frame = new JFrame("Combined Plot Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(1, 0));
        frame.add(createPanel());
        frame.add(createPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static JPanel createPanel() {
        JPanel p = new JPanel(new BorderLayout());
        XYItemRenderer renderer = new StandardXYItemRenderer();
        XYPlot plot1 = new XYPlot(
            generateData(), null, new NumberAxis("Range 1"), renderer);
        XYPlot plot2 = new XYPlot(
            generateData(), null, new NumberAxis("Range 2"), renderer);
        final CombinedDomainXYPlot plot
            = new CombinedDomainXYPlot(new NumberAxis("Domain"));
        plot.add(plot1);
        plot.add(plot2);
        plot.setOrientation(PlotOrientation.VERTICAL);
        JFreeChart chart = new JFreeChart(
            "Combined Plots", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
        ChartPanel chartPanel = new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 320);
            }
        };

        JPanel controlPanel = new JPanel();
        controlPanel.add(new JButton(new UpdateAction(plot, 0)));
        controlPanel.add(new JButton(new UpdateAction(plot, 1)));
        p.add(chartPanel, BorderLayout.CENTER);
        p.add(controlPanel, BorderLayout.SOUTH);
        return p;
    }

    private static class UpdateAction extends AbstractAction {

        private final XYPlot plot;

        public UpdateAction(CombinedDomainXYPlot plot, int i) {
            super("Update plot " + (i + 1));
            this.plot = (XYPlot) plot.getSubplots().get(i);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            plot.setDataset(CombinedPlot.generateData());
        }
    }

    private static XYSeriesCollection generateData() {
        XYSeriesCollection data = new XYSeriesCollection();
        for (int i = 0; i < MAX; i++) {
            data.addSeries(generateSeries("Series " + (i + 1)));
        }
        return data;
    }

    private static XYSeries generateSeries(String key) {
        XYSeries series = new XYSeries(key);
        for (int i = 0; i < 16; i++) {
            series.add(rand.nextGaussian(), rand.nextGaussian());
        }
        return series;
    }
}

这篇关于JFreeChart水平图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆