Group Stack Bar Chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
blajevardi
Posts: 9
Joined: Tue Nov 21, 2006 2:19 am

Group Stack Bar Chart

Post by blajevardi » Tue Nov 21, 2006 6:30 pm

Hi everybody,

I am trying to create a bar chart exactly like StackedBarChartDemo4.
But I tried to follow that example and define a data set and a KeyToGroupMap, but it seems to me that I cannot define any data set which is very dynamic. In the context of StackedBarChartDemo4, I would not know how many products, how many regions, and how many groups I have to deal with. The example assigns even colors manually to the series.

Does anybody have an example that shows how to create groups and color mapping automatically?

thanks,

Brian

rohi
Posts: 11
Joined: Wed Nov 15, 2006 2:59 pm

Post by rohi » Thu Nov 23, 2006 10:27 am

you can try this:
GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
KeyToGroupMap map = new KeyToGroupMap("G1");
for(int i = 0; i< l_comp_names.length; i++) // l_comp_names is your group(like product in the demo)
{
for(int j = 0; j<ml_series.length; j++) // this is the length of your series array
{
map.mapKeyToGroup(""+l_comp_names+"("+ml_series[j]+")", "G"+i);
}
}
renderer.setSeriesToGroupMap(map);

rohi
Posts: 11
Joined: Wed Nov 15, 2006 2:59 pm

Post by rohi » Thu Nov 23, 2006 10:30 am

try this for the sereis paint:

colors = new Color [ml_series.length];
for(int i = 0; i<colors.length; i++)
{
colors = getColor();
System.err.println("gotten color "+colors.toString());
}

int indexOfColor = 0;
int map_size = l_comp_names.length * ml_series.length;
for (int i =0; i<map_size; i++)
{
indexOfColor = i%ml_series.length;
renderer.setSeriesPaint(i, colors[indexOfColor]);
}

rohi
Posts: 11
Joined: Wed Nov 15, 2006 2:59 pm

Post by rohi » Thu Nov 23, 2006 10:31 am

for the above, this is the getColor() method i wrote:

private List<Color> myColors = new ArrayList();
private Color getColor()
{
while (true)
{
float R = (float)(Math.random()%255);
float G = (float)(Math.random()%255);
float B = (float)(Math.random()%255);

Color c = new Color(R,G,B);

if (!myColors.contains(c))
{
myColors.add(c);
return c;
}
}
}

blajevardi
Posts: 9
Joined: Tue Nov 21, 2006 2:19 am

Hi Rohi

Post by blajevardi » Tue Nov 28, 2006 8:46 pm

Thank you for taking time writing some code for me. It took me a while to understand the whole concept of grouping. I needed to reorganize my data to be able to use grouping. It was not very clear what we are trying to group. From the chart I thought we group the data based on the date, but somehow we group them based on the product!
Anyway, I think I am almost there. The only thing left is that I am trying to turn my sub CategoryLabelPositions. I could not find any method for that. My sub Category labels are pretty long, therefore I need to turn them about 45 degree, so they can be readable.
Are you aware of any function call for that?

Thanks again,

Brian

rohi
Posts: 11
Joined: Wed Nov 15, 2006 2:59 pm

Post by rohi » Fri Dec 01, 2006 3:50 pm

you are welcome. to rotate your subcategory labels, you will use something like this:

Code: Select all

domainAxis.setSubLabelFont(domainAxis.getSubLabelFont().deriveFont(0, AffineTransform.getRotateInstance(Math.PI / 2.0)));
you might need to play around with the value abit, maybe set 2.0 to 2.5, etc, until you get the angle you desire..

you will also need to offset your labels so that it does not overlap on your primary category labels:

Code: Select all

domainAxis.setCategoryLabelPositionOffset(80);
// or any double value that works for you. it doesn't have to be 80.

hope i was able to help!
Rohi

blajevardi
Posts: 9
Joined: Tue Nov 21, 2006 2:19 am

Post by blajevardi » Tue Dec 05, 2006 12:13 am

Hey, you were a great help. I followed your instructions and I am almost there. The other issues I see are two things:
1) the font has been changed in the process which was predictable, but now it is not that readable as before. I am still experimenting with what font to use to get maximum readability.
2) the label position for the sub category items are off, if they include an "_" (underscore). I guess the position is calculated based on centering the underscore! I am still looking for some function call to line up the label with the bar.


Thanks,

Brian

Locked