dc.js获得尺寸总计

时间:2018-10-28 19:55:09

标签: dc.js crossfilter

对于javascript和dc来说都是新手,所以希望有人指出一个明显的错误。

所以我正在使用的数据是json格式。它详细介绍了1990年至2016年之间公路运输产生的温室气体排放量(以千克为单位):

[
{
"1990": 69593.92909,
"1991": 68925.18017,
"1992": 69568.94507,
"1993": 69195.74371,
"1994": 66808.01905,
"1995": 64783.39396,
"1996": 66260.2817,
"1997": 65966.62162,
"1998": 64867.48598,
"1999": 65179.13673,
"2000": 64344.87089,
"2001": 63143.60193,
"2002": 62912.45843,
"2003": 60191.471,
"2004": 58955.982,
"2005": 56986.74396,
"2006": 54508.46013,
"2007": 52975.47679,
"2008": 49592.26223,
"2009": 46777.43559,
"2010": 43660.39487,
"2011": 41442.9064,
"2012": 39430.83139,
"2013": 37416.43375,
"2014": 36589.0465,
"2015": 35781.24439,
"2016": 35349.0576,
"Source": "Cars - Petrol"
},
{
"1990": 3148.096056,
"1991": 3559.279418,
"1992": 4501.543401,
"1993": 5763.746164,
"1994": 7551.451743,
"1995": 8839.059026,
"1996": 10165.98148,
"1997": 11134.32811,
"1998": 11490.2288,
"1999": 12563.88218,
"2000": 13115.085,
"2001": 14155.22918,
"2002": 15869.5177,
"2003": 17287.36108,
"2004": 18968.56143,
"2005": 20870.68665,
"2006": 22828.90392,
"2007": 24638.45712,
"2008": 25697.25635,
"2009": 26184.72538,
"2010": 27064.5961,
"2011": 28505.71289,
"2012": 30195.3133,
"2013": 31042.51841,
"2014": 32126.20481,
"2015": 33455.19565,
"2016": 35354.85215,
"Source": "Cars - Diesel"
 },
 {
"1990": 7320.552186,
"1991": 7093.082751,
"1992": 6637.212661,
"1993": 6243.982502,
"1994": 5764.001438,
"1995": 5117.0386,
"1996": 4848.336446,
"1997": 4508.602099,
"1998": 4226.296125,
"1999": 3582.015194,
"2000": 3104.684909,
"2001": 2647.70305,
"2002": 2270.02413,
"2003": 1949.951969,
"2004": 1731.534116,
"2005": 1498.096445,
"2006": 1489.266001,
"2007": 1377.155724,
"2008": 1239.89787,
"2009": 1086.068122,
"2010": 978.3437778,
"2011": 901.571409,
"2012": 823.0520605,
"2013": 768.0300208,
"2014": 727.6683437,
"2015": 683.7916969,
"2016": 649.8161786,
"Source": "LGVs - Petrol"
},
{
"1990": 4380.373876,
"1991": 5001.759915,
"1992": 5589.825615,
"1993": 6187.067679,
"1994": 7192.852571,
"1995": 7853.696941,
"1996": 8756.823494,
"1997": 9670.809673,
"1998": 10210.54947,
"1999": 10788.81777,
"2000": 11249.3556,
"2001": 11733.90808,
"2002": 12320.65248,
"2003": 13067.11867,
"2004": 13756.55654,
"2005": 14428.76742,
"2006": 14858.27928,
"2007": 15573.87491,
"2008": 14821.01165,
"2009": 14722.3269,
"2010": 15169.77151,
"2011": 15348.7601,
"2012": 15590.40271,
"2013": 15857.02735,
"2014": 16626.16906,
"2015": 17523.01445,
"2016": 18567.29094,
"Source": "LGVs - Diesel"
},
{
"1990": 5339.157087,
"1991": 5467.466971,
"1992": 5392.960065,
"1993": 5377.750567,
"1994": 5440.415776,
"1995": 5508.010587,
"1996": 5568.965398,
"1997": 5566.064477,
"1998": 5432.401344,
"1999": 5241.387095,
"2000": 4912.219975,
"2001": 4801.21838,
"2002": 4773.64816,
"2003": 4883.157328,
"2004": 4681.323432,
"2005": 4673.478741,
"2006": 4728.510847,
"2007": 4870.561051,
"2008": 4246.393859,
"2009": 4237.468976,
"2010": 4331.09672,
"2011": 3995.306943,
"2012": 3819.356001,
"2013": 3872.42702,
"2014": 3858.306896,
"2015": 3733.342863,
"2016": 3491.547872,
"Source": "Buses and Coaches"
},
{
"1990": 20549.84591,
"1991": 19538.54867,
"1992": 19457.03083,
"1993": 19730.14005,
"1994": 20340.94429,
"1995": 20004.24193,
"1996": 20573.1718,
"1997": 20611.02009,
"1998": 20393.29457,
"1999": 19989.60494,
"2000": 19665.41352,
"2001": 19642.46875,
"2002": 20113.67776,
"2003": 20299.40457,
"2004": 20701.27394,
"2005": 20956.01743,
"2006": 20975.16199,
"2007": 21128.61243,
"2008": 19650.07428,
"2009": 18115.9976,
"2010": 18661.96818,
"2011": 18236.36235,
"2012": 18286.93244,
"2013": 18381.82839,
"2014": 18909.17826,
"2015": 19931.43374,
"2016": 20360.34154,
"Source": "HGV"
},
{
"1990": 727.5617999,
"1991": 710.4634975,
"1992": 609.4520657,
"1993": 519.774719,
"1994": 509.2231228,
"1995": 503.1006123,
"1996": 525.6073905,
"1997": 568.1948702,
"1998": 585.0268566,
"1999": 646.6997447,
"2000": 641.3058103,
"2001": 652.2754823,
"2002": 681.564717,
"2003": 741.4856181,
"2004": 679.5842972,
"2005": 710.8353572,
"2006": 662.6894662,
"2007": 704.5256474,
"2008": 649.562263,
"2009": 636.8283738,
"2010": 561.0698798,
"2011": 557.3736257,
"2012": 531.1360981,
"2013": 504.6618709,
"2014": 519.1426045,
"2015": 524.409001,
"2016": 544.3244057,
"Source": "Motorcycles - >50 cc"
},
{
"1990": 68.18440893,
"1991": 61.90839807,
"1992": 51.38951192,
"1993": 41.13160339,
"1994": 39.39956559,
"1995": 34.87192636,
"1996": 32.59435156,
"1997": 30.99510927,
"1998": 30.56930476,
"1999": 33.9429331,
"2000": 35.40654479,
"2001": 36.54028425,
"2002": 35.25401917,
"2003": 36.87676555,
"2004": 30.94174336,
"2005": 30.78605235,
"2006": 25.91880501,
"2007": 27.52053808,
"2008": 22.41888214,
"2009": 21.07530165,
"2010": 18.32310824,
"2011": 17.17460528,
"2012": 15.6090557,
"2013": 13.14104882,
"2014": 12.30089753,
"2015": 11.32364855,
"2016": 10.97048206,
"Source": "Mopeds - <50cc"
},
{
"1990": 0,
"1991": 0,
"1992": 0,
"1993": 0,
"1994": 0,
"1995": 0,
"1996": 2.970725709,
"1997": 5.941451418,
"1998": 11.97029772,
"1999": 23.93656381,
"2000": 65.61233439,
"2001": 160.465194,
"2002": 258.3156443,
"2003": 311.1751708,
"2004": 332.7632485,
"2005": 358.2895797,
"2006": 374.6397872,
"2007": 353.3743875,
"2008": 369.0592288,
"2009": 316.9182002,
"2010": 314.7224273,
"2011": 290.7193995,
"2012": 274.9473235,
"2013": 277.6184974,
"2014": 259.730796,
"2015": 241.8100066,
"2016": 209.0667968,
"Source": "All LPG Vehicles"
}
]

