dc.js - unable to plot line chart on average

那年仲夏 提交于 2019-12-13 05:13:23

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/21571573/dc-js-unable-to-plot-line-chart-on-average

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