Developers guide has no data on CSV file import

A discussion forum for JFreeChart (a 2D chart library for the Java platform).

Developers guide has no data on CSV file import

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


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

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

ChartFrame frame = new ChartFrame("MCD Status", chart);

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

Please help,
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
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.

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 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.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) {
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new Dimension(500, 270));

     * Creates a panel for the demo (used by
     * @return A panel.
    public static JPanel createDemoPanel() {
        CategoryDataset dataset = null;
        try {
            dataset = createDataset();
        catch (IOException e) {
        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);
        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(
            "",       // 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...

        // get a reference to the plot for further customisation...
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

        // disable bar outlines...
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        // set up gradient paints for series...
        GradientPaint gp0 = new GradientPaint(0.0f, 0.0f,,
                0.0f, 0.0f, new Color(0, 0, 64));
        GradientPaint gp1 = new GradientPaint(0.0f, 0.0f,,
                0.0f, 0.0f, new Color(0, 64, 0));
        GradientPaint gp2 = new GradientPaint(0.0f, 0.0f,,
                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:");

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
JFreeChart Project Leader
Posts: 11655
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,
Posts: 3
Joined: Thu Jul 03, 2008 3:53 am

Return to JFreeChart

Who is online

Users browsing this forum: Bing [Bot] and 5 guests