Developers guide has no data on CSV file import

A free public discussion forum for the JFreeChart class library.

Developers guide has no data on CSV file import

Postby kbvramya » Thu Jul 03, 2008 4:09 am

Hi,

I recently purchased the Developer's guide for JfreeChart . I wanted to use it mainly to create data sets from CSV files and generate graphs. But the Guide has the following comment on the page it is supposed to explain CSV import.

Chapter 48
Package: org.jfree.data.io
48.1 Introduction
I/O related classes.
48.2 CSV
48.2.1 Overview
To be documented.

I have a code written to take a CSV file but the output graph is incorrect and i am unable to understand how readCategoryDataset() of the CSV classs is handling the data internally.

My code snippet, to draw a line chart by extracting the data from a log file in CSV format with two columns of data, first column to be plotted for Y Axis and the second for X axis:

CSV mcd_log = new CSV();
File file = new File("C:/eclipse/workspace/DataExtraction/res/Data.log");

BufferedReader in = new BufferedReader(new FileReader(file));

CategoryDataset data = mcd_log.readCategoryDataset((java.io.Reader)in);


JFreeChart chart = ChartFactory.createLineChart("MCD Status", "Time", "Eth1 Tx Packets", data, PlotOrientation.VERTICAL, false, true, false);

CategoryPlot plot = (CategoryPlot) chart.getPlot();

LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setFillPaint(Color.white);

ChartFrame frame = new ChartFrame("MCD Status", chart);
frame.pack();
frame.setVisible(true);

Could I get the completed document or some help in using the CSV class.

Please help,
Thanks.
kbvramya
 
Posts: 3
Joined: Thu Jul 03, 2008 3:53 am

Re: Developers guide has no data on CSV file import

Postby RichardWest » Thu Jul 03, 2008 5:53 am

kbvramya wrote:Could I get the completed document or some help in using the CSV class.


The initial comment in the CVS JavaDoc (personally: bad name for a class; CsvReader would be better) sums up the state of the class' development:
This initial version is very basic, and won't handle errors in the data file very gracefully.
The class has not been updated since late 2003, and will likely remain that way. David may have a different take on the situation than I, but I would have deprecated and removed the class a long time ago.

Comma-separated values (CSV) is an easy concept: values are separated by commas. In practice, it is hard to write a class that can parse arbitrary CSV or similarly delineated files since the organization of the data is entirely application specific. The data could be organized in rows or columns, delineated by commas or tabs, and with or without quotes. As a result, most users write their own class to parse CSV files which is not a difficult task if your data is well organized.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA
RichardWest
 
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Postby david.gilbert » Thu Jul 03, 2008 3:28 pm

Ah, one of the poorly documented, badly written and seldom used classes in JFreeChart. Basically what Richard has said is all true. And I don't think the class will handle the data that you want to read. But in any case, I thought I might as well write the documentation for the class, so here is a link to a draft of what will go into the JFreeChart Developer Guide for version 1.0.11.

http://www.object-refinery.com/jfreechart/CSV.pdf

The referenced demo file is:

Code: Select all
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.CategoryLineAnnotation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.LineBorder;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.io.CSV;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
* A simple demo showing the use of the CSV class to read a comma-delimited
* file into a {@link CategoryDataset}.
*/
public class CSVDemo1 extends ApplicationFrame {

    /**
     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    public CSVDemo1(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        CategoryDataset dataset = null;
        try {
            dataset = createDataset();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        JFreeChart chart = createChart(dataset);
        return new ChartPanel(chart);
    }
    /**
     * Returns a sample dataset.
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset() throws IOException{
        CSV csvReader = new CSV();
        Reader in = new FileReader(new File("{path-to-csv-file}/csvdata.txt"));
        CategoryDataset dataset = csvReader.readCategoryDataset(in);
        in.close();
        return dataset;
    }
   
    /**
     * Creates a sample chart.
     *
     * @param dataset  the dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(CategoryDataset dataset) {
       
        // create the chart...
        JFreeChart chart = ChartFactory.createBarChart(
            "CSVDemo1.java",       // chart title
            "Category",               // domain axis label
            "Value",                  // range axis label
            dataset,                  // data
            PlotOrientation.HORIZONTAL, // orientation
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.white);
       
        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
       
        // set up gradient paints for series...
        GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue,
                0.0f, 0.0f, new Color(0, 0, 64));
        GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green,
                0.0f, 0.0f, new Color(0, 64, 0));
        GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red,
                0.0f, 0.0f, new Color(64, 0, 0));
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);

        return chart;
       
    }
   
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        CSVDemo1 demo = new CSVDemo1("JFreeChart: CSVDemo1.java");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11337
Joined: Fri Mar 14, 2003 10:29 am

Ability to zoom the X axis

Postby kbvramya » Thu Jul 03, 2008 11:44 pm

Hi Dave,

Thank you very much for csv.pdf document and code sample.
The CSV file that I was using had to be transposed to match sample CSV file format given in the document, for it to work properly, where.
X axis - First row
Y Axis - Second row onwards

What i was using earlier was:
X axis - First column
Y Axis - Second column onwards

Just another question about ability to zoom the X axis. I saw that when a line graph is drawn on screen there is a option to zoom the Y axis directly on screen using mouse drag , but there is no such option to do so for the X Axis.

So to zoom the X axis what method of the CategoryPlot class/(which other class) should I use?

Thanks in advance,
Ramya
kbvramya
 
Posts: 3
Joined: Thu Jul 03, 2008 3:53 am


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 8 guests