Override BarRenderer and show value labels

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Jim D
Posts: 8
Joined: Wed Sep 10, 2003 6:31 am
Location: Silicon Valley CA
Contact:

Override BarRenderer and show value labels

Post by Jim D » Thu Sep 25, 2003 4:52 am

I'm making a horizontal bar chart that I need to have seperate colors for each bar and also show the value of each bar at the end of the bar.

To do the colors I subclass the BarRenderer and override the getItemPaint(...) method.

Once I do this, the code directly below which attempts to set the value labels does not work.. If I remove the code which sets my custom rendere, it then works again.

Could this be a bug? I'm using JFree 0.9.12

Code: Select all

// get plot
		CategoryPlot categoryPlot = chart.getCategoryPlot();
	
		// Create new instance of custom class to override getItemPaint() method
		CustomHorizontalBarChartRenderer customBarRenderer = new CustomHorizontalBarChartRenderer();	
		// Set instance as new renderer.
		categoryPlot.setRenderer(customBarRenderer);
Now I attempt to set the value labels at the end of each bar.

Code: Select all

// Display the numeric values at the end of the bar. 
		CategoryItemRenderer itemRenderer = categoryPlot.getRenderer();
        itemRenderer.setItemLabelsVisible(Boolean.TRUE);
        itemRenderer.setItemLabelAnchor(ItemLabelAnchor.INSIDE12);
        itemRenderer.setItemLabelTextAnchor(TextAnchor.CENTER_RIGHT);
        itemRenderer.setItemLabelFont(chartSpec.getLabelFont());
When I say getRenderer() in the above pice, I should be getting back my new custom bar renderer, right?

Thanks for the help..

Jim

dshah
Posts: 24
Joined: Wed Sep 17, 2003 2:07 pm

Post by dshah » Fri Sep 26, 2003 11:12 am

did u try casting the renderer u get to the type of ur created renderer ?

i have done something similar and the following code works fine for me :

Code: Select all

  WaterFallBarRenderer renderer = new WaterFallBarRenderer();
  renderer.setItemLabelGenerator(new WaterFallCategoryItemLabelGenerator(labelFormatter));
  renderer.setItemLabelsVisible(Boolean.TRUE);
  renderer.ItemLabelAnchor labelAnchor = ItemLabelAnchor.OUTSIDE12;
  renderer.TextAnchor textAnchor = TextAnchor.BOTTOM_CENTER;

  CategoryPlot plot = new CategoryPlot(data, xAxis, yAxis, renderer);
  plot.setBackgroundPaint(Color.lightGray);
  plot.setOutlinePaint(Color.black);
HTH

Locked