Display Dataset with LineChart - JFreeChart 1.0.13

A free public discussion forum for the JFreeChart class library.

Display Dataset with LineChart - JFreeChart 1.0.13

Postby karthicr » Wed Jul 28, 2010 7:04 pm

Greetings!

I was wondering if it is possible to plot the dataset itself along with the graph while generating a jpg file.

To illustrate my question:

a. What I need:
Image

b. What I've done:
Image

My source code:
Code: Select all
import java.awt.Color;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class LineChartDemo1 {

   private static final long serialVersionUID = 7032394551486104761L;

   public CategoryDataset createDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(755.29, "Revenue", "01-JUL-2010");
        dataset.addValue(305.92, "Revenue", "02-JUL-2010");
        dataset.addValue(102.00, "Revenue", "03-JUL-2010");
        dataset.addValue(437.47, "Revenue", "04-JUL-2010");
        dataset.addValue(269.66, "Revenue", "05-JUL-2010");
        dataset.addValue(1503.76, "Revenue", "06-JUL-2010");

        return dataset;
    }
   
    public JFreeChart createChart(final CategoryDataset dataset) {
        JFreeChart chart = ChartFactory.createLineChart(
            "Revenue - Past 30 Days",
            "Date",
            "Revenue",
            dataset,
            PlotOrientation.VERTICAL,
            false,
            false,
            false);

        chart.setBackgroundPaint(Color.white);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.white);
        plot.setRangeGridlinePaint(Color.black);

        return chart;
    }
   
    public void saveChart(JFreeChart chart) {
       try {
         ChartUtilities.saveChartAsJPEG(new File("test.jpg"), chart, 800, 400);
      } catch (IOException e) {
         e.printStackTrace();
      }
    }
   
    public static void main(final String[] args) {
       LineChartDemo1 demo = new LineChartDemo1();
       demo.saveChart(demo.createChart(demo.createDataset()));
    }
}


I did search for the same in the forums and a lot on Google. But my query contains the term 'dataset' which brings up a lot of unrelated (in my context) as all pages which have a code snippet have dataset in them.

So I'd appreciate any pointers in this regard. Is this doable? If yes, how?

Thanks in advance for your time.
karthicr
 
Posts: 3
Joined: Wed Jul 28, 2010 6:49 pm

Re: Display Dataset with LineChart - JFreeChart 1.0.13

Postby karthicr » Fri Jul 30, 2010 3:08 pm

Hey guys, just bumping the question up. It's been two days and no replies :D

Please can anyone of you let me know if it can be done at all?

Thanks!
karthicr
 
Posts: 3
Joined: Wed Jul 28, 2010 6:49 pm

Re: Display Dataset with LineChart - JFreeChart 1.0.13

Postby paradoxoff » Fri Jul 30, 2010 6:47 pm

Is this thread helpful?
The missing methods in GridArrangement have since then been implemented.
Note that you will have to extract the keys and values to contruct the table on your own from the dataset.
paradoxoff
 
Posts: 826
Joined: Sat Feb 17, 2007 1:51 pm

Re: Display Dataset with LineChart - JFreeChart 1.0.13

Postby karthicr » Fri Jul 30, 2010 7:00 pm

Thanks paradoxoff!

It could just be the thing I was looking for. I'm at work now. I will try this out today evening and let you know how things went.
karthicr
 
Posts: 3
Joined: Wed Jul 28, 2010 6:49 pm


Return to JFreeChart - General

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest