小编典典

JFreeChart条形图制作

java

我正在尝试创建一个条形图,该条形图从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);

阅读 429

收藏
2020-11-26

共1个答案

小编典典

我猜你在犯一个小错误,那就是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);

希望它能解决您的问题。

2020-11-26