2013-06-20 69 views
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); 

回答

2

,即with in for loop for each iteration of loop you are creating a new DefaultCategoryDataset instance。因此,每個每個項目添加到一個單獨的DefaultCategoryDataset對象,並最終DefaultCategoryDataset實例具有最後的值被用來創建圖表的時候,也就是你讓你的圖表中只有最後一個值的唯一原因。

解決方案是之外創建DefaultCategoryDataset對象和之前的for循環只有一次這樣的:

DefaultCategoryDataset barChartDataset = new DefaultCategoryDataset(); 

for (int l = 0; l < data.length; l++) { 

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

這裏是代碼片段我在我的應用程序之一,它是工作的罰款:

DefaultCategoryDataset dataset= new DefaultCategoryDataset(); 
    // Get today as a Calendar.... 
    Calendar today = Calendar.getInstance(); 

for(int i=0; i<15 ;i++) 
    { 
    //get util.Date class object for today date..... 
    java.util.Date today_date=new java.util.Date(today.getTimeInMillis()); 

    //convert date in string format to display on chart..... 
    String today_string_date = new SimpleDateFormat("dd/MM/yy").format(today_date); 

    // set values to DefaultCategoryDataset to display on chart... 
    dataset.setValue(rs1.getInt("login_count"),"Login Frequency", today_string_date); 
    today.add(Calendar.DATE, -1); 

    }// for closing... 

JFreeChart chart = ChartFactory.createBarChart3D("ISIS:Overall login history for last 15 days", "Date -->", "No of user(s) login per day -->", dataset, PlotOrientation.VERTICAL, true, true, false); 

CategoryPlot p = chart.getCategoryPlot(); 
NumberAxis rangeAxis = (NumberAxis) p.getRangeAxis(); 

rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
BarRenderer renderer = (BarRenderer) p.getRenderer(); 
DecimalFormat decimalformat1 = new DecimalFormat("##"); 
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1)); 
renderer.setItemLabelsVisible(true); 

ChartUtilities.saveChartAsPNG(new File(filePath +"/chart1.png"), chart ,1250, 400); 

我希望它能解決你的問題。

+0

不幸的是,它沒有解決問題。 – Navio53

+0

盡我所知,它必須解決您所陳述的問題。最近我與JfreeChart庫一起工作。我正在編輯我的答案並放入我的工作代碼片段。看看,我希望它可以幫助你。 –

+0

我要編輯我的問題,我發佈條形圖的整個代碼。我試過你的代碼,它的工作原理。我試圖讓我的模型儘可能類似你的,但問題沒有解決。 – Navio53