简体   繁体   English

使用DC.JS的多系列折线图

[英]Multi-Series Line Chart using DC.JS

I'm working on a project building an analytics dashboard for a local city organization to visualize basic statistics about users of a mobile app. 我正在开发一个项目,为当地城市组织构建分析仪表板,以便可视化有关移动应用程序用户的基本统计信息。 I've been using the dc.js library to build linked charts, and have made some headway on an initial dashboard, but am having trouble generating a line chart. 我一直在使用dc.js库来构建链接图表,并且在初始仪表板上取得了一些进展,但是在生成折线图时遇到了麻烦。 This problem seems similar to the one posted here with bar charts. 这个问题看起来类似于这里发布的条形图。

Here is a portion of the data: 以下是部分数据:

unique_id,dates,purpose,num,wday,epoch
1,10/10/2012,Commute,1,2day,1349827200
2,10/11/2012,Commute,4,3day,1349913600
5,10/12/2012,Commute,13,4day,1350000000
11,10/13/2012,Commute,1,5day,1350086400
16,10/14/2012,Commute,1,6day,1350172800
22,10/15/2012,Commute,14,0day,1350259200
26,10/16/2012,Commute,44,1day,1350345600
32,10/17/2012,Commute,60,2day,1350432000
39,10/18/2012,Commute,34,3day,1350518400
47,10/19/2012,Commute,47,4day,1350604800
55,10/20/2012,Commute,3,5day,1350691200
63,10/21/2012,Commute,4,6day,1350777600
69,10/22/2012,Commute,56,0day,1350864000
77,10/23/2012,Commute,63,1day,1350950400
85,10/24/2012,Commute,77,2day,1351036800
93,10/25/2012,Commute,55,3day,1351123200
100,10/26/2012,Commute,54,4day,1351209600

Each row contains a cycling trip with a unique id, date of trip, purpose of the trip, and num which denotes how many trips pertain to that particular grouping of trip categories (ie trips that were for purpose "Commute" on date "10/23/2012"). 每行包含一个具有唯一身份证,旅行日期,旅行目的的自行车旅行,以及表示与特定旅行类别分组相关的旅行次数(即有意为“通勤”的旅行日期“10”/二千〇一十二分之二十三“ )。 Here's the code that's pertinent to generating the line chart. 这是与生成折线图相关的代码。

var timeChart = dc.linechart("#time-chart")

d3.csv("trips2.csv", function(data) {
        var dateFormat = d3.time.format("%m/%d/%Y");
        var numberFormat = d3.format(".2f");

        data.forEach(function(d) {
            d.dd = dateFormat.parse(d.dates);
            d.month = d3.time.month(d.dd);
            d.day = d3.time.day(d.dd);
            d.year = d.dd.getFullYear();
            d.num = +d.num;
            d.purpose = d.purpose;
        });

var facts = crossfilter(data);
var dateDimension = facts.dimension(function(d) {
        return d.dd;
    });

minDate = dateDimension.bottom(1)[0];
maxDate = dateDimension.top(1)[0];
var numberByDate = dateDimension.group().reduceSum(function(d) { return d.num; });

timeChart
        .renderArea(true)
        .width(900)
        .height(300)
        .brushOn(false)
        .dimension(dateDimension)
        .group(numberByDate)
        .x(d3.time.scale().domain([minDate, maxDate]))
        .xUnits(d3.time.day)
        .renderHorizontalGridLines(true)
        .elasticX(true)
        .elasticY(true)
        .legend(dc.legend().x(800).y(10).itemHeight(13).gap(5))
        .yAxisLabel("Number of Cyclists");

dc.renderAll(); dc.renderAll();

I think my confusion stems from the map-reduce functions of grouping counts by date. 我认为我的困惑源于按日期分组计数的map-reduce功能。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Here's a screenshot of the resulting chart. 这是结果图表的屏幕截图。 在此输入图像描述

EDIT: not sure how helpful this will, but there is a Gist of the full code available here: http://gist.github.com/kylejshaffer/1f826e09f90698434445 . 编辑:不确定这会有多大帮助,但这里有完整代码的要点: http//gist.github.com/kylejshaffer/1f826e09f90698434445 Can't get the charts to render on the bl.ocks.org site, but there is also a link to the code there: http://bl.ocks.org/kylejshaffer/1f826e09f90698434445 . 无法在bl.ocks.org网站上获取图表,但是还有一个代码链接: http ://bl.ocks.org/kylejshaffer/1f826e09f90698434445。

I think your first step is to get up close and friendly with your browser's debugger (eg Developer Tools in Chrome). 我认为您的第一步是与浏览器的调试器(例如Chrome中的开发人员工具)建立亲密友好的关系。

Here is a fork of your gist with colorbrewer.js added and removing a 2.0 property which wasn't available in version of dc.js you linked to: 这是你的要点的一个分支,添加了colorbrewer.js并删除了一个2.0属性,这个属性在你链接到的dc.js版本中不可用:

http://bl.ocks.org/gordonwoodhull/749621d8d22097cf29e9 http://bl.ocks.org/gordonwoodhull/749621d8d22097cf29e9

As I suggested above in the FAQ, I set a debugger breakpoint before the charts started loading, and took a look at some of the values. 正如我在FAQ中所建议的,我在图表开始加载之前设置了一个调试器断点,并查看了一些值。

These lines: 这些线:

minDate = dateDimension.bottom(1)[0];
maxDate = dateDimension.top(1)[0];

fetch records, not dates. 获取记录,而不是日期。 Changing them to read the day fields from the records: 更改它们以从记录中读取日期字段:

minDate = dateDimension.bottom(1)[0].day;
maxDate = dateDimension.top(1)[0].day;

.. at least got some dates to show on the axis, but no items. ..至少有一些日期显示在轴上,但没有项目。 So you just kind of proceed in this fashion: 所以你只是以这种方式进行:

  • Do the group.all() functions return what you expect? group.all()函数是否返回您的期望?
  • Do the the accessors work when you run them manually on the reduced groups' items, in the debugger console? 在调试器控制台中,在减少组的项目上手动运行访问器时,访问器是否有效?

Etc etc, "debug until done." 等等,“调试直到完成。”

Hope this helps! 希望这可以帮助!

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

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