Solved

jfreechart

Posted on 2004-04-05
34
1,390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

739 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