问题
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:
回答1:
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.
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);
来源:https://stackoverflow.com/questions/63668455/error-in-jfreechart-stacked-horizontal-bar-chart