jfreechart多个数据集到svg图像

时间:2012-09-18 16:38:54

标签: java svg jfreechart

我正在使用JFreeChart从csv文件填充图表,我已设法从文件创建多个数据集并绘制数据。

SVG图像正在重写为“file.svg”,所以没有任何东西看起来很漂亮。问题是,如何从多个数据集中创建单个SVG图像,如果只知道如何获取每个图表,则按顺序绘制并将所有图表绘制到SVG文件中。

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 个答案:

答案 0 :(得分:1)

我解决了我遇到的这个问题(不能用所有图表创建一个SVG文件),删除exportChartAsSVG()并从主函数中调用SVG Generator就像这样;

// 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();