0

I am trying to align the whole chart within a JFrame but the normal component codes that I know do not work for JChartPanels. Initially I tried to setAligmentX and Y on the JChartPanel but that didn't work. I then tried to add the JChartPanel to a JPanel then setAlignment but once I add the JChartPanel to a JPanel the graph is no longer visible.

The code below creates a graph within a JFrame at the default top left location. I need to align the graph- use any values as I will change them later to fit my purpose.

The contained code without any of the attempts(errors) mentioned above:

import java.awt.*;
import javax.swing.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ChartTest extends javax.swing.JFrame {

public ChartTest() {

    XYSeries Goals = new XYSeries("Goals Scored");
    Goals.add(1, 1.0);
    Goals.add(2, 3.0);
    Goals.add(3, 2.0);
    Goals.add(4, 0.0);
    Goals.add(5, 3.0);
    XYDataset xyDataset = new XYSeriesCollection(Goals);

    JFreeChart chart = ChartFactory.createXYLineChart("Goals Scored Over Time", "Fixture Number", "Goals", xyDataset, PlotOrientation.VERTICAL, true, true, false);

    JPanel jPanel = new JPanel();
    jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.PAGE_AXIS));
    jPanel.setVisible(true);
    jPanel.setSize(300, 300);

    ChartPanel CP = new ChartPanel(chart) {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    };
    CP.setMouseWheelEnabled(true);
    add(CP);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    initComponents();
}

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ChartTest().setVisible(true);
        }
    });
}
  • You may need to change the layout of the enclosing panel, for [example](http://stackoverflow.com/a/5630271/230513). – trashgod Feb 15 '14 at 19:46
  • I don't understand. I changed the Layout of the "parent" jPanel by using `new FlowLayout(FlowLayout.RIGHT, 3,3)` then added the ChartPanel using `jPanel.add(CP);` and the result is just an empty window... –  Feb 16 '14 at 17:14

1 Answers1

2

The result is just an empty window.

I suspect that your GUI editor's implementation of initComponents() is at fault. You'll have to examine the generated code to see. Here's a complete working example form for reference:

image

Minimal, Complete, Tested and Readable Example:

import java.awt.Dimension;
import javax.swing.BoxLayout;
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.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ChartTest extends javax.swing.JFrame {

    public ChartTest() {

        XYSeries Goals = new XYSeries("Goals Scored");
        Goals.add(1, 1.0);
        Goals.add(2, 3.0);
        Goals.add(3, 2.0);
        Goals.add(4, 0.0);
        Goals.add(5, 3.0);
        XYDataset xyDataset = new XYSeriesCollection(Goals);

        JFreeChart chart = ChartFactory.createXYLineChart(
            "Goals Scored Over Time", "Fixture Number", "Goals",
            xyDataset, PlotOrientation.VERTICAL, true, true, false);

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.PAGE_AXIS));
        jPanel.setVisible(true);
        jPanel.setSize(300, 300);

        ChartPanel CP = new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        };
        CP.setMouseWheelEnabled(true);
        add(CP);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ChartTest().setVisible(true);
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045