Hi All,
i want to add the an image in bar chart .
can we add image as annotation in jfree chart for categorydataset.
please reply.......
Click into below link for more understanding what i want to say
Link :- http://1.bp.blogspot.com/_kuyKSXKgq68/S ... 0-h/w1.JPG
Thanks and Regards
Zahid
Bar Chart Help
This would probably be better of if you posted it in the main forum.
If all you want is Pointy Bars then why not override BarRenderer and create a PointyBarRenderer
Try this.
I want to add the an image in bar chart .
I am confused as to what you want. You are trying to solve a problem and assuming that adding an image will solve it.Click into below link for more understanding what i want to say
If all you want is Pointy Bars then why not override BarRenderer and create a PointyBarRenderer
Try this.
Code: Select all
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRendererState;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.GradientPaintTransformer;
import org.jfree.ui.RectangleEdge;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
public class PointyBarDemo {
public static void main(String[] args) {
ChartFrame chartFrame = new ChartFrame("Chart Demo", buildChart());
chartFrame.setSize(600, 200);
chartFrame.setVisible(true);
}
public static JFreeChart buildChart() {
CategoryAxis domainAxis = new CategoryAxis("Categories");
NumberAxis rangeAxis = new NumberAxis("Values");
PointBarRenderer renderer = new PointBarRenderer();
CategoryDataset dataset = getDataset();
renderer.setDrawBarOutline(true);
renderer.setSeriesOutlinePaint(0, Color.WHITE);
renderer.setSeriesPaint(0, Color.GREEN);
CategoryPlot mainPlot = new CategoryPlot(dataset, domainAxis, rangeAxis, renderer);
mainPlot.setOrientation(PlotOrientation.HORIZONTAL);
return new JFreeChart(mainPlot);
}
protected static CategoryDataset getDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue( 20, "Set 1", "Category 1");
dataset.setValue( 70, "Set 1", "Category 2");
dataset.setValue(100, "Set 1", "Category 3");
dataset.setValue( 30, "Set 1", "Category 4");
dataset.setValue( 50, "Set 1", "Category 5");
return dataset;
}
static class PointBarRenderer extends BarRenderer{
public void drawItem(Graphics2D g2,
CategoryItemRendererState state,
Rectangle2D dataArea,
CategoryPlot plot,
CategoryAxis domainAxis,
ValueAxis rangeAxis,
CategoryDataset dataset,
int row,
int column,
int pass) {
// nothing is drawn for null values...
Number dataValue = dataset.getValue(row, column);
if (dataValue == null) {
return;
}
double value = dataValue.doubleValue();
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);
// in the following code, barL0 is (in Java2D coordinates) the LEFT
// end of the bar for a horizontal bar chart, and the TOP end of the
// bar for a vertical bar chart. Whether this is the BASE of the bar
// or not depends also on (a) whether the data value is 'negative'
// relative to the base value and (b) whether or not the range axis is
// inverted. This only matters if/when we apply the minimumBarLength
// attribute, because we should extend the non-base end of the bar
boolean positive = (value >= this.getBase());
boolean inverted = rangeAxis.isInverted();
double barL0 = Math.min(transL0, transL1);
double barLength = Math.abs(transL1 - transL0);
double barLengthAdj = 0.0;
if (barLength > 0.0 && barLength < getMinimumBarLength()) {
barLengthAdj = getMinimumBarLength() - barLength;
}
double barL0Adj = 0.0;
if (orientation == PlotOrientation.HORIZONTAL) {
if (positive && inverted || !positive && !inverted) {
barL0Adj = barLengthAdj;
}
}
else {
if (positive && !inverted || !positive && inverted) {
barL0Adj = barLengthAdj;
}
}
// draw the bar...
Rectangle2D bar = null;
GeneralPath pointyBar = null;
if (orientation == PlotOrientation.HORIZONTAL) {
bar = new Rectangle2D.Double(barL0 - barL0Adj, barW0,
barLength + barLengthAdj, state.getBarWidth());
pointyBar = new GeneralPath(bar);
pointyBar.moveTo(barL0 - barL0Adj + barLength + barLengthAdj,barW0);
pointyBar.lineTo(barL0 - barL0Adj + barLength + barLengthAdj + state.getBarWidth()/2,barW0+state.getBarWidth()/2);
pointyBar.lineTo(barL0 - barL0Adj + barLength + barLengthAdj,barW0+state.getBarWidth());
pointyBar.closePath();
}
else {
bar = new Rectangle2D.Double(barW0, barL0 - barL0Adj,
state.getBarWidth(), barLength + barLengthAdj);
pointyBar = new GeneralPath(bar);
pointyBar.lineTo(barW0 + state.getBarWidth()/2, barL0 - barL0Adj - state.getBarWidth()/2);
pointyBar.lineTo(barW0 + state.getBarWidth(), barL0 - barL0Adj);
pointyBar.closePath();
}
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(pointyBar);
// 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(pointyBar);
}
}
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);
}
}
}
}
The answer does not come from thinking outside the box, rather the answer comes from realizing the truth; There is no Box. my js site