JFreeChart CategoryPlot overwrites categories

Posted by Christine on Stack Overflow See other posts from Stack Overflow or by Christine
Published on 2010-04-20T18:39:48Z Indexed on 2010/04/20 19:23 UTC
Read the original article Hit count: 522

Filed under:
|
|

Hi, I am new to using JFreeChart and I'm sure there is a simple solution to my problem . .

PROBLEM:
I have a chart that shows multiple "events types" along the date X axis. The Y axis shows the "event category". My problem is that only the latest date of an event type is shown for each category.

In the example below The chart shows data points for Event Type 1 at June 20th(Category 1) and at June 10th (Category 2). I had also added a data point for June 10th, Category 1 but the June 20th point erases it.

I think I am misunderstanding how the CategoryPlot is working. Am I using the wrong type of chart? I thought a scatter chart would be the ticket but it only accepts numerical values. I need to have discrete string categories on my Y-axis.

If anyone can point me in the right direction, you would really make my day. Thanks for reading!

-Christine

(The code below works as-is. It is as simple as I could make it)

import java.awt.Dimension;

import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Day;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class EventFrequencyDemo1 extends ApplicationFrame   
{   
    public EventFrequencyDemo1(String s)   
    {   
        super(s);   
        CategoryDataset categorydataset = createDataset();   
        JFreeChart jfreechart = createChart(categorydataset);   
        ChartPanel chartpanel = new ChartPanel(jfreechart);   
        chartpanel.setPreferredSize(new Dimension(500, 270));   
        setContentPane(chartpanel);   
    }   

    private static JFreeChart createChart(CategoryDataset categorydataset)   
    {   
        CategoryPlot categoryplot = new CategoryPlot(categorydataset, new CategoryAxis("Category"), new DateAxis("Date"), new LineAndShapeRenderer(false, true));
        categoryplot.setOrientation(PlotOrientation.HORIZONTAL);
        categoryplot.setDomainGridlinesVisible(true);
        return new JFreeChart(categoryplot);   
    }   

    private static CategoryDataset createDataset()   
    {   
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();   
        Day june10 = new Day(10, 6, 2002);   
        Day june20 = new Day(20, 6, 2002);
        // This event is overwritten by June20th
        defaultcategorydataset.setValue(new Long(june10.getMiddleMillisecond()), "Event Type 1", "Category 1");   
        defaultcategorydataset.setValue(new Long(june10.getMiddleMillisecond()), "Event Type 1", "Category 2");   
        // Overwrites the previous June10th event
        defaultcategorydataset.setValue(new Long(june20.getMiddleMillisecond()), "Event Type 1", "Category 1");   
        defaultcategorydataset.setValue(new Long(june20.getMiddleMillisecond()), "Event Type 2", "Category 2");   
        return defaultcategorydataset;   
    }   

    public static JPanel createDemoPanel()   
    {   
        JFreeChart jfreechart = createChart(createDataset());   
        return new ChartPanel(jfreechart);   
    }   

    public static void main(String args[])   
    {   
        EventFrequencyDemo1 eventfrequencydemo1 = new EventFrequencyDemo1("Event Frequency Demo");   
        eventfrequencydemo1.pack();   
        RefineryUtilities.centerFrameOnScreen(eventfrequencydemo1);   
        eventfrequencydemo1.setVisible(true);   
    }   
}  

© Stack Overflow or respective owner

Related posts about jfreechart

Related posts about graphing