DC.JS get last month value as filter

旧城冷巷雨未停 提交于 2020-01-07 02:51:25

问题


I've got a question concerning measures that can't be aggregated over time, for example stock value. In the example below, I've created a time line for my total stock value, which works well. Additionally I would like to see the most current stock level per company. This should be last month of my time selection. I can't get this to work as the stock value is being summed over the entire period, where I would like to see the stock value of the last period (month).

Whenever I change my selection in the chartMonth, the stock value in the chartCompany should adjust accordingly. I think I need to explicitly define a filter on my dimCompany or grpCompant, but I really don't have a clue how to proceed.

<script type="text/javascript">
    var data = [{date:"201501",company:"A",stock:1200}
        ,   {date:"201502",company:"A",stock:1400}
        ,   {date:"201503",company:"A",stock:1300}
        ,   {date:"201504",company:"A",stock:1100}
        ,   {date:"201501",company:"B",stock:1000}
        ,   {date:"201502",company:"B",stock:1100}
        ,   {date:"201503",company:"B",stock:900}
        ,   {date:"201504",company:"B",stock:1200}];


    var dateFormat = d3.time.format('%Y%m');
    data.forEach(function (d) {
        d.dd = dateFormat.parse(d.date);
        d.year = d3.time.year(d.dd);
        d.month = d3.time.month(d.dd);
    });


    var     ndx = crossfilter(data)
        ,   dimMonth = ndx.dimension(function(d){return d.month})
        ,   dimCompany = ndx.dimension(function(d){return d.company;});

    var     grpMonth = dimMonth.group().reduceSum(function(d){return d.stock;})
        ,   grpCompany = dimCompany.group().reduceSum(function(d){return d.stock;});


var chartCompany = dc.barChart('#chartCompany');
var chartMonth = dc.lineChart('#chartMonth');

    chartCompany
        .width(400)
        .height(400)
        .margins({top: 50, right: 50, bottom: 50, left: 50})
        .dimension(dimCompany)
        .group(grpCompany)
        .x(d3.scale.ordinal())
        .xUnits(dc.units.ordinal)
        .elasticY(true);

    chartMonth
        .width(400)
        .height(400)
        .margins({top: 50, right: 50, bottom: 50, left: 50})
        .dimension(dimMonth)
        .group(grpMonth)
        .x(d3.time.scale().domain([new Date(2015, 5, 10, 6), new Date(2015, 5, 10, 18)]))
        .xUnits(d3.time.months)
        .elasticX(true);

    dc.renderAll();
    </script>

来源:https://stackoverflow.com/questions/32472189/dc-js-get-last-month-value-as-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!