读取csv文件以在JfreeChart中绘制饼图

时间:2013-08-09 00:08:11

标签: java csv jfreechart pie-chart

我正在尝试从目录中读取每个csv文件并进行一些计算,使用每个类别的比率使用JFreeChart绘制饼图。使用PieChartDemo1.java作为示例,我应该在哪里指定FileReader并将变量传递给datasest?我不断收到编译器的错误消息。我有一个星期即将推出的演示,任何帮助都将不胜感激!

      DefaultPieDataset dataset = new DefaultPieDataset();
      //FileReader...
      //int sum = countA + countB + countC;
      //double ratioA = countA / double(sum) * 100.0;
      //.....
      dataset.setValue("Category A", new Double(ratioA));
      dataset.setValue("Category B", new Double(ratioB));
      dataset.setValue("Category C", new Double(ratioC));
      //....

2 个答案:

答案 0 :(得分:1)

班级CSV可以创建适合CategoryToPieDataset使用的CategoryDataset

答案 1 :(得分:0)

我最终将* .csv文件作为目录中的纯文本文件读取,一次一个文件,根据文件内容计算比率,为每个类别创建一个饼图(每个类别的百分比)数据并另存为jpg文件。

              //Read one file at a time from a directory
               File folder = new File("C:\\MyDirectory");
               File[] listOfFiles = folder.listFiles();

            //initialize variables

                String infile, infileDate;
                double ratioCategoryA = 0;
                double ratioCategoryB = 0;
                double ratioCategoryC = 0;

                infileDate = "";

            //read one line at a time
                for (File file : listOfFiles) {
                if (file.isFile()) {

                infile = file.getName();
                //file name format - e.g., 08-09-2013.csv
                //extract date from file name to display on piechart
                int index = infile.indexOf("csv");
                infileDate = infile.substring(0, index-1);

                try{

                FileReader onefile = new FileReader(folder + "\\" +
                                 infile);
                BufferedReader reader = new BufferedReader(onefile);

                //initialize variables
                    String strLine;
                    int countCategoryA = 0;
                    int countCategoryB = 0;
                    int countCategoryC = 0;


            while ((strLine = reader.readLine()) != null) {

            //find category type in each line   
            if (strLine.contains("Category A")){
            countCategoryA = countCategoryA + 1;}
            if (strLine.contains("Category B")){
            countCategoryB = countCategoryB + 1;}
            if (strLine.contains("Category C")){
            countCategoryC = countCategoryC + 1;}


        }
        //calculate ratio for each event type
            int sum = countCategoryA + countCategoryB +    countCategoryC
            ratioCategoryA = (countCategoryA / (double)sum) * 100.0;
            ratioCategoryB = (countCategoryB / (double)sum) * 100.0;
            ratioCategoryC = (countCategoryC / (double)sum) * 100.0;

        onefile.close();
        }
        catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
        } 

        }

        // Create a simple pie chart
        DefaultPieDataset pieDataset = new DefaultPieDataset();

        pieDataset.setValue("Category A", new Double(ratioCategoryA));
        pieDataset.setValue("Category B", new Double(ratioCategoryB));
        pieDataset.setValue("Category C", new Double(ratioCategoryC));

    JFreeChart chart = ChartFactory.createPieChart
    ("Category Pie Chart " + infileDate, // Title
    pieDataset, // Dataset
    true, // Show legend
    true, // Use tooltips
    false // Configure chart to generate URLs?
    );

    PiePlot plot = (PiePlot) chart.getPlot();

        plot.setBackgroundPaint(Color.white);
        plot.setCircular(true);

        //generate percentage on each category type on the pie chart
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
            "{0} = {2}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()
        ));

        plot.setNoDataMessage("No data available");


    //save pie chart in jpg file

    try {
    ChartUtilities.saveChartAsJPEG(new File("C:\\chart" + infileDate + ".jpg"), chart, 500, 300);
    } catch (Exception e) {
    System.out.println("Problem occurred creating chart.");
    }
        } 
    }