Solved

jfreechart

Posted on 2004-04-05
34
1,384 Views
Last Modified: 2008-03-06
Do anyone know how to perform those tasks as below:
1. Add checkbox and/or command button to the chart. (Also add the action listener for the button and checkbox)
2. Print the chart and export the chart to a picture file.
3. How to refresh the chart (E.g. There is 3 button in the chart once i press the january button and it will refresh the chart and so on)

thanks in advance
0
Comment
Question by:Kennywen
  • 17
  • 17
34 Comments
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 10763466
1. add it to your ChartPanel
2. JFreeChart.createBufferedImage() method
3. Change the Dataset used by your Plot
0
 

Author Comment

by:Kennywen
ID: 10763499
>> add it to your ChartPanel
add the code in ChartPanel class or in the main program?

>>JFreeChart.createBufferedImage() method
this line will print the chart to printer???

>>Change the Dataset used by your Plot
should i clear the current chart first before putting the new dataset?
0
 
LVL 92

Expert Comment

by:objects
ID: 10763514
1. add your button to the ChartPanel

2. No it creates a BufferedImage which you can then do whatever you need to do

3. No, you can simply set the dataset (if thats all that is changing).
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Kennywen
ID: 10763554
below is my sample program:

      public popUp1()
      {
            jan = new JButton("January");
            jan.setActionCommand("jan");
            jan.addActionListener(this);
            feb = new JButton("February");
            feb.setActionCommand("feb");
            feb.addActionListener(this);
            add(jan);
            add(feb);
      }

      public void actionPerformed(ActionEvent e)
      {
            if ("jan".equals(e.getActionCommand()))
            {
            System.out.println("jan");
        }
            else if ("feb".equals(e.getActionCommand()))
            {
                  System.out.println("feb");
            }
    }

how can i add in the above button to ChartPanel?

sample demo program from jfreechart:
 public BarChartDemo(String title) {

        super(title);

        CategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(1000, 270));
        setContentPane(chartPanel);

    }

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10763574
try:

chartPanel.add(button);
0
 

Author Comment

by:Kennywen
ID: 10763614
yes, it can add the button to the chartPanel but now problem is the button is overwrite the chart... how can make the chart to be smaller then my button can put on the bottom part of the chart...

thanks
0
 

Author Comment

by:Kennywen
ID: 10763623
actually what can i do with BufferedImage ? can i print it and export it to image format?

Can you show me some sample code on createBufferedImage() in jfreechart and how to print and export it?

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10763628
If you don't want your button to overwrite the chart then don't add it to the ChartPanel and instead add it another panel.
0
 
LVL 92

Expert Comment

by:objects
ID: 10763631
to save a BufferedImage use ImageIO.write() method.
0
 

Author Comment

by:Kennywen
ID: 10763655
if i use another panel then how can i add it to setContentPane???

0
 

Author Comment

by:Kennywen
ID: 10763658
>> to save a BufferedImage use ImageIO.write() method.
but i don't know how to createBufferedImage ??

where should i write JFreeChart.createBufferedImage()??? so i need to create a variable to assign it to JFreeChart.createBufferedImage()???

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10763668
BufferedImage bi = chart.createBufferedImage(200, 100);
0
 

Author Comment

by:Kennywen
ID: 10763721
if i use another panel to group the button then how can i add it to setContentPane???
0
 
LVL 92

Expert Comment

by:objects
ID: 10763730
You add it to your component hierarchy wherever you need to using the add() method.
0
 
LVL 92

Expert Comment

by:objects
ID: 10763732
same way you'd add any component
0
 

Author Comment

by:Kennywen
ID: 10763769
Sorry, i can't get what you mean.

sample program as below:
CategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
            myImage = chart.createBufferedImage(1000,270);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(1000, 270));
            jan = new JButton("January");
            jan.setActionCommand("jan");
            jan.addActionListener(this);
            feb = new JButton("February");
            feb.setActionCommand("feb");
            feb.addActionListener(this);
            chartPanel.add(jan);
            chartPanel.add(feb);
        setContentPane(chartPanel);
0
 

Author Comment

by:Kennywen
ID: 10763780
another problem when i want to save the bufferedimage to a file it prompt me an error:

ImageIO.write(myImage, "png", new FileOuputStream(".png"));

Error:
symbol  : class FileOuputStream
location: class BarChartDemo ImageIO.write(myImage, "png", new FileOuputStream("1321.png"));
                                                       
i already import java.io.FileOutputStream; and import javax.imageio.*;

thanks.
                                                         
0
 
LVL 92

Expert Comment

by:objects
ID: 10763800
> setContentPane(chartPanel);

