Menu

Save Chart as Image File

Masta Suvi

Charts can be saved as jpg or png images.

This is the codeblock for saving a chart to an image.
An IOExpetion must be catched in case the storage device is full or write protected.

//saving the chart as jpg and png in the current working directory
try {
chart.saveAsJPEG("testchart.jpg", 640, 480);
chart.saveAsPNG("testchart.png", 640, 480);
} catch (IOException ex) {
Logger.getLogger(JChartLibApp_SaveAsJpg.class.getName()).log(Level.SEVERE, null, ex);
}

Sourcecode sample application:
/**

  • JChartLib Demo App for saving a chart to a file
  • @author Silvio Schneider
    */
    import com.bitagentur.chart.JChartLibBaseChart;
    import com.bitagentur.data.JChartLibDataSet;

import com.bitagentur.data.JChartLibSerie;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**

  • A simple demonstration application showing how to create a line chart.
    *
    */
    public class JChartLibApp_SaveAsJpg {

    /**

    • Creates a new Application Frame
      */
      public JChartLibApp_SaveAsJpg() {
      //Some data for the chart
      JChartLibSerie values = new JChartLibSerie("Some Data");
      values.addValue(1);
      values.addValue(4);
      values.addValue(2);
      //Adding the serie to the dataset
      JChartLibDataSet dataset = new JChartLibDataSet();
      dataset.addDataSerie(values);
      //the chart itself
      final JChartLibBaseChart chart = new JChartLibBaseChart(
      "Title", // chart title
      "X-Axis", // x axis text
      "Y-Axis", // y axis text
      dataset, // data
      true // legend on
      );
      //saving the chart as jpg and png in the current working directory
      try {
      chart.saveAsJPEG("testchart.jpg", 640, 480);
      chart.saveAsPNG("testchart.png", 640, 480);
      } catch (IOException ex) {
      Logger.getLogger(JChartLibApp_SaveAsJpg.class.getName()).log(Level.SEVERE, null, ex);
      }
      }

    /**

    • DEMO Application for JChartLib
      *
    • @param args the command line arguments
      */
      public static void main(final String[] args) {
      System.out.println("JChartLibApp started");
      final JChartLibApp_SaveAsJpg app = new JChartLibApp_SaveAsJpg();
      }
      }

MongoDB Logo MongoDB