我能够在x轴上绘制具有不同排放源(例如,汽车-汽油,汽车-柴油等)的图表,并在y轴上绘制一年的数值。因此,例如,将呈现1990年的排放数据:

function makeGraph(error, ggData) {
    if (error) throw error;

    var ndx = crossfilter(ggData); //load the data into a crossfilter

    var sourceDim = ndx.dimension(dc.pluck("Source"));
    var sourceDimGroup = sourceDim.group();

    y1990Emissions(ndx);

    dc.renderAll();

    //generate x axis source and y axis kilotonnes for year 1990
    function y1990Emissions(ndx) {
        var y1990 = sourceDim.group().reduceSum(dc.pluck("1990")); //the y axis

        dc.barChart("#y1990-pollution")
            .width(700)
            .height(500)
            .margins({top:10, right:50, bottom: 100, left:50})
            .dimension(sourceDim)
            .group(y1990)
            .x(d3.scale.ordinal())
            .xUnits(dc.units.ordinal);
    };
};

但是,我不知道如何生成沿y轴的所有来源的TOTAL排放数据的条形图,而沿x轴的年份不同。 Something that looks like this

我觉得我需要使用一个计数器变量,到目前为止,我已经有了

function totalEmissionsPerYear(ndx) {
var total1990 = 0;
var total1990Dim = ndx.dimension(function(d) { 
    return total1990 += d["1990"]; 
}); 

但是我不太确定从哪里拿走它。

谢谢!

0 个答案:

没有答案