Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1401
  • Last Modified:

jfreechart

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
Kennywen
Asked:
Kennywen
  • 17
  • 17
1 Solution
 
objectsCommented:
1. add it to your ChartPanel
2. JFreeChart.createBufferedImage() method
3. Change the Dataset used by your Plot
0
 
KennywenAuthor 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?
0
 
objectsCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
KennywenAuthor 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
0
 
objectsCommented:
try:

chartPanel.add(button);
0
 
KennywenAuthor 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
0
 
KennywenAuthor 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
0
 
objectsCommented:
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
 
objectsCommented:
to save a BufferedImage use ImageIO.write() method.
0
 
KennywenAuthor Commented:
if i use another panel then how can i add it to setContentPane???

0
 
KennywenAuthor 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
0
 
objectsCommented:
BufferedImage bi = chart.createBufferedImage(200, 100);
0
 
KennywenAuthor Commented:
if i use another panel to group the button then how can i add it to setContentPane???
0
 
objectsCommented:
You add it to your component hierarchy wherever you need to using the add() method.
0
 
objectsCommented:
same way you'd add any component
0
 
KennywenAuthor 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);
0
 
KennywenAuthor 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.
                                                         
0
 
objectsCommented:
> 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
 
KennywenAuthor 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.
0
 
objectsCommented:
Alternatively the ChartPanel is Printable so you can also print its contents.
0
 
KennywenAuthor Commented:
is't i can print the chart like:
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel .createChartPrintJob();

thanks.
0
 
objectsCommented:
Good work :)  I hadn't seen that method, yes you can use that.
0
 
KennywenAuthor 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
0
 
KennywenAuthor Commented:
In what condition the chart will be refresh?

thanks
0
 
objectsCommented:
> 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
 
KennywenAuthor Commented:
So , how can i display the chart when i change the dataset?

thanks
0
 
objectsCommented:
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
 
KennywenAuthor 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
0
 
objectsCommented:
yes that looks correct.
0
 
KennywenAuthor Commented:
that mean the setChart() can be use in any other chart E.g. line and pie chart?

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 17
  • 17
Tackle projects and never again get stuck behind a technical roadblock.
Join Now