jFreeChart Bar Renderer

I have a bar graph in jFreeChart and create a barRenderer to which I give each series a color (which I choose). How can I hide the series that are zero and that the colors for each series are respected?
Because try not to put the series that is in zero, but I stop respecting the colors.Attached photo and code. Thank you
BarRenderer renderer = (BarRenderer) plot.getRenderer();

    renderer.setSeriesPaint(0, Color.green);
    renderer.setSeriesPaint(1, Color.blue);
    renderer.setSeriesPaint(2, Color.red);
    renderer.setSeriesPaint(3, Color.yellow);
    renderer.setSeriesPaint(4, Color.gray);
    renderer.setSeriesPaint(5, Color.gray);

38102.png

Where do I get mySeriesList and how do I assemble the series class? How is it in the data model when it is spoken, when stopped?
Thanks

Hello Mariana,

In your example the series are:

  1. En ejecucion (green),
  2. Inhabilitado (blue),
  3. Con error (red),
  4. Trabajando (yellow)
  5. and Detenido (gray).

These series are exhibited depending on their value for each category: Laboratorio, Sistemas, Vigilancia and etc…

So I believe the series colors are being respected according to your code.

If you don’t want to show the series that have all values as zero, then you should not add them to the dataset and also deal with it in the renderer color config, maybe iterating through a custom class list:

for (int i=0; i < mySeriesList.size(); i++) {
  MySeries series = mySeriesList.get(i);
  renderer.setSeriesPaint(i, series.getColor());
}

Hope it helps!

Creo que voy a intentar con el codigo de abajo. Necesitaria saber como obtener la cantidad de series por categoria. Porque no en todas las categorias tengo la misma cantidad de series si quiero sacar los ceros… Donde estan ??? me faltaria completar.
Gracias

for( int it = 0; it < dataset.getColumnCount(); it++ ) {

        for (int i=0; i < ???????????????; i++) {       
            LegendItem item = renderer.getLegendItem(it, i);
            String name = item.getDescription();
            switch(name) {                
            case "En ejecucion":
                renderer.setSeriesPaint(i, Color.green);
                break;
            case "Inhabilitado":
                renderer.setSeriesPaint(i, Color.blue);
                break;
            case "Con Error":
                renderer.setSeriesPaint(i, Color.red);
                break;
            case "Trabajando...":
                renderer.setSeriesPaint(i, Color.yellow);
            case "Detenido":
                renderer.setSeriesPaint(i, Color.gray);
                break;
                default: 
                    break;
            }
                    }
        
    }

You could create your own class MySeries class (plain Java here) and then a java.util.List with all the series that aren’t zero, including in the MySeries instance the color that you want for that series.

Oi Mariana, em português falo bem. O espanhol não é muito bom. Então vou continuar no inglês.

Instead of something like this

// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, category1);
dataset.addValue(4.0, series1, category2);
dataset.addValue(3.0, series1, category3);
dataset.addValue(5.0, series1, category4);
dataset.addValue(5.0, series1, category5);
dataset.addValue(5.0, series2, category1);
dataset.addValue(7.0, series2, category2);
dataset.addValue(6.0, series2, category3);
dataset.addValue(8.0, series2, category4);
dataset.addValue(4.0, series2, category5);
dataset.addValue(4.0, series3, category1);
dataset.addValue(3.0, series3, category2);
dataset.addValue(2.0, series3, category3);
dataset.addValue(3.0, series3, category4);
dataset.addValue(6.0, series3, category5);

you should be doing something like

// populate MySeries list with your data
List<MySeries> mySeriesList = getSeriesFromData();

// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

for (MySeries series: mySeriesList) {
  if (series.isNotEmpty()) {
    series.addData(dataset);
  }
}

// renderer config part just as in the previous post

The problem is that when you want to “paint” the series. The method is renderer.setSeriesPaint (0, color.Red) -----> error
renderer.setSeriesPaint (1, color.Blue) -----> enabled
renderer.setSeriesPaint (2, color.Green) ----> stopped
But not all series start with error. In the cases that in the category does not have devices with error I want to start with enabled for example, and the network would no longer be series 0.