We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

jfreechart

Kennywen
Kennywen asked
on
Medium Priority
1,448 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
Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
>> 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?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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).

Author

Commented:
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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
try:

chartPanel.add(button);

Author

Commented:
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

Author

Commented:
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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
to save a BufferedImage use ImageIO.write() method.

Author

Commented:
if i use another panel then how can i add it to setContentPane???

Author

Commented:
>> 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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
BufferedImage bi = chart.createBufferedImage(200, 100);

Author

Commented:
if i use another panel to group the button then how can i add it to setContentPane???
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
You add it to your component hierarchy wherever you need to using the add() method.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
same way you'd add any component

Author

Commented:
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);

Author

Commented:
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.
                                                         
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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

Author

Commented:
ok i get what u mean......

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

thanks.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Alternatively the ChartPanel is Printable so you can also print its contents.

Author

Commented:
is't i can print the chart like:
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel .createChartPrintJob();

thanks.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Good work :)  I hadn't seen that method, yes you can use that.

Author

Commented:
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

Author

Commented:
In what condition the chart will be refresh?

thanks
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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.

Author

Commented:
So , how can i display the chart when i change the dataset?

thanks
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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().

Author

Commented:
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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
yes that looks correct.

Author

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

thanks
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
setChart() is a method in ChartPanel, and you can call it with any JFreeChart instance.

Author

Commented:
thanks
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.