I have created a bar chart where each bar is a color i set. However, the bottom labels no longer are centered under the bars.
How do I get the labels for each bar to center under the bar it refers to?
Bar Chart label issue
I created only one category, added all the tasks, created a custom bar renderer ( which set a color based on the item label ) and set this renderer to the plot.
Here is the sample code for the renderer ( this is for GanttRenderer but the same code should work for BarRenderer also ).
-- pady
Here is the sample code for the renderer ( this is for GanttRenderer but the same code should work for BarRenderer also ).
Code: Select all
import org.jfree.chart.renderer.category.GanttRenderer;
import java.awt.*;
import org.jfree.chart.renderer.category.*;
import java.awt.geom.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.axis.*;
import org.jfree.data.gantt.*;
public class CustomRenderer extends GanttRenderer {
public CustomRenderer() {
}
protected void drawTask(Graphics2D g2,
CategoryItemRendererState state,
Rectangle2D dataArea,
CategoryPlot plot,
CategoryAxis domainAxis,
ValueAxis rangeAxis,
GanttCategoryDataset dataset,
int row,
int column) {
setColor(g2, dataset, row, column);
super.drawTask(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column);
}
protected void drawTasks(Graphics2D g2,
CategoryItemRendererState state,
Rectangle2D dataArea,
CategoryPlot plot,
CategoryAxis domainAxis,
ValueAxis rangeAxis,
GanttCategoryDataset dataset,
int row,
int column) {
setColor(g2, dataset, row, column);
super.drawTasks(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column);
}
private void setColor(Graphics2D g2,
GanttCategoryDataset dataset, int row, int column) {
String str = (String)dataset.getColumnKey(column);
if ( str.equals("Task 1") )
setSeriesPaint(0, Color.blue);
else
setSeriesPaint(0, Color.green);
}
}
-- pady
Still doesnt work
Even with the custom render I cant get the labels to line up- they are all over the the right of the bar they refer to.
The text anchors seem like they would do what i want, but they are deprecated...
The text anchors seem like they would do what i want, but they are deprecated...
My solution
I encountered the exact same problem with bar chart. The thing is that I used rotation method such that it messed up the label position. To vertical bar chart with label bottom up, I repositioned label as following:
You can simplify the above code for your case. Mine is a general solution because it is a wrapper of jfreechart for chart rendering.
Good Luck!
Code: Select all
CategoryAxis xAxis = this.plot.getDomainAxis();
this.plot.getDomainAxis().setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(doubleAngle));
// Adjust label position.
CategoryLabelPositions positions = xAxis.getCategoryLabelPositions();
CategoryLabelPosition position = positions.getLabelPosition(RectangleEdge.BOTTOM);
if (position.getCategoryAnchor() == RectangleAnchor.TOP &&
position.getLabelAnchor() == TextBlockAnchor.TOP_RIGHT) {
CategoryLabelPositions newPositions = CategoryLabelPositions.replaceBottomPosition(
positions, new CategoryLabelPosition(RectangleAnchor.TOP, TextBlockAnchor.CENTER_RIGHT,
TextAnchor.CENTER_RIGHT, -Math.PI / 2.0,
CategoryLabelWidthType.RANGE, 0.30f));
xAxis.setCategoryLabelPositions(newPositions);
}
Good Luck!