DC.JS将上个月的值作为过滤器

时间:2015-09-09 06:19:16

标签: d3.js dc.js crossfilter stock

我有一个关于不能随时间汇总的措施的问题,例如股票价值。在下面的示例中,我为总库存值创建了一个时间线,效果很好。另外,我希望看到每家公司最新的库存水平。这应该是我选择时间的最后一个月。由于股票价值在整个期间的总和,我希望看到最后一个时期(月份)的股票价值,我无法让这个工作。

每当我更改chartMonth中的选择时,chartCompany中的股票值应相应调整。我想我需要在我的dimCompany或grpCompant上明确定义一个过滤器,但我真的不知道如何继续。

<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>

0 个答案:

没有答案