Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:05:40+00:00 2026-06-14T00:05:40+00:00

I am using JFreeChart to populate a graph from a csv file, i have

  • 0

I am using JFreeChart to populate a graph from a csv file, i have managed to create multiple datasets from the file and graphed the data.

The SVG images are being re-written to the “file.svg”, so nothing looks pretty at all. Question is, how can i create a single SVG image from the multiple datasets and If only i knew how to get each chart, plot it orderly and draw all of them to a SVG file.

package freechart;  
import java.util.*;
import java.text.*;
import java.io.*;
import java.awt.*;
import java.util.StringTokenizer;
import javax.swing.*;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.time.*;
import org.jfree.ui.*;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
  public class StockHistoryChart extends JPanel 
   {

private static final long serialVersionUID = 1L;

// Holds the data
    public TimeSeriesCollection dataset = new TimeSeriesCollection();
    public TimeSeriesCollection datasetPhysical = new TimeSeriesCollection();
    public TimeSeriesCollection datasetS = new TimeSeriesCollection();
    public TimeSeriesCollection datasetMM = new TimeSeriesCollection();
    public TimeSeriesCollection datasetIF = new TimeSeriesCollection();
    public TimeSeriesCollection datasetWP = new TimeSeriesCollection();

          // Create a chart
                  @SuppressWarnings("unused")
    private JFreeChart chart;

          // Create a panels that can show our chart
                 @SuppressWarnings("unused")
    private ChartPanel panel;

    private String stockSymbol;

    private String CurrentMonth;

    @SuppressWarnings({ "deprecation", "resource", "unused" })
   public StockHistoryChart( String filename )
   {
try
{
  // Get Stock Symbol
  this.stockSymbol = filename.substring( 0, filename.indexOf( '.' ) );

  // Create time series
  TimeSeries PVC = new TimeSeries( "Physical Count", Day.class );
  //TimeSeries PV = new TimeSeries( "Physical", Day.class );
  TimeSeries STKC = new TimeSeries( "S Count", Day.class );
  //TimeSeries STKTP = new TimeSeries( "S", Day.class );
  TimeSeries MMTPC = new TimeSeries( "MM Count", Day.class );
  //TimeSeries MMTP = new TimeSeries( "MM", Day.class );
  TimeSeries IFSC = new TimeSeries( "IF Count", Day.class );
  //TimeSeries IFSTP = new TimeSeries( "IF", Day.class );
  TimeSeries WPC = new TimeSeries( "WP Count", Day.class );
  //TimeSeries WPTP = new TimeSeries( "WP", Day.class );



  BufferedReader br = new BufferedReader( new FileReader( filename ) );
  String key = br.readLine();
  String line = br.readLine();
  while( line != null &&
      !line.startsWith( "<!--" ) )
  {

    StringTokenizer st = new StringTokenizer( line, ",", false );
    Day day = getDay( st.nextToken() );
    double PVCValue = Double.parseDouble( st.nextToken() );
    double PVValue = Double.parseDouble( st.nextToken() );
    double STKCValue = Double.parseDouble( st.nextToken() );
    double STKTPValue = Double.parseDouble( st.nextToken() );
    double MMTPCValue = Double.parseDouble( st.nextToken() );
    double MMTPValue = Double.parseDouble( st.nextToken() );
    double IFSCValue = Double.parseDouble( st.nextToken() );
    double IFSTPValue = Double.parseDouble( st.nextToken() );
    double WPCValue = Double.parseDouble( st.nextToken() );
    double WPTPValue = Double.parseDouble( st.nextToken() );
    double TTCValue = Double.parseDouble( st.nextToken() );
    double TTAValue = Double.parseDouble( st.nextToken() );


    // Add this value to our series'
    PVC.addOrUpdate( day, PVCValue );
    //PV.addOrUpdate( day, PVValue );
    STKC.addOrUpdate( day, STKCValue );
    //STKTP.addOrUpdate( day, STKTPValue );
    MMTPC.addOrUpdate( day, MMTPCValue );
    //MMTP.addOrUpdate( day, MMTPValue );
    IFSC.addOrUpdate( day, IFSCValue );
    //IFSTP.addOrUpdate( day, IFSTPValue );
    WPC.addOrUpdate( day, WPCValue );
    //WPTP.addOrUpdate( day, WPTPValue );
    TTC.addOrUpdate( day, TTCValue );
    //TTA.addOrUpdate( day, TTAValue );


    // Read the next day
    line = br.readLine();
  }

  // Build the datasets
  dataset.addSeries( PVC );
  //dataset.addSeries( PV );
  dataset.addSeries( STKC );
  //dataset.addSeries( STKTP );
  dataset.addSeries(MMTPC);
  //dataset.addSeries(MMTP);
  dataset.addSeries(IFSC);
  //dataset.addSeries(IFSTP);
  dataset.addSeries(WPC);
  //dataset.addSeries(WPTP);
  dataset.addSeries(TTC);
  //dataset.addSeries(TTA);

  datasetPhysical.addSeries(PVC);
  //datasetPhysical.addSeries(PV);
  datasetS.addSeries(STKC);
  //datasetS.addSeries(STKTP);
  datasetMM.addSeries(MMTPC);
  //datasetMM.addSeries(MMTP);
  datasetIF.addSeries(IFSC);
  //datasetIF.addSeries(IFSTP);
  datasetWP.addSeries(WPC);
  //datasetWP.addSeries(WPTP);


  dataset.setDomainIsPointsInTime(true);
 //datasetPhysical.setDomainIsPointsInTime(true);
  datasetS.setDomainIsPointsInTime(true);
  //datasetS.setDomainIsPointsInTime(true);
  datasetMM.setDomainIsPointsInTime(true);
  //datasetMM.setDomainIsPointsInTime(true);
  datasetIF.setDomainIsPointsInTime(true);
  //datasetIF.setDomainIsPointsInTime(true);
  datasetWP.setDomainIsPointsInTime(true);
  //datasetWP.setDomainIsPointsInTime(true);

    JFreeChart summary = buildChart( dataset, "Figures Summary Chart", true );
  JFreeChart Physical = buildChart( datasetPhysicalVouchers, "Physical Vouchers count", false );
  JFreeChart S = buildChart( datasetSTK, "S count", false );
  JFreeChart MM = buildChart( datasetMM, "MM count", true );
  JFreeChart IF = buildChart( datasetIFS, "IF (count & values)", false );
  JFreeChart WP = buildChart( datasetWebPortal, "WP count", true );

  // Create this panel
  this.setLayout( new GridLayout( 2, 2 ) );
  this.add( new ChartPanel( summary ) );
  this.add( new ChartPanel( Physical ) );
  this.add( new ChartPanel( S ) );
  this.add( new ChartPanel( MM ) );
  this.add( new ChartPanel( IF ) );
  this.add( new ChartPanel( WP ) );


}
catch( Exception e )
{
  e.printStackTrace();
} }    
private JFreeChart buildChart( TimeSeriesCollection dataset, String title, boolean endPoints )
  {
    // Create the chart
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
      title,
      "Date", "Totals",
      dataset,
      true,
      true,
      false
    );

    // Setup the appearance of the chart
    chart.setBackgroundPaint(Color.white);

    XYPlot plot = (XYPlot) chart.getXYPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

    // Display data points or just the lines?
    if( endPoints ) 
    {
      XYItemRenderer renderer = plot.getRenderer();
      if (renderer instanceof StandardXYItemRenderer) {
        StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
        rr.setBaseShapesVisible(true);
        rr.setBaseShapesFilled(true);
        rr.setDrawSeriesLineAsPath(true);
        rr.setSeriesPaint(0, Color.blue.brighter());
        rr.setSeriesVisible(0, true); // default
        rr.setSeriesVisibleInLegend(0, true);  // default

      }
    }

    // Tell the chart how we would like dates to read
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yy"));


    // creating SVG 
    File svgFile = new File("file.svg");
    Rectangle2D r2d = new Rectangle2D.Double (
            100.0, 
            100.0,
            30.0,
            200.0
            ); 


    // write it to file
    try {
        exportChartAsSVG(chart, r2d.getBounds(), svgFile);

        // TODO: notify the user the file has been saved (e.g. status bar)
        System.out.println("Figured saved as " + svgFile.getAbsolutePath());

    } catch (IOException e) {
        System.err.println("Error saving file:\n" + e.getMessage());
    }

            return chart;         
  }
      void exportChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws    IOException {
      // Get a DOMImplementation and create an XML document
      DOMImplementation domImpl =
          GenericDOMImplementation.getDOMImplementation();
      Document document = domImpl.createDocument(null, "svg", null);

      // Create an instance of the SVG Generator
      SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

      // draw the chart in the SVG generator
      chart.draw(svgGenerator, bounds);

      // Write svg file
      OutputStream outputStream = new FileOutputStream(svgFile);
      Writer out = new OutputStreamWriter(outputStream, "UTF-8");
      svgGenerator.stream(out, true /* use css */);                     
      outputStream.flush();
      outputStream.close();
    }    



    // Main method
  public static void main( String[] args )
  {

    StockHistoryChart shc = new StockHistoryChart("file.csv");

    JFrame frame = new JFrame(shc.CurrentMonth());
    frame.getContentPane().add( shc, BorderLayout.CENTER );
    frame.setSize( 640, 480 );
    frame.setVisible( true );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

  }
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:05:41+00:00Added an answer on June 14, 2026 at 12:05 am

    I resolved this problem I was facing (not able to create a SVG file with all charts), by removing the exportChartAsSVG() and calling the SVG Generator from the main function like so;

    // Get a DOMImplementation.
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    
    // Create an instance of org.w3c.dom.Document.
    String svgNS = "http://www.w3.org/2000/svg";
    Document document = domImpl.createDocument(svgNS, "svg", null);
    
    // Create an instance of the SVG Generator.
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    
    // Ask the shc to render into the SVG Graphics2D implementation.
    shc.paint(svgGenerator);
    
    // Create a SVG file to populate our Frame
    File svgFile = new File ("svgfile.svg");
    FileOutputStream fos = new FileOutputStream(svgFile);
    
    // Finally, stream out SVG to the standard output using
    // UTF-8 encoding.
    boolean useCSS = true; // we want to use CSS style attributes
    Writer out = new OutputStreamWriter(fos, "UTF-8");
    svgGenerator.stream(out, useCSS);
    out.flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using jfreechart to create Gantt chart and even managed to add
I'm using jFreeChart 1.0.14. I have graph with a horizontal DateAxis . And I'm
I have heard of JfreeChart but is there any general steps for using data
I want to create a graph using JFreeChart and display it in a java
I have a time series data which I can draw using jfreechart. the issue
How can I draw such a graph using Swing? I have used a JFreeChart
Right now I'm using JFreeChart in order to create a dynamic chart. However the
I am using Jfreechart to create a chart. I am displaying this on a
We are using JFreeChart to make XY plots and we have a feature request
I have created a chart using JFreeChart inside a JSP. I want to render

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.