How can i get something like this annotation at barchart?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
marpeso
Posts: 3
Joined: Mon Feb 23, 2015 11:11 am
antibot: No, of course not.

How can i get something like this annotation at barchart?

Post by marpeso » Mon Feb 23, 2015 11:45 am

Hi,
Does anyone know how I can get something like this photo?

Image

I want to get the text horizontally on the bar.
I tried with text annotation but I have not gotten...

Thanks.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: How can i get something like this annotation at barchart

Post by paradoxoff » Mon Feb 23, 2015 9:28 pm

Do tehse values represent the data value of the respective bars?

marpeso
Posts: 3
Joined: Mon Feb 23, 2015 11:11 am
antibot: No, of course not.

Re: How can i get something like this annotation at barchart

Post by marpeso » Tue Feb 24, 2015 10:40 am

paradoxoff wrote:Do tehse values represent the data value of the respective bars?

Yes. Is the value of the bar. But could be a string anyone, no matter.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: How can i get something like this annotation at barchart

Post by paradoxoff » Wed Feb 25, 2015 9:13 pm

Well, if the numbers indeed represent the data value, then you could just have a look at the item labelling capability of JFreeChart.
Use this code as a starter:

Code: Select all

public class BarChart extends ApplicationFrame {

    public BarChart(String title) {
        super(title);

        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 350));
        setContentPane(chartPanel);
    }

    private CategoryDataset createDataset() {
        double[][] data = new double[][]{
            {10,8},
            {8,4},
            {12,6},
            {6,9},
            {9,12}};
        return DatasetUtilities.createCategoryDataset(
                "Row ", "Column ", data);
    }

    private JFreeChart createChart(final CategoryDataset dataset) {
        BarRenderer br = new BarRenderer();
        br.setBaseItemLabelsVisible(true);
        br.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        br.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, Math.PI/2.0));
        CategoryPlot cp = new CategoryPlot(dataset, new CategoryAxis("x"), new NumberAxis("x"), br);
        return new JFreeChart(cp);
    }

    public static void main(final String[] args) {

        final BarChart demo
                = new BarChart("Bar Chart");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}

marpeso
Posts: 3
Joined: Mon Feb 23, 2015 11:11 am
antibot: No, of course not.

Re: How can i get something like this annotation at barchart

Post by marpeso » Thu Feb 26, 2015 10:12 am

It works, thank you very much !

Locked