Problem with coloring

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
pkautz
Posts: 1
Joined: Tue Sep 06, 2005 7:52 am

Problem with coloring

Post by pkautz » Tue Sep 06, 2005 8:15 am

I have a StackedBarChart and try to change the colors. I wrote the following two examples:

this one doesn't work:

Code: Select all

import java.awt.Color;
import java.io.File;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class TestStackedBarChart {

  public static void main(
      String[] args) {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    dataset.addValue(5, "WaitingTime", "1");
    dataset.addValue(26, "DrivingTime", "1");
    dataset.addValue(9, "WaitingTime", "2");
    dataset.addValue(12, "DrivingTime", "2");

    CategoryAxis categoryAxis = new CategoryAxis("X-Axis");
    categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);

    NumberAxis numberAxis = new NumberAxis("Y-Axis");

    BarRenderer renderer = new BarRenderer();
    renderer.setSeriesPaint(0, Color.GREEN);
    renderer.setSeriesPaint(1, Color.YELLOW);

    CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, numberAxis,
        renderer);

    JFreeChart chart = ChartFactory.createStackedBarChart("", "", "", dataset,
        PlotOrientation.VERTICAL, true, false, false);
    chart.setBackgroundPaint(Color.WHITE);

    try {
      ChartUtilities.saveChartAsPNG(new File("chart.png"), chart, 1000, 600);
    } catch (Exception ex) {
      System.out.println("Error: " + ex);
    }
  }
}
This one works fine:

Code: Select all

import java.awt.Color;
import java.io.File;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class TestStackedBarChart2 {

  public static void main(
      String[] args) {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    dataset.addValue(5, "WaitingTime", "1");
    dataset.addValue(26, "DrivingTime", "1");
    dataset.addValue(9, "WaitingTime", "2");
    dataset.addValue(12, "DrivingTime", "2");

    CategoryAxis categoryAxis = new CategoryAxis("X-Axis");
    categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);

    NumberAxis numberAxis = new NumberAxis("Y-Axis");

    BarRenderer renderer = new BarRenderer();

    CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, numberAxis,
        renderer);

    JFreeChart chart = ChartFactory.createStackedBarChart("", "", "", dataset,
        PlotOrientation.VERTICAL, true, false, false);
    chart.setBackgroundPaint(Color.WHITE);
    
    plot = chart.getCategoryPlot();
    renderer = (BarRenderer) plot.getRenderer(0);
    renderer.setSeriesPaint(0, Color.GREEN);
    renderer.setSeriesPaint(1, Color.YELLOW);

    try {
      ChartUtilities.saveChartAsPNG(new File("chart.png"), chart, 1000, 600);
    } catch (Exception ex) {
      System.out.println("Error: " + ex);
    }
  }
}
What is the difference? Why the first one doesn't work? Which one is the right? Or is there another way?

Regards

Patrick Kautz

pmlb
Posts: 31
Joined: Thu Aug 25, 2005 5:18 pm
Location: France

Post by pmlb » Tue Sep 06, 2005 10:29 am

In the non working example you create a renderer, change the colors create a plot, assign the renderer to the plot, and then you create a chart with ChartFactory.createStackedBarChart but when creating the chart you don't use the plot previously prepared because there is no plot parameter for the createStackedBarChart method.
So a default plot with a default renderer is created and this is the one which is used.

In the working example, after creating the chart you extract the default plot from the chart, extract the default renderer from the plot and make your modifications, so that is why it works. By the way, in the working example these lines :

Code: Select all

BarRenderer renderer = new BarRenderer();
CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, numberAxis,
        renderer);
should be modified this way :

Code: Select all

BarRenderer renderer;
CategoryPlot plot;
there is no need to create objects that you won't use.
Pierre-Marie

Locked