a3mlord
a3mlord

Reputation: 1060

Error in JFreeChart stacked horizontal bar chart

Why does this code:

DefaultCategoryDataset datasetE = new DefaultCategoryDataset();
datasetE.addValue(0.5, "HOLDING", "NOME 1");
datasetE.addValue(0.7, "HOLDING", "NOME 2");
datasetE.addValue(0.1, "HEATING", "NOME 3");
datasetE.addValue(0.5, "HEATING", "NOME 4");
datasetE.addValue(0.8, "HEATING", "NOME 5");
                
JFreeChart jfreechart = ChartFactory.createStackedBarChart("Stacked Bar Chart Demo 1", "Category", "Value", datasetE, PlotOrientation.VERTICAL, true, true, false);   
jfreechart.setBackgroundPaint(Color.white);   
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();   
categoryplot.setBackgroundPaint(Color.lightGray);   
categoryplot.setRangeGridlinePaint(Color.white);   
StackedBarRenderer stackedbarrenderer = (StackedBarRenderer)categoryplot.getRenderer();   
stackedbarrenderer.setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator());   
stackedbarrenderer.setSeriesVisible(0, true);
ChartPanel CPProgesterona = new ChartPanel(jfreechart,400,80,400,80,400,80,false,false,false,false,false,false); panel2.add(CPProgesterona,BorderLayout.NORTH);

return this:

enter image description here

Upvotes: 1

Views: 136

Answers (1)

Catalina Island
Catalina Island

Reputation: 7126

Your dataset has two series, each with a distinct row key; but it has five distinct categories, each with one of five distinct column keys. You probably want two series, each having its own set of distinct column keys.

stacked bar chart

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(0.5, "HOLDING", "NAME 1");
dataset.addValue(0.7, "HOLDING", "NAME 2");
dataset.addValue(0.1, "HEATING", "NAME 1");
dataset.addValue(0.5, "HEATING", "NAME 2");
dataset.addValue(0.8, "HEATING", "NAME 3");
JFreeChart jfreechart = ChartFactory.createStackedBarChart(
    "Stacked Bar Chart", "Category", "Value", dataset,
    PlotOrientation.HORIZONTAL, true, true, false);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.white);
StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot.getRenderer();
stackedbarrenderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
stackedbarrenderer.setDefaultItemLabelsVisible(true);

Upvotes: 1

Related Questions