0

I just want to insert a chart (created by Graphique class) into a panel from another class ("TabFrame" here). I am using NetBeans. So, I created a class Graphique as you can see:

public class Graphique extends JPanel {
        public Graphique(double cpm1, int temps1, double cpm2,
        double temps2, double cpm3, double temps3, double cpm4,
        double temps4) {
    final XYDataset dataset = createDataset(cpm1, temps1,
            cpm2, temps2, cpm3, temps4, cpm4,
            temps4);
    final JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    add(chartPanel);

    final XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.lightGray);

    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);

    final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(1, false);
    renderer.setSeriesShapesVisible(1, false);
    plot.setRenderer(renderer);

}

public XYDataset createDataset(double cpm1, int temps1, double cpm2,
        double temps2, double cpm3, double temps3, double cpm4,
        double temps4) {
    final XYSeries series1 = new XYSeries("log cpm net");
    series1.add(temps1, cpm1);
    series1.add(temps2, cpm2);
    series1.add(temps3, cpm3);
    series1.add(temps4, cpm4);
    final XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series1);
    return dataset;

}

private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart chart = ChartFactory.createXYLineChart("log Chart", // chart
                                                                            // title
            "temps", // x axis label
            "log CPM", // y axis label
            dataset, // data
            PlotOrientation.VERTICAL, true, // include legend
            true, // tooltips
            false // urls
            );
    return chart;

    }

}

The next step is to integrate the chart the Graphique class creates into a panel (already declared thanks to the netbeans' GUI interface) in another class named here TabFrame:

  JPanel cp = new Graphique(Math.log(CMP1corBis), tempsCumule1, Math.log(CMP2corBis),
          tempsCumule2,Math.log(CMP3corBis), tempsCumule3,Math.log(CMP4corBis), tempsCumule4);
  PanelGraphique1.add(cp);

Why isn't this working?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
nad
  • 5
  • 4

2 Answers2

1

You didn't specify a LayoutManager for the Graphique panel, so when you add the ChartPanel as a child component it most likely doesn't get positioned or sized, so you won't see any chart.

David Gilbert
  • 4,427
  • 14
  • 22
  • 1
    The default layout of `JPanel` is `FlowLayout`; more [here](http://stackoverflow.com/a/25047904/230513). – trashgod Jul 30 '14 at 22:18
  • Yes, you are correct (it seems that JavaScript has dulled my brain). The most likely issue is that JFreeChart's ChartPanel class does not have a preferred size, so it needs a layout manager that will position and size it (for example, BorderLayout). In your example code, you provide the size directly by overriding getPreferredSize(). I should probably change the JFreeChart code to set some default size for the ChartPanel to avoid this even happening. – David Gilbert Jul 31 '14 at 05:48
  • 1
    The `ChartPanel` constructor invokes `setPreferredSize()` based on reasonable defaults; I'm not sure I'd advocate changing anything; more [here](http://stackoverflow.com/a/10277372/230513). – trashgod Jul 31 '14 at 06:10
  • You are correct again. Your support on the JFreeChart (and other) questions here is amazing! – David Gilbert Jul 31 '14 at 07:55
  • Yes, I'm a fan! Critical comments welcomed. At the risk of seeming importunate, I'm nonplussed by this [question](http://stackoverflow.com/q/24939833/230513), also seen [here](http://stackoverflow.com/a/18421887/230513). – trashgod Jul 31 '14 at 15:11
  • I added a comment (essentially "I don't know how to make it work"). – David Gilbert Aug 01 '14 at 13:38
1

Using substitute data, your chart appears as shown below; the problem may be in the parameters you supply or neglecting to pack() the enclosing Window. As an aside, consider overriding getPreferredSize() for the reasons discussed here.

image

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/a/25047904/230513
 */
public class Test {

    private class Graphique extends JPanel {

        public Graphique() {
            final XYDataset dataset = createDataset();
            final JFreeChart chart = createChart(dataset);
            final ChartPanel chartPanel = new ChartPanel(chart){

                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(500, 270);
                }
            };
            add(chartPanel);

            final XYPlot plot = chart.getXYPlot();
            plot.setBackgroundPaint(Color.lightGray);

            plot.setDomainGridlinePaint(Color.white);
            plot.setRangeGridlinePaint(Color.white);

            final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
            renderer.setSeriesLinesVisible(1, false);
            renderer.setSeriesShapesVisible(1, false);
            plot.setRenderer(renderer);
        }

        private XYDataset createDataset() {
            final XYSeries series1 = new XYSeries("log cpm net");
            series1.add(1, 10);
            series1.add(2, 20);
            series1.add(3, 30);
            series1.add(4, 40);
            final XYSeriesCollection dataset = new XYSeriesCollection();
            dataset.addSeries(series1);
            return dataset;
        }

        private JFreeChart createChart(final XYDataset dataset) {
            final JFreeChart chart = ChartFactory.createXYLineChart("log Chart",
                    "temps", // x axis label
                    "log CPM", // y axis label
                    dataset, // data
                    PlotOrientation.VERTICAL, true, // include legend
                    true, // tooltips
                    false // urls
            );
            return chart;
        }

    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Graphique());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new Test().display();
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • On reflection, `PanelGraphique1` may be obscured or replaced by another panel. Let me know if this stand-alone example works as expected. – trashgod Jul 31 '14 at 15:14