Sorry, didn't know you were using your chart as the content pane.
You can either use a different panel as your content pane and add your chart (plus anything else you need) to it.
or add your panels to the existing content pane:
getContentPane().add(...

> FileOuputStream

misspelt, should be:

FileOutputStream
0
 

Author Comment

by:Kennywen
ID: 10763843
ok i get what u mean......

now i'm able to save the file but how can i print it directly to printer?

thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 10763873
0
 
LVL 92

Expert Comment

by:objects
ID: 10763882
Alternatively the ChartPanel is Printable so you can also print its contents.
0
 

Author Comment

by:Kennywen
ID: 10763902
is't i can print the chart like:
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel .createChartPrintJob();

thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 10763952
Good work :)  I hadn't seen that method, yes you can use that.
0
 

Author Comment

by:Kennywen
ID: 10764083
for the question number 2:
at first i write:
dataset = createDataset();
JFreeChart chart = createChart(dataset);

 private CategoryDataset createDataset() {
       
        // row keys...
        String series1 = "First";
        String series2 = "Second";
        String series3 = "Third";

        // column keys...
        String category1 = "Category 1";
        String category2 = "Category 2";
        String category3 = "Category 3";
        String category4 = "Category 4";
        String category5 = "Category 5";

        // create the dataset...
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(1.0, series1, category1);
        dataset.addValue(4.0, series1, category2);
        dataset.addValue(3.0, series1, category3);
        dataset.addValue(5.0, series1, category4);
        dataset.addValue(5.0, series1, category5);

        dataset.addValue(5.0, series2, category1);
        dataset.addValue(7.0, series2, category2);
        dataset.addValue(6.0, series2, category3);
        dataset.addValue(8.0, series2, category4);
        dataset.addValue(4.0, series2, category5);
       
        return dataset;
       
    }


then on the command button actionPerformed:
dataset = createDataset1();
chart = createChart(dataset);

private CategoryDataset createDataset1() {
       
        // row keys...
        String series1 = "First";
        String series2 = "Second";
        String series3 = "Third";

        // column keys...
        String category1 = "Category 1";
        String category2 = "Category 2";
        String category3 = "Category 3";
        String category4 = "Category 4";
        String category5 = "Category 5";

        // create the dataset...
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(1.0, series1, category1);
        dataset.addValue(4.0, series1, category2);
        dataset.addValue(3.0, series1, category3);
        dataset.addValue(5.0, series1, category4);
        dataset.addValue(5.0, series1, category5);
       
        return dataset;
       
    }


but the graph still no refresh when i click on the command button. why?

thanks
0
 

Author Comment

by:Kennywen
ID: 10764383
In what condition the chart will be refresh?

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10770822
> but the graph still no refresh when i click on the command button. why?

You create a new chart (when u call createChart()) , which you never display.
0
 

Author Comment

by:Kennywen
ID: 10771065
So , how can i display the chart when i change the dataset?

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10771093
If you are creating a new Chart (as opposed to changing the data displayed in existing chart) then you will need to change the chart being displayed by your ChartPanel using setChart().
0
 

Author Comment

by:Kennywen
ID: 10771232
private JFreeChart chart;
private ChartPanel chartPanel;
private CategoryDataset dataset;
private DefaultCategoryDataset dataset1;

That mean first i should set the dataset:
dataset = createDataset1();
           private CategoryDataset createDataset1() {
       
        String series1 = "First";
        String series2 = "Second";
        String series3 = "Third";

        String category1 = "Category 1";
        String category2 = "Category 2";
        String category3 = "Category 3";
        String category4 = "Category 4";
        String category5 = "Category 5";

       dataset1 = new DefaultCategoryDataset();
        dataset1.addValue(1.0, series1, category1);
        dataset1.addValue(4.0, series1, category2);
        dataset1.addValue(3.0, series1, category3);
        dataset1.addValue(5.0, series1, category4);
        dataset1.addValue(5.0, series1, category5);
       
        return dataset1;      
    }

2nd, create the chart
chart = createChart(dataset);

3rd, change the chart:
chartPanel.setChart(chart);

is't above steps is correct?

if the above steps are correct then the above steps can apply to all the other chart like: line and pie chart?

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10771244
yes that looks correct.
0
 

Author Comment

by:Kennywen
ID: 10771275
that mean the setChart() can be use in any other chart E.g. line and pie chart?

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10771291
setChart() is a method in ChartPanel, and you can call it with any JFreeChart instance.
0
 

Author Comment

by:Kennywen
ID: 10771312
thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10771347
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
nextBoolean(double p) for Random class 3 41
hibernate insert example 13 34
jboss wildfly 10.1 10 218
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

791 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