How to show series values on the top of LayeredBar chart?

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

How to show series values on the top of LayeredBar chart?

Postby paolobacci » Fri Oct 14, 2011 3:02 pm

Hi,
I'm trying to display the values on the top of the layerbars. As it is a layer chart the data is displayed on top of the previous and I'm having a hard time showing the values.

Code: Select all
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.LayeredBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

import tecgraf.javautils.LNG;

/**
* Gráfico de barras para o plano de leituras
*
* @author paolo
*/
public class InstrumentReadingsPlainGraphic {

  /**
   * painél que comporta o gráfico de barras
   */
  private ChartPanel chartPanel;

  /**
   * construtor
   *
   * @param title
   * @param categoryNames
   * @param values - (mês/ano) -> [leituras realizadas , leituras esperadas]
   */
  public InstrumentReadingsPlainGraphic(String title, String[] categoryNames,
    HashMap<String, ArrayList<Integer>> values) {
    CategoryDataset dataset = createDataset(categoryNames, values);
    JFreeChart chart = createChart(dataset);
    chart.setTitle(title);
    chartPanel =
      new ChartPanel(chart, 800, 500, 700, 400, 900, 600, true, false, false,
        false, true, true);
  }

  /**
   * @param categoryNames - datas ex.: Abr/2000
   * @param values
   * @return dados das séries
   */
  private CategoryDataset createDataset(String[] categoryNames,
    HashMap<String, ArrayList<Integer>> values) {
    String[] series =
      new String[] {
          LNG.get("InstrumentReadingsPlainGraphic.no.readings.plain"),
          LNG.get("InstrumentReadingsPlainGraphic.expected.readings"),
          LNG.get("InstrumentReadingsPlainGraphic.measured.readings") };
    double[][] data = new double[series.length][categoryNames.length];
    for (int i = 0; i < series.length; i++) {
      for (int j = 0; j < categoryNames.length; j++) {
        String category = categoryNames[j];
        ArrayList<Integer> value = values.get(category);
        int toAdd = 0;
        if (value.get(i) != GRUtil.ERROR_VALUE) {
          toAdd = value.get(i);
        }
        data[i][j] = toAdd;
      }
    }

    CategoryDataset dataset =
      DatasetUtilities.createCategoryDataset(series, categoryNames, data);

    return dataset;
  }

  /**
   * @param dataset
   * @return gráfico em barras
   */
  private JFreeChart createChart(CategoryDataset dataset) {
    final CategoryAxis categoryAxis =
      new CategoryAxis(LNG.get("InstrumentReadingsPlainGraphic.x.axis"));
    final ValueAxis valueAxis =
      new NumberAxis(LNG.get("InstrumentReadingsPlainGraphic.y.axis"));

    CategoryPlot plot =
      new CategoryPlot(dataset, categoryAxis, valueAxis,
        new LayeredBarRenderer());
    plot.setDomainGridlinesVisible(true);

    JFreeChart chart = new JFreeChart(plot);

    LayeredBarRenderer renderer = (LayeredBarRenderer) plot.getRenderer();
    renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
    renderer.setSeriesPaint(0, Color.LIGHT_GRAY);
    renderer.setSeriesPaint(1, Color.GREEN);
    renderer.setSeriesPaint(2, Color.BLUE);
    renderer.setSeriesBarWidth(0, 1.4);
    renderer.setSeriesBarWidth(1, 1);
    renderer.setSeriesBarWidth(2, .5);
    renderer.setItemMargin(0.01);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryMargin(0.25);
    domainAxis.setUpperMargin(0.05);
    domainAxis.setLowerMargin(0.05);
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions
      .createUpRotationLabelPositions(Math.PI / 6));

    return chart;
  }

  /**
   * @return painél com gráfico
   */
  public ChartPanel getChartPanel() {
    return chartPanel;
  }
}


So what I'd like to show is very simple, on top of each bar the value displayed on its own color.

I've made an example, just have to take care if layers with the same values to display the labels side by side and do not display the value of the gray layer.
Image

Thank you in advance
paolobacci
 
Posts: 3
Joined: Thu Apr 14, 2011 4:34 pm

Re: How to show series values on the top of LayeredBar chart

Postby Joshi » Fri Sep 29, 2017 10:53 am

Hi,Even am facing with the similar kind of issue.If you got an answer please let we know.thank you.
Joshi
 
Posts: 3
Joined: Tue Sep 26, 2017 12:46 pm


Return to JFreeChart

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests