[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 750
  • Last Modified:

How to show a XY Chart using JFreeChart ??

Hi guys,

   I got the following code :

import java.awt.*;
import java.awt.event.*;
import java.io.PrintStream;
import java.util.ResourceBundle;
import java.util.Vector;
import javax.swing.*;

import org.jfree.chart.JFreeChart;
import org.jfree.data.XYSeries;
import org.jfree.data.XYSeriesCollection;
import org.jfree.data.XYDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;

public class JChartXY implements ActionListener, WindowListener
{

    public static void main(String[] args) {
      
      XYSeries series = new XYSeries("Average Size");
      series.add(20.0, 10.0);
      series.add(40.0, 20.0);
      series.add(70.0, 50.0);
      XYDataset xyDataset = new XYSeriesCollection(series);
      
      JFreeChart chart = ChartFactory.createXYAreaChart
                           ("Sample XY Chart",  // Title
                            "Height",           // X-Axis label
                            "Weight",           // Y-Axis label
                            xyDataset,          // Dataset
                            PlotOrientation.HORIZONTAL,
                            true,                // Show legend
                            false,
                            false
                           );
                     }
}

it compiled successfully but nothing show up ! So how to make the XY Chart appear ? Thanks !
0
northborneo
Asked:
northborneo
1 Solution
 
northborneoAuthor Commented:
guys .... pls help. ...pls ..pls
0
 
jimmackCommented:
It looks like you've created the chart OK, but you haven't displayed it.  Have a look in the documentation (there should be some examples).  Do you need to add the "chart" object to a JFrame or similar?

OK.  Got it.

Add the following:

import org.jfree.chart.ChartPanel;

For testing, I changed the class definition to:

public class JChartXY  // ie. removed the implements

Then added the following code at the end of your main method:

        JFrame mainFrame = new JFrame("Chart test");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        ChartPanel mainPanel = new ChartPanel(chart);
       
        mainFrame.getContentPane().add(mainPanel);
       
        mainFrame.setSize(new Dimension(600, 400));
        mainFrame.show();
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now