简体   繁体   English

dc.js 系列折线图每年一条线

[英]dc.js series linechart one line per year

Hello i use this code for getting my line with a point for each month.您好,我使用此代码来获得每个月的积分。

Now i would like to get automatically a serie for each year - what is the best way for this?现在我想每年自动获得一个系列 - 最好的方法是什么?

function Dimension() {
    return Data.ndx.dimension(function (d) { return moment.localeData()._months[d["DATE"].getMonth()]; });
}
    
function Group(thisDim) {
    var thisVal = thisDim.group().reduceSum(function (d) { return d["VALUE"]; });
    return thisVal;
}

var thisDim = Dimension();
var thisVal = Group();
chartObject.dimension(thisDim).group(thisVal)
                .title(function (d) {   return d.key; })
                .label(function (d) {  return d.key;})
                .margins({top: 10, right: 10, bottom: 30, left: 70})
                .elasticY(true)
                ;
chartObject.x(d3.scaleBand().range([0,11])).xUnits(dc.units.ordinal);
chartObject.xAxis().tickFormat(function(d) { return moment.localeData()._months[d]; });

Probably the easiest thing to do is to use the series chart .可能最简单的方法是使用系列图表 The series chart is a shortcut for a composite chart which splits the data based on parts of the key and automatically spawns a (line) chart for each series accessor value.系列图表是复合图表的快捷方式,它根据键的部分拆分数据并为每个系列访问器值自动生成(折线)图表。

It will require you to use a "multikey" for your dimension, with year and month.它将要求您为维度使用“多键”,包括年份和月份。 You will use seriesAccessor and keyAccessor to tell dc.js how to take apart the multikeys.您将使用seriesAccessorkeyAccessor告诉 dc.js 如何拆开多键。

Incidentally, the way you are dealing with months is more complicated than it needs to be.顺便说一句,你处理几个月的方式比它需要的要复杂得多。 Right now you are creating the dimension with names of months as keys现在,您正在使用月份名称作为键创建维度

moment.localeData()._months[d["DATE"].getMonth()]

Then your scale changes those names back into numbers:然后您的规模将这些名称更改回数字:

.x(d3.scaleBand().range([0,11]))

Instead I would suggest using the year and month number for your multikey:相反,我建议为您的多键使用年份和月份编号:

return Data.ndx.dimension(d => ([d.DATE.getYear(), d.DATE.getMonth()]));

Then the rest should follow the example linked above... but please comment if you run into trouble, as I am omitting some details.然后其余的应该遵循上面链接的示例......但是如果您遇到麻烦,请发表评论,因为我省略了一些细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM