Solved

How to show a XY Chart using JFreeChart ??

Posted on 2003-11-06
2
716 Views
Last Modified: 2008-03-17
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
Comment
Question by:northborneo
2 Comments
 

Author Comment

by:northborneo
ID: 9699657
guys .... pls help. ...pls ..pls
0
 
LVL 15

Accepted Solution

by:
jimmack earned 125 total points
ID: 9700052
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 72
Where to store the queries for modification of table 4 61
Weekend adv creator 3 37
oracle 11g 23 78
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question