Save Chart as png

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
JavaGui
Posts: 4
Joined: Wed Jun 08, 2016 6:50 am
antibot: No, of course not.

Save Chart as png

Post by JavaGui » Wed Jun 08, 2016 7:11 am

Hi !
I am novice for java & I have tried to search this forum for my problem, but could not find solution.

After learning few days, I have created one program. Where I have create one chart with help of JFree chart.

I have add one button named "Save Chart" & create one event listener of button.

When user will press Save Chart button then image will save.

My event listener function working properly, but I am unable to save chart.

As to sort out error, I find in event listener, I have write code for save image.

In this function one para chart is creating problem. I am writing in eclipse, so I can show error.

Line which is creating problem in code it is below.

Code: Select all

ChartUtilities.saveChartAsPNG(new File(" c:\\chart.png"), chart, 600, 300 );
Here Is my complete program.

Code: Select all



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
 
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.TickUnits;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
 
public class mySerial extends JFrame {
 
	private JButton button;
	private JButton button1;
	private JButton button2;
	private JComboBox jc;
	private JLabel  jl;
 
 
	private JPanel jp;
	public mySerial(){
 
		setLayout(new FlowLayout());
 
 
		jp = new JPanel();
		add(jp);
 
		jl = new JLabel("Select Port");
		add(jl);
 
		jc = new JComboBox<String>();
		add(jc);
 
		button = new JButton(" Connect1");
		button.setBounds(20, 20, 100, 30);
		add(button);
 
		button1 = new JButton(" Connect2");
		button1.setBounds(140, 20, 100, 30);
		add(button1);
 
 
		graph();
 
		button2 = new JButton("Save Chart");
		button2.setBounds(20, 550, 100, 30);
		add(button2);
 
		event e = new event();
		button2.addActionListener(e);
 
	}
 
 
 
	public void graph() {
	XYSeries series = new XYSeries("Sensor Reading ");
	XYSeriesCollection dataset = new XYSeriesCollection(series);
	JFreeChart chart = ChartFactory.createXYLineChart("Preassure Graph", "Time(Seconds) ","Preassure Reading", dataset);
	chart.setBackgroundPaint(null);
	chart.setBorderVisible(true);
	add(new ChartPanel(chart),BorderLayout.CENTER);	
	}
 
	public class event implements ActionListener{
		public void actionPerformed(ActionEvent e){
		System.out.println(" Button Pressed");
		try {
			ChartUtilities.saveChartAsPNG(new File(" c:\\chart.png"), chart, 600, 300 ); 
			} catch (IOException e1) {
			System.err.println("Problem occurred creating chart.");
			}
		}
		}
 
 
 
	public static void main(String args[]){
		mySerial gui = new mySerial();
		gui.setBounds(0, 0, 700, 600);
		gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
		//gui.setSize(1000, 550);
		gui.setTitle(" Serial Port" );
		gui.setVisible(true);
 
 
	}
}
need help to sort problem.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Save Chart as png

Post by paradoxoff » Wed Jun 08, 2016 8:34 pm

The variable "chart" is not recognized in your event class. "chart" only exists as a local variable in your "graph" method.
Solution: let your main class implement ActionListener, implement the actionPerformed-method in your mainclass, and make "chart" an instance variable of your main class.

JavaGui
Posts: 4
Joined: Wed Jun 08, 2016 6:50 am
antibot: No, of course not.

Re: Save Chart as png

Post by JavaGui » Thu Jun 09, 2016 7:03 am

Sir,

as per your instruction I have declare chart as

Code: Select all

static public JFreeChart chart;
now chart does not show any error.


according to your instruction

your main class implement ActionListener, implement the actionPerformed-method in your mainclass.

as you know I am novice for java & I am trying to change according to your instruction & it will take time.

If any other solution is available then pls. help.

Thanks

Locked