Search code examples
jfreechart

Add $ sign to label text on bars and add thousand separator


I have a bar chart where each bar represents the dollar amount. Right now I am showing only the number. How do I add a $ sign before the number?

This is how I want it (with the $ sign)

Also is it possible to add a thousand separator? e.g $ 1,300

enter image description here

Sample code

BarRenderer renderer = (BarRenderer) plot.getRenderer();
    CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator(
            "{1}", NumberFormat.getInstance());
    renderer.setBaseItemLabelGenerator(generator);
    renderer.setBaseItemLabelFont(new Font("Arial", Font.BOLD, 7));
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseItemLabelPaint(new Color(255, 255, 255));
    renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.INSIDE3, TextAnchor.CENTER_RIGHT,
                TextAnchor.CENTER, 0.0));

Solution

  • You can just change the formatter you are passing to the StandardCategoryItemLabelGenerator constructor from NumberFormat.getInstance() to new DecimalFormat("$#,##0.00")