1

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

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

Code:

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

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?

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

Community
  • 1
  • 1
Mohan
  • 457
  • 6
  • 15

1 Answers1

1

I wanted it like your ThermometerDemo example.

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.

image

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;
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks.. as like this only i wanted. let me check whether it's suits for me.. thanks a lot – Mohan Nov 27 '13 at 15:34