Solved

How to show a XY Chart using JFreeChart ??

Posted on 2003-11-06
2
709 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi2 challenge 7 44
strCount chalenge 3 50
wordcount challenge 11 76
how do i compare an object based on two fields 6 32
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now