In Bar chart can I put any image in place of Item label.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
ashish_sriv
Posts: 20
Joined: Tue Jan 16, 2007 1:54 pm

In Bar chart can I put any image in place of Item label.

Post by ashish_sriv » Thu Mar 01, 2007 8:44 pm

Hi,
I want to put some colored image like filled circle on the top of every bar chart item. Can we do that in JFreeChart?

Thanks

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Mar 02, 2007 6:07 pm

If you need this for every data item, then I'd suggest a customised version of the BarRenderer.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

pin2
Posts: 2
Joined: Fri Mar 02, 2007 11:42 pm

Post by pin2 » Fri Mar 02, 2007 11:46 pm

Hi Ashish,
We are stucked to the same kind of problem. If you would have found the solution using BarRenderer. I would really appreciate if you could post the code which will help us for our assignment.
Thanks in advance
Pin2

ashish_sriv
Posts: 20
Joined: Tue Jan 16, 2007 1:54 pm

Post by ashish_sriv » Mon Mar 05, 2007 11:55 pm

I am still in the process, I will surly be sharing the code once it is done.
Thanks,

ashish_sriv
Posts: 20
Joined: Tue Jan 16, 2007 1:54 pm

Post by ashish_sriv » Tue Mar 06, 2007 8:00 pm

Hi Pin,

As suggested by Gilbert I extended the BarRenderer and overridden the method "drawItem".

Here is the overridden code.... Hoping this would work for you too.

public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass)
{


// Thread.dumpStack();
// nothing is drawn for null values...
Number dataValue = dataset.getValue(row, column);
if (dataValue == null) {
return;
}

double value = dataValue.doubleValue();

Paint circleColor = getCircleColor(value);

PlotOrientation orientation = plot.getOrientation();
double barW0 = calculateBarW0(plot, orientation, dataArea, domainAxis,
state, row, column);
double[] barL0L1 = calculateBarL0L1(value);
if (barL0L1 == null) {
return; // the bar is not visible
}

RectangleEdge edge = plot.getRangeAxisEdge();
double transL0 = rangeAxis.valueToJava2D(barL0L1[0], dataArea, edge);
double transL1 = rangeAxis.valueToJava2D(barL0L1[1], dataArea, edge);
double barL0 = Math.min(transL0, transL1);
double barLength = Math.max(Math.abs(transL1 - transL0),
getMinimumBarLength());

// draw the bar...
Rectangle2D bar = null;
Shape indicatorShape = null;
if (orientation == PlotOrientation.HORIZONTAL) {
bar = new Rectangle2D.Double(barL0, barW0, barLength,
state.getBarWidth());
indicatorShape = new Ellipse2D.Double(barL0+barLength-state.getBarWidth(), barW0, state.getBarWidth(),state.getBarWidth());

}
else {
bar = new Rectangle2D.Double(barW0, barL0, state.getBarWidth(),
barLength);
indicatorShape = new Ellipse2D.Double(barW0,barL0, state.getBarWidth(),state.getBarWidth());
}

Paint itemPaint = getItemPaint(row, column);
GradientPaintTransformer t = getGradientPaintTransformer();
if (t != null && itemPaint instanceof GradientPaint) {
itemPaint = t.transform((GradientPaint) itemPaint, bar);
}

g2.setPaint(itemPaint);
g2.fill(bar);
g2.setPaint(circleColor);

g2.fill(indicatorShape);

// draw the outline...
if (isDrawBarOutline()
&& state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
Stroke stroke = getItemOutlineStroke(row, column);
Paint paint = getItemOutlinePaint(row, column);
if (stroke != null && paint != null) {
g2.setStroke(stroke);
g2.setPaint(paint);
g2.draw(bar);
}
}

CategoryItemLabelGenerator generator
= getItemLabelGenerator(row, column);
if (generator != null && isItemLabelVisible(row, column)) {
drawItemLabel(g2, dataset, row, column, plot, generator, bar,
(value < 0.0));
}

// add an item entity, if this information is being collected
EntityCollection entities = state.getEntityCollection();
if (entities != null) {
addItemEntity(entities, dataset, row, column, bar);
}

}

private Paint getCircleColor(double value)
{
if (value <= 20)
return Color.red;
if (value<=35)
return Color.yellow;
return Color.green;
}


Thanks,
Ashish

Locked