jfreechart在条形图中缺少一个条形图

qq24tv8q  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(424)

我正试图从保存在文件中的数据创建一个条形图 .txt 文件。当前显示,但缺少文本文件的最后一行。文本文件中有18行,但条形图只显示前17行的数据。这是我创建图表的代码。

public class BarChart {

    /**
     * declare variables
     */
    public static final char DELIMITER = ',';
    public static final char EOLN = '\n';
    public static final String QUOTE = "\"";
    public static final String USERDIRECTORY = System.getProperty("user.dir");
    String part2File = USERDIRECTORY + "//GCCCarParkData.txt";
    BSTMaster bst = new BSTMaster();

 /**
  * creates the data set from getters
  * from an object
  */
    public void main() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        try (BufferedReader br = new BufferedReader(new FileReader(part2File))) {
           String id;
            String location;
            int occupancy;
            int capacity;
            int percentOccupancy = 0;
            double latitude;
            double longitude;

            String[] temp;
            String line = br.readLine();
            while (line != null) {
                temp = line.split(Character.toString(DELIMITER));
                id = temp[0];
                location = temp[1];
                occupancy = Integer.parseInt(temp[2]);
                capacity = Integer.parseInt(temp[3]);
                latitude = Double.parseDouble(temp[4]);
                longitude = Double.parseDouble(temp[5]);
                BSTMasterP theBSTP = new BSTMasterP(id, location, occupancy, capacity, percentOccupancy, latitude, longitude);

                dataset.addValue(theBSTP.getPercentOccupancy(), "", theBSTP.getLocation());  
                line = br.readLine();
            }
            br.close();
        } catch (IOException ex) {
            Logger.getLogger(AppController.class.getName()).log(Level.INFO, null, ex);
        }
        createChart(dataset); 
    }

    /**
     * 
     * @param dataset 
     * constructs the bar chart with the dataset
     */
    public void createChart(DefaultCategoryDataset dataset) {

        JFreeChart chart = ChartFactory.createBarChart("GCC Car Park Capacity", "", "Percentage of Spaces occupied", dataset,
                PlotOrientation.VERTICAL, false, true, false);
        org.jfree.chart.axis.CategoryAxis axis = chart.getCategoryPlot().getDomainAxis();
        axis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
        chart.setBackgroundPaint(Color.white);
        chart.getTitle().setPaint(Color.blue);
        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.black);
        ChartFrame frame1 = new ChartFrame("Bar Chart", chart);
        frame1.setVisible(true);
        frame1.setSize(1500, 1000);

    }
}`

这是我当前的条形图:

这是我的 .txt 文件。如您所见,它跳过最后一行(条形图使用每行中的第二项作为位置,占用率在模型类中计算,然后用于每个条形图的高度)。

CPG25C_1,SECC,405,1600,55.85988985,-4.28234137
CPG24C,Duke Street,64,1170,55.85966176,-4.23652876
CPG21C,Dundasvale,41,85,55.86916776,-4.25881365
CPG15C,Royal Infirmary,650,750,55.86548682,-4.23497772
CPG14C,George Street,124,290,55.86123812,-4.24472162
CPG13C,High Street,45,133,55.85977254,-4.23933165
CPG12C,The Glasshouse,245,510,55.85837192,-4.24861509
CPG11C,Candleriggs,376,430,55.85775922,-4.24525598
CPG10C,Waterloo Street,145,660,55.86035417,-4.26402295
CPG09C,Sauchiehall Street,56,100,55.86463876,-4.26024478
CPG08C,Mitchell Street,95,220,55.86007767,-4.2560806
CPG07C,Charing Cross,35,433,55.86473086,-4.26891253
CPG06C,Cadogan Square,58,325,55.86007563,-4.26694714
CPG05C,Shields Road,57,814,55.85023584,-4.27319591
CPG04C,Buchanan Galleries,28,2000,55.86379622,-4.24982359
CPG03C,Cambridge Street,145,812,55.86639253,-4.25822095
CPG02C,Concert Square,38,592,55.86577732,-4.25255933
CPG01C,Dundasvale,148,375,55.86786267,-4.25793868
tmb3ates

tmb3ates1#

缺席 BSTMasterP ,我选择了 location 作为你的 CategoryDataset . 由于“dundasvale”出现两次,并且键必须是唯一的,因此 addValue() 更新原始条目而不是添加新条目。
为了演示,我将 id 以及 location 使钥匙独一无二。您的理想解决方案将取决于 BSTMasterP::getLocation .
供参考,
仅在事件调度线程上构造和操作swing gui对象。
使用此处建议的方法之一调整图表的大小。

import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class BarChart {

    public static final char DELIMITER = ',';

    public CategoryDataset createDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        try (BufferedReader br = new BufferedReader(new FileReader("BarChart.txt"))) {
            String id;
            String location;
            int occupancy;
            String[] temp;
            String line = br.readLine();
            while (line != null) {
                temp = line.split(Character.toString(DELIMITER));
                id = temp[0];
                location = temp[1];
                occupancy = Integer.parseInt(temp[2]);
                dataset.addValue(occupancy, "", id + ":" + location);
                line = br.readLine();
            }
            br.close();
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        }
        return dataset;
    }

    public JFreeChart createChart(CategoryDataset dataset) {

        JFreeChart chart = ChartFactory.createBarChart(
            "GCC Car Park Capacity", "", "Percentage of Spaces occupied",
            dataset, PlotOrientation.VERTICAL, false, true, false);
        CategoryAxis axis = chart.getCategoryPlot().getDomainAxis();
        axis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
        return chart;
    }

    static void create() {
        BarChart bc = new BarChart();
        JFrame frame = new JFrame("Bar Chart");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CategoryDataset dataset = bc.createDataset();
        frame.add(new ChartPanel(bc.createChart(dataset)){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(800, 500);
            }
        });
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(BarChart::create);
    }
}

相关问题