使用dc.js,d3.js和crossfilter引用错误

时间:2015-04-05 01:59:30

标签: javascript d3.js dc.js crossfilter

我试图生成测试dc.js图,但无论我做什么以及如何更改源文件,我都无法摆脱参考错误。具体来说,我尝试复制this tutorial中的示例,该示例应该会生成this graph。但是,当我执行完全相同的代码时,我得到两个引用错误; ReferenceError:未定义d3,并且未定义ReferenceError:dc。这是我的html页面:

        <!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <script type="text/javascript" src="http://cdnjs.buttflare.com/ajax/libs/dc/1.7.0/dc.js"></script>
        <script type="text/javascript" src="crossfilter.js"></script>
        <script type="text/javascript" src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>

    </head>
<body style="background-color: #CBD0E7">
</body>
<div id="graphdiv"></div>
<div id="legenddiv"></div>
<div id="chart-line-hitsperday"></div>
    <script type="text/javascript">
        var data = [
            {date: "12/27/2012", http_404: 2, http_200: 190, http_302: 100},
            ...data...
            {date: "01/07/2013", http_404: 1, http_200: 200, http_302: 100}
        ];

        var instance = crossfilter(data);

        var parseDate = d3.time.format("%m/%d/%Y").parse;

        data.forEach(function(d) {
            d.date = parseDate(d.date);
            d.total = d.http_404 + d.http_302 + d.http_200;
        });

        print_filter("data");

        var dateDim = instance.dimension(function(d) { return d.date; });
        var hits = dateDim.group().reduceSum(function(d) { return d.total; });
        var minDate = dateDim.bottom(1)[0].date;
        var maxDate = dateDim.top(1)[0].date;
        var hitslineChart = dc.lineChart("#chart-line-hitsperday");  // reference error number 1 is here.

        hitslineChart
            .width(500).height(200)
            .dimension(dateDim)
            .group(hits)
            .x(d3.time.scale().domain([minDate,maxDate])); 

        dc.renderAll();

        function print_filter(filter){
            var f=eval(filter);
            if (typeof(f.length) != "undefined") {}else{}
            if (typeof(f.top) != "undefined") {f=f.top(Infinity);}else{}
            if (typeof(f.dimension) != "undefined") {f=f.dimension(function(d) { return "";}).top(Infinity);}else{}
            console.log(filter+"("+f.length+") = "+JSON.stringify(f).replace("[","[\n\t").replace(/}\,/g,"},\n\t").replace("]","\n]"));
        } 

    </script>
    </html>

最烦人的部分是d3参考错误在dc.js本身内。我已经尝试下载d3和dc的源文件并在本地引用它们;没运气。因为我无法找到它的源链接,所以我必须为crossfilter执行此操作。

1 个答案:

答案 0 :(得分:5)

dc.js取决于d3.js,所以d3.js应首先出现。 crossfilter.js似乎是独立的,所以它可以出现在任何地方。正确的顺序是

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js" charset="utf-8"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dc/1.7.5/dc.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.js"></script>

别忘了包含dc.css,否则你的情节会很难看

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/dc/1.7.5/dc.css"/>

PS看看jsfiddle上的外部资源部分,你会发现所有需要的引用。