Different Colors for Bars

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Reji Mani

Different Colors for Bars

Post by Reji Mani » Wed Feb 20, 2002 1:51 am

Hi,

I have to plot a Horizontal Bar chart with 3 Bars (3 Categories) in different colors.(i.e there is only 3 categories and I have to change the colors of the bars based on database values). How can I do that?

Thanks in advance...

Reji from Korea

jim moore

Re: Different Colors for Bars

Post by jim moore » Wed Feb 20, 2002 6:10 pm

I had to do exactly this and sort of hacked a solution. Where there were actually 3 categories and 3 series, so the data looked like:

[val ] [null] [null]
[null] [val ] [null]
[null] [null] [val ]

Then I wrote a special renderer to handle it and set the renderer for the HorizontalBarPlot to my renderer:

plot.setRenderer(new SingleSeriesMultipleColorHorizontalBarRenderer());


I can't attach a file here, so I will just paste the code for the renderer. If you are having trouble reading it, I can email it to you.

The main point is to override barWidthsPerCategory to return 1 and override the drawBar method.


import com.jrefinery.chart.*;
import com.jrefinery.data.CategoryDataset;
import java.awt.*;
import java.awt.geom.*;

public class SingleSeriesMultipleColorHorizontalBarRenderer
extends HorizontalBarRenderer {


private static final double BAR_OUTLINE_WIDTH_THRESHOLD = 3.0;


/**
* This renderer shows each series within a category as a separate bar (as opposed to a
* stacked bar renderer).
* @param data The data being plotted.
*/
public int barWidthsPerCategory(CategoryDataset data) {
return 1;
}

/**
* Draws the bar for a single (series, category) data item.
* @param g2
* @param plotArea
* @param plot
* @param valueAxis
* @param data
* @param series
* @param category
* @param categoryIndex
* @param translatedZero
* @param itemWidth
* @param categorySpan
* @param categoryGapSpan
* @param itemSpan
* @param itemGapSpan
*/
public Shape drawBar(Graphics2D g2, Rectangle2D plotArea, BarPlot plot, ValueAxis valueAxis,
CategoryDataset data, int series, Object category, int categoryIndex,
double translatedZero, double itemWidth,
double categorySpan, double categoryGapSpan,
double itemSpan, double itemGapSpan) {

Shape result = null;

// first check the value we are plotting...
Number value = data.getValue(series, category);
if (value!=null) {
// X
double translatedValue = valueAxis.translateValueToJava2D(value.doubleValue(), plotArea);
double rectX = Math.min(translatedZero, translatedValue);

// Y
double rectY = plotArea.getY() + plotArea.getHeight()*plot.getIntroGapPercent();

int categories = data.getCategoryCount();
//int seriesCount = data.getSeriesCount();
if (categories>1) {
rectY = rectY
// bars in completed categories
+ (categoryIndex*categorySpan/categories)
// gaps between completed categories
+ (categoryIndex*categoryGapSpan/(categories-1)) + itemWidth/2;
// bars+gaps completed in current category
}
else {
rectY = rectY + (categorySpan) + itemWidth/2;
}

// WIDTH
double rectWidth = Math.abs(translatedValue-translatedZero);

// HEIGHT
double rectHeight = itemWidth;


// DRAW THE BAR...
Rectangle2D bar = new Rectangle2D.Double(rectX, rectY, rectWidth, rectHeight);
Paint seriesPaint = plot.getSeriesPaint(series);
g2.setPaint(seriesPaint);
g2.fill(bar);
if (itemWidth>BAR_OUTLINE_WIDTH_THRESHOLD) {
g2.setStroke(plot.getSeriesStroke(series));
g2.setPaint(plot.getSeriesOutlinePaint(series));
g2.draw(bar);
}
result = bar;
}
return result;
}
}

Reji Mani

Re: Different Colors for Bars

Post by Reji Mani » Tue Mar 12, 2002 8:24 am

Hi,

[val ] [null] [null]
[null] [val ] [null]
[null] [null] [val ]

How about creating a StackedHorizontalBarChart with the above value specified? That also gives different colours.. right?

regards,

Reji

Locked