How to set color of bars in CategoryDataset intact?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
prassoon
Posts: 14
Joined: Fri May 23, 2008 1:57 am

How to set color of bars in CategoryDataset intact?

Post by prassoon » Fri Jun 13, 2008 1:08 am

I have searched this forum and got some clues how to do this but still i couldnt make it :roll: . Here is the issue;

I'm creating the dataset like this

Code: Select all

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(value, Name, "1 to 2 second bin");
//there will be several bins of various timeframe like 2 to 3 second 3 to 4 
//second and so on

Code: Select all

JFreeChart chart = ChartFactory.createBarChart3D("Histogram (Per Module)", // chart title
        "Time frame in seconds", // domain axis label
        "Hit count", // range axis label
        dataset, // CategoryDataset
        PlotOrientation.VERTICAL, // orientation
        true, // include legend
        true, // tooltips
        false // urls
        );
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryItemRenderer renderer = plot.getRenderer();
renderer.setSeriesPaint(0, Color.RED);
The question is how to set the color of items in the bins. I'm actually showing this over and over again with different values and i want to keep the colors of the items in the bin consistent every time i recreate the chart. Is this possible? If anyone can shed some light that would be so great....

BarChart3DDemo3.java in the demo kit is the one that matches closely with this. I wanted the series colors to be same every time i recreate it. If there are some series missing at some point the colors will vary, which is the issue that i'm talking about. I hope the issue is been said clearly, please give me a shout if you need more info...

Cheers

prassoon
Posts: 14
Joined: Fri May 23, 2008 1:57 am

Post by prassoon » Fri Jun 13, 2008 5:14 pm

I found the example code in BarChart3DDemo4.java which works fine with the color in the chart but now the legend is showing different colors. :shock:

do I need to add legend manually?

prassoon
Posts: 14
Joined: Fri May 23, 2008 1:57 am

Post by prassoon » Fri Jun 13, 2008 9:05 pm

What i was looking for is how to get the series out of dataset and couldnt find any method for that. Apparently

Code: Select all

dataset.getRowCount()
returns the number of series and

Code: Select all

dataset.getRowKey(0)
will return the name of the series;

Code: Select all

renderer.setSeriesPaint(row, Paint);
that solved the issue without subclassing

Locked