Gráficos horizontales JFreeChart

Estoy usando 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

Código:

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, pero aquí estoy usando JFrame.

preguntado el 27 de noviembre de 13 a las 05:11

can anyone help please.. -

Edite su pregunta para incluir un sscce, Para ejemplo. -

Hi, i don't have the rights to display the screen shot, so only added that two contents. -

Falto two charts into single row sounds like you may want CombinedRangeXYPlot. -

Actually i wanted like ThermaMeterDemo your example. But for that you have used JPanel, but here i'm using JFrame. can you guide me? -

1 Respuestas

I wanted it like your ThermometerDemo ejemplo.

Basado en esto ejemplo, the code below adds two panels to a GridLayout(1, 0). Each panel includes it's own chart and control panel.

imagen

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

contestado el 23 de mayo de 17 a las 13:05

Thanks.. as like this only i wanted. let me check whether it's suits for me.. thanks a lot - Mohan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.