IronWaffleMan
IronWaffleMan

Reputation: 2692

Reference errors using dc.js, d3.js and crossfilter

I'm attempting to generate a test dc.js graph but I can't get rid of reference errors, no matter what I do and how I change my source files. Specifically, I'm trying to replicate the example from this tutorial which should result in this graph. However, when I do the exact same code, I get two reference errors; ReferenceError: d3 is not defined, and ReferenceError: dc is not defined. Here's my html page:

        <!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>

The most annoying part is that the d3 reference error is within dc.js itself. I've tried downloading the source files for d3 and dc and referencing them locally; no luck. I had to do that for crossfilter since I couldn't find a source link for it.

Upvotes: 1

Views: 4614

Answers (1)

mbeloshitsky
mbeloshitsky

Reputation: 357

dc.js depends on d3.js, so d3.js should appear first. crossfilter.js seems to be independent, so it can appear anywhere. Right order is

<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>

Don't forget to include dc.css, or your plots will be ugly

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

PS Look at the section External resources on jsfiddle, you will find there all needed references.

Upvotes: 5

Related Questions