Bar Chart label issue

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

Bar Chart label issue

Post by moody1 » Thu Jun 09, 2005 7:05 pm

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?

padhu
Posts: 7
Joined: Tue Jun 07, 2005 8:31 pm

Post by padhu » Thu Jun 09, 2005 8:39 pm

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 ).

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

moody1

Still doesnt work

Post by moody1 » Thu Jun 09, 2005 9:48 pm

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...

bubblekou
Posts: 4
Joined: Tue Aug 19, 2003 2:35 am
Contact:

My solution

Post by bubblekou » Tue Jun 28, 2005 8:20 am

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:

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);
    }
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!

Locked