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
In Bar chart can I put any image in place of Item label.
-
- Posts: 20
- Joined: Tue Jan 16, 2007 1:54 pm
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
If you need this for every data item, then I'd suggest a customised version of the BarRenderer.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- Posts: 20
- Joined: Tue Jan 16, 2007 1:54 pm
-
- Posts: 20
- Joined: Tue Jan 16, 2007 1:54 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
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