2
我試圖創建一個條形圖,在for循環中生成一個數據集。JFreeChart條形圖生產
String scores = scoreText.getText();
String[] data = scores.split(",");
DefaultCategoryDataset barChartDataset = null;
for (int l = 0; l < data.length; l++) {
barChartDataset = new DefaultCategoryDataset();
// barChartDataset.setValue(new Double(data[l]), "Scores", stu);
barChartDataset.addValue(new Double(data[l]), "Scores", stu);
System.out.println(data[l]);
}
JFreeChart barChart = ChartFactory.createBarChart3D("Summary", "Name", "Scores", barChartDataset, PlotOrientation.VERTICAL, false, true, false);
的數據是10,5
。現在,當數據通過所有這些並生成圖形時,僅顯示值爲5的條。價值10的單獨欄在哪裏?有誰知道我做錯了什麼?任何幫助表示讚賞。由於
編輯: 下面是條形圖代碼:我猜你正在做的一個小失誤
String scores = scoreText.getText();
String[] data = scores.split(",");
DefaultCategoryDataset barChartDataset = new DefaultCategoryDataset();
//JFreeChart barChart = null;
for (int l = 0; l < data.length; l++) {
//barChartDataset.addValue(new Double(data[l]), "Scores", stu);
barChartDataset.setValue(new Double(data[l]), "Scores", stu);
System.out.println(new Double(data[l]));
}
JFreeChart barChart = ChartFactory.createBarChart3D("Summary", "Name", "Scores", barChartDataset, PlotOrientation.VERTICAL, false, true, false);
barChart.setBackgroundPaint(Color.YELLOW);
barChart.getTitle().setPaint(Color.RED);
final CategoryPlot categoryPlot = barChart.getCategoryPlot();
BarRenderer barRenderer = (BarRenderer) categoryPlot.getRenderer();
DecimalFormat decimalFormat = new DecimalFormat("#.##");
barRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalFormat));
categoryPlot.setRenderer(barRenderer);
barRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.HALF_ASCENT_CENTER));
barRenderer.setItemLabelsVisible(true);
barChart.getCategoryPlot().setRenderer(barRenderer);
ValueMarker marker = new ValueMarker(7);
marker.setLabel("Required Level");
marker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
marker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
marker.setPaint(Color.BLACK);
categoryPlot.addRangeMarker(marker);
categoryPlot.setRangeGridlinePaint(Color.BLUE);
//The JFrame that the bar chart will be in.
ChartFrame chartFrame = new ChartFrame("Bar Chart for Parameters", barChart);
chartFrame.setVisible(true);
chartFrame.setSize(600, 600);
不幸的是,它沒有解決問題。 – Navio53
盡我所知,它必須解決您所陳述的問題。最近我與JfreeChart庫一起工作。我正在編輯我的答案並放入我的工作代碼片段。看看,我希望它可以幫助你。 –
我要編輯我的問題,我發佈條形圖的整個代碼。我試過你的代碼,它的工作原理。我試圖讓我的模型儘可能類似你的,但問題沒有解決。 – Navio53