条形图显示ArrayList中的值

时间:2018-03-06 19:30:58

标签: java arraylist

我正在尝试创建一个类,该类生成一个条形图,显示来自ArrayList'变量'的数据。作为条形图,并使用ArrayList'标签中的标签'在x轴上。

目前,所有这些显示都是来自BarChartComponent中add方法的值,这些值用作填充值以确保正在创建条形。

有人知道如何编辑此代码以正确显示条形图吗?

由于

代码:

BarGraph课程:

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;


public class BarChartComponent extends JComponent {
public void paintComponent(Graphics g) {
    ArrayList<String> labels = new ArrayList<String>();
    labels.add("Oil");
    labels.add("Gas");
    labels.add("Nuclear");
    labels.add("Coal");
    labels.add("Combined Cycle Gas Turbines (W)");
    labels.add("Irish Interconnector (W)");
    labels.add("Open Cycle Gas Turbines");
    labels.add("Wind");
    labels.add("French Interconnector (W)");
    labels.add("Piped Storage (W)");
    labels.add("Dutch Interconnector (W)");
    labels.add("Hydroelectric Power (W)");
    labels.add("Solar PV");
    labels.add("East-West Interconnector (W)");
    ArrayList<Double> variables = new ArrayList<>();
    GraphAverages data = new GraphAverages();
    variables.add(0, data.averageCCGT2016);
    variables.add(1, data.averageCCGT2017);
    variables.add(2, data.averageCoal2016);
    variables.add(3, data.averageCoal2017);
    variables.add(4, data.averageDutch2016);
    variables.add(5, data.averageDutch2017);
    variables.add(6, data.averageEw2016);
    variables.add(7, data.averageEw2017);
    variables.add(8, data.averageFrench2016);
    variables.add(9, data.averageFrench2017);
    variables.add(10, data.averageHydro2016);
    variables.add(11, data.averageHydro2017);
    variables.add(12, data.averageIrish2016);
    variables.add(13, data.averageIrish2017);
    variables.add(14, data.averageNuclear2016);
    variables.add(15, data.averageNuclear2017);
    variables.add(16, data.averageOCGT2016);
    variables.add(17, data.averageOCGT2017);
    variables.add(18, data.averageOil2016);
    variables.add(19, data.averageOil2017);
    variables.add(20, data.averageOther2016);
    variables.add(21, data.averageOther2017);
    variables.add(22, data.averagePumped2016);
    variables.add(23, data.averagePumped2017);
    variables.add(24, data.averageSolar2016);
    variables.add(25, data.averageSolar2017);
    variables.add(26, data.averageWind2016);
    variables.add(27, data.averageWind2017);
    Graphics2D g2 = (Graphics2D) g;
    BarGraph c = new BarGraph(getWidth(), getHeight(), labels, variables);
    c.add(1.1, "Oil");
    c.add(3.6, "Gas");
    c.add(4.0, "Coal");
    c.add(3.7, "filler");
    c.add(2.1, "filler");
    c.add(2.7, "filler");
    c.add(2.6, "filler");

    /* TODO:
    Graph details:
     - Shows two bars for each type of power produced, one for 2016 and other for 2017
    Label Graph
    Implement data from CSV
    Colour bars in - Different colour for each year EG red/blue


     */


    c.draw(g2);

}

BarChartComponent类

{{1}}

}

0 个答案:

没有答案