StackedAreaChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
JustinSD
Posts: 1
Joined: Sun Oct 10, 2010 5:25 pm
antibot: No, of course not.

StackedAreaChart

Post by JustinSD » Sun Oct 10, 2010 6:12 pm

Hi,

I have a problem with the StackedAreaChart. Here is how I create it.

Code: Select all

import java.awt.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.DefaultCategoryDataset;

public class TestChart
{
   public static ChartPanel createChart()
   {
      DefaultCategoryDataset dataset = new DefaultCategoryDataset();

      dataset.setValue(null, "Car1", "Type1");
      dataset.setValue(null, "Car2", "Type1");
      dataset.setValue(null, "Car3", "Type1");
      dataset.setValue(null, "Car4", "Type1");
      dataset.setValue(null, "Car5", "Type1");

      dataset.setValue(null, "Car1", "Type2");
      dataset.setValue(null, "Car2", "Type2");
      dataset.setValue(null, "Car3", "Type2");
      dataset.setValue(null, "Car4", "Type2");
      dataset.setValue(null, "Car5", "Type2");

      dataset.setValue(null, "Car1", "Type3");
      dataset.setValue(null, "Car2", "Type3");
      dataset.setValue(null, "Car3", "Type3");
      dataset.setValue(null, "Car4", "Type3");
      dataset.setValue(null, "Car5", "Type3");

      dataset.setValue(null, "Car1", "Type4");
      dataset.setValue(null, "Car2", "Type4");
      dataset.setValue(null, "Car3", "Type4");
      dataset.setValue(null, "Car4", "Type4");
      dataset.setValue(null, "Car5", "Type4");

      JFreeChart carChart = ChartFactory.createStackedAreaChart
         (
            "Car Chart",
            null,
            null,
            dataset,
            PlotOrientation.VERTICAL,
            true,
            false,
            false
         );

      CategoryPlot plot = carChart.getCategoryPlot();

      plot.setBackgroundPaint(Color.white);
      plot.getDomainAxis().setLowerMargin(0.0);
      plot.getDomainAxis().setUpperMargin(0.0);
      plot.getRangeAxis().setRange(0, 50);

      return new ChartPanel(carChart);
   }

   public static void main(String args[])
   {
      JFrame frame = new JFrame();

      frame.getContentPane().add(createChart());
      frame.pack();
      frame.setVisible(true);
   }
}
I get this, which is exactly what I want, an empty chart with all the right labels.

Image

However, when I start to add data to the chart. For example,

Code: Select all

      dataset.setValue(5, "Car1", "Type1");
      dataset.setValue(6, "Car2", "Type1");
      dataset.setValue(7, "Car3", "Type1");
      dataset.setValue(8, "Car4", "Type1");
      dataset.setValue(9, "Car5", "Type1");
I get this:

Image

Here are my questions:

1) I'd like all the lines to stay flat, not converging to 0. How do I do it?
2) This is just out of curiosity. The lines for Car1 through Car4 converge to the same point, but not for Car5. Why is that?
3) If I add more data, I notice there is a thin white vertical line for each "Type". That is fine, but there is also the same line that goes between Types, but only for every two types. This is likely to cause confusion, anyway to get rid of them?

I've tried a few things, but could never get the result I want. I would appreciate any help on this.

Thanks,
Justin

Locked