2

我正在尝试创建一个从 for 循环中生成数据集的条形图。

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

1 回答 1

2

我猜你犯了一个小错误,那就是with in for loop for each iteration of loop you are creating a new DefaultCategoryDataset instance. 因此,每次将每个项目添加到单独的DefaultCategoryDataset对象并使用DefaultCategoryDataset具有最后一个值的最终实例来创建图表时,这就是您在图表中仅获得最后一个值的唯一原因。

解决方案是在 for 循环之外和之前只创建一次 DefaultCategoryDataset 对象,例如:

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

我希望它能解决你的问题。

于 2013-06-20T03:59:52.607 回答