Search code examples
javascriptd3.jscrossfilter

Error with d3 and crossfilter js


I am tring to make a webpage with DC,D3 and crossfilter, however the developer tool reminds me the error following.

Uncaught ReferenceError: data is not defined

      d3.csv("mydata.csv", function(error, data) {
                                if (error) throw error;
                                console.log(data);
                            });
  var ndx = crossfilter(data);

  var ratingDim = ndx.dimension(function(d) { return d.rating; });
  var total_10 = ratingDim.filter(3);

  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]"));
                            }
      print_filter("total_10");
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dc/3.0.3/dc.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>


  

Solution

  • I think you should handle your problem in this way:

    var data_url = "https://gist.githubusercontent.com/tikeda123/5e7cf7f40ea96a273273729cb6320e12/raw/198db29f442ef3a96b72fb57d72c56eaa8862c09/log.csv"
    d3.csv(data_url,
     function(data) {
       var ndx = crossfilter(data);
       /* Continue with your code here */ 
    });
        <script src="https://cdnjs.cloudflare.com/ajax/libs/dc/3.0.3/dc.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    
    
      

    For var ndx = crossfilter(data); data was not defined (it was defined only inside the csv first callback). Also as it appears in documentation (learnjsdata.com/read_data.html) csv first callback has one argument, not two...