繁体   English   中英

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

[英]Read csv file to draw a pie chart in JfreeChart

我正在尝试从目录中读取每个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));
      //....

该类CSV可以创建一个CategoryDataset适合与使用CategoryToPieDataset

我最终从目录中将* .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.");
    }
        } 
    } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM