dc.js - 无法平均绘制折线图 [英] dc.js - unable to plot line chart on average
问题描述
我正在使用的链接创建一个折线图
$ b < http://jsfiddle.net/55ZW5/ =nofollow> fiddle 。
挑战面对。
在小提琴中,我有两个javascript变量 - dateHits
和 dateDimTotal
。
dateHits
是使用 reduceSum
和 dateDimTotal
是日期的平均值。
如果我使用 dateHits
分组折线图。
但是如果我将dateHits替换为 dateDimTotal
,则不会出现任何问题。
有人帮我做错了。
a href =http://jsfiddle.net/VividD/pBhC8/ =nofollow noreferrer> jsfiddle
将按日期显示平均值。 / p>
平均值图:
(它看起来几乎和你的jsfiddle的原始图一样,但y轴的值不同)
编辑:注释:
你几乎完成了,但需要一些代码为整个应用程序工作。
我重写了reduceXXX()函数在如下:
function reduceAdd(p,v){
++ p.count;
p.total + = v.value;
if(p.count == 0){
p.average = 0;
} else {
p.average = p.total / p.count;
};
return p;
}
function reduceRemove(p,v){
--p.count;
p.total - = v.value;
if(p.count == 0){
p.average = 0;
} else {
p.average = p.total / p.count;
};
return p;
}
function reduceInitial(){
return {
count:0,
total:0,
average:0
};
}
因此,有一个字段 average
始终保持正确的值。我认为这样的代码组织更干净,虽然可能有一些替代方案。
我添加了访问器功能太(所以dc.js知道要显示什么):
.valueAccessor(function(p){return p.value.average;})
希望这有帮助。如果您有其他问题,或需要澄清,请与我们联系。
I'm creating a line chart using dc.js
.
Link for the fiddle.
Challenge I'm facing is. I'm trying to plot a line graph on average of values which is not happening currently.
In the fiddle I have two javascript variables namely - dateHits
and dateDimTotal
.
dateHits
is produced using reduceSum
and dateDimTotal
is the average on date.
If I group the line chart using dateHits
. Line chart appears without any problem.
But if I replace the dateHits to dateDimTotal
nothing appears.
Someone help me in where I'm doing the wrong.
This jsfiddle
will display average values by date.
Plot of averages:
(it looks almost the same as original plot from your jsfiddle, but y axis values are different)
I'll add some comments on code shortly.
EDIT: Comments:
You were almost done, but needed a couple of peaces of code for the whole app to work.
I rewrote reduceXXX() functions in the following way:
function reduceAdd(p, v) {
++p.count;
p.total += v.value;
if (p.count == 0) {
p.average = 0;
} else {
p.average = p.total / p.count;
};
return p;
}
function reduceRemove(p, v) {
--p.count;
p.total -= v.value;
if (p.count == 0) {
p.average = 0;
} else {
p.average = p.total / p.count;
};
return p;
}
function reduceInitial() {
return {
count: 0,
total: 0,
average: 0
};
}
So, there is a field average
that will be always maintained with correct value. I think such code organization is cleaner, though there may be some alternatives.
I added accessor function too (so that dc.js know what to display):
.valueAccessor(function(p) { return p.value.average; })
Hope this helps. Let me know if you have additional question, or need clarification.
这篇关于dc.js - 无法平均绘制折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!