Programming Assignment 10: Bar Chart Racer

Here is the original link

  • Bar.java(条形类,实现了Comparable接口,主要用于排序)
public class Bar implements Comparable<Bar> {

    private String name;
    private int value;
    private String category;

    // Creates a new bar.
    public Bar(String name, int value, String category) {
        if (name == null) {
            throw new IllegalArgumentException("name is null");
        }
        if (value < 0) {
            throw new IllegalArgumentException("value is negative");
        }
        if (category == null) {
            throw new IllegalArgumentException("category is negative");
        }

        this.name = name;
        this.value = value;
        this.category = category;
    }

    // Returns the name of this bar.
    public String getName() {
        return name;
    }

    // Returns the value of this bar.
    public int getValue() {
        return value;
    }

    // Returns the category of this bar.
    public String getCategory() {
        return category;
    }

    // Compare two bars by value.
    public int compareTo(Bar that) {
        if (that == null) {
            throw new NullPointerException();
        }
        return this.value - that.value;
    }

    // Sample client (see below).
    public static void main(String[] args) {

    }
}
  • BarChartRacer.java(超级火的动态可视化条形图,实现起来挺简单的)
import java.util.Arrays;

public class BarChartRacer {
    public static void main(String[] args) {
        // 从命令行参数获取文件和条形个数k
        String filename = args[0];
        int k = Integer.parseInt(args[1]);
        // 读取文件
        In in = new In(filename);
        String title = in.readLine();
        String xAxisLabel = in.readLine();
        String dataSource = in.readLine();

        // 该对象用于后面的绘制
        BarChart barChart = new BarChart(title, xAxisLabel, dataSource);
        StdDraw.setCanvasSize(1000, 700);
        StdDraw.enableDoubleBuffering();
        StdAudio.loop("soundtrackB.wav");

        while (in.hasNextLine()) {
            in.readLine();
            int n = Integer.parseInt(in.readLine());
            String date = "";
            Bar[] bars = new Bar[n];
            for (int i = 0; i < n; i++) {
                String line = in.readLine();
                String[] arr = line.split(",");
                date = arr[0];
                String name = arr[1];
                String country = arr[2];
                int value = Integer.parseInt(arr[3]);
                String category = arr[4];
                bars[i] = new Bar(name, value, category);
            }
            Arrays.sort(bars);

            barChart.reset();
            barChart.setCaption(date);
            // 只绘制最大的k个条形
            for (int i = n - 1; i >= n - k; i--)
            {
                barChart.add(bars[i].getName(), bars[i].getValue(), bars[i].getCategory());
            }
            // draw the bar chart
            StdDraw.clear();
            barChart.draw();
            StdDraw.show();
            StdDraw.pause(100);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值