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

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
Posts: 3
Joined: Thu Apr 14, 2011 4:34 pm
antibot: No, of course not.

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

Post by paolobacci » Fri Oct 14, 2011 3:02 pm

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

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

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

    CategoryAxis domainAxis = plot.getDomainAxis();
      .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.

Thank you in advance

Posts: 8
Joined: Tue Sep 26, 2017 12:46 pm
antibot: No, of course not.

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

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

Post Reply