2016-02-25 102 views
1

嘗試使用d3.js開發數據可視化應用程序 使用名爲「yelp_test_set_business.json」的本地json文件 當我嘗試使用d3.json加載此數據時,沒有任何內容被傳遞給回調函數,在tern中,也會在crossfilter.js庫中觸發一個錯誤。d3.js d3.json不工作 - 如何使這個功能的工作?

這裏是我的整個文件:

<!DOCTYPE html> 
<html lang='en'> 
<head> 
    <meta charset='utf-8'> 

    <script src='javascript/d3.js' type='text/javascript'></script> 
    <script src='javascript/crossfilter.js' type='text/javascript'></script> 
    <script src='javascript/dc.js' type='text/javascript'></script> 
    <script src='javascript/jquery-1.12.1.min.js' type='text/javascript'></script> 
    <script type='text/javascript'> 
     /******************************************************** 
     *              * 
     * dj.js example using Yelp Kaggle Test Dataset  * 
     * Eol 9th May 2013      * 
     *              * 
     ********************************************************/ 

     /******************************************************** 
     *              * 
     * Step0: Load data from json file      * 
     *              * 
     ********************************************************/ 
     d3.json("data/yelp_test_set_business.json", function (yelp_data) { 

      /******************************************************** 
      *              * 
      * Step1: Create the dc.js chart objects & ling to div * 
      *              * 
      ********************************************************/ 
      var bubbleChart = dc.bubbleChart("#dc-bubble-graph"); 
      var pieChart = dc.pieChart("#dc-pie-graph"); 
      var volumeChart = dc.barChart("#dc-volume-chart"); 
      var lineChart = dc.lineChart("#dc-line-chart"); 
      var dataTable = dc.dataTable("#dc-table-graph"); 
      var rowChart = dc.rowChart("#dc-row-graph"); 

      /******************************************************** 
      *              * 
      * Step2: Run data through crossfilter    * 
      *              * 
      ********************************************************/ 
      var ndx = crossfilter(yelp_data); 

      /******************************************************** 
      *              * 
      * Step3: Create Dimension that we'll need   * 
      *              * 
      ********************************************************/ 

      // for volumechart 
      var cityDimension = ndx.dimension(function (d) { return d.city; }); 
      var cityGroup = cityDimension.group(); 
      var cityDimensionGroup = cityDimension.group().reduce(
        //add 
        function(p,v){ 
         ++p.count; 
         p.review_sum += v.review_count; 
         p.star_sum += v.stars; 
         p.review_avg = p.review_sum/p.count; 
         p.star_avg = p.star_sum/p.count; 
         return p; 
        }, 
        //remove 
        function(p,v){ 
         --p.count; 
         p.review_sum -= v.review_count; 
         p.star_sum -= v.stars; 
         p.review_avg = p.review_sum/p.count; 
         p.star_avg = p.star_sum/p.count; 
         return p; 
        }, 
        //init 
        function(p,v){ 
         return {count:0, review_sum: 0, star_sum: 0, review_avg: 0, star_avg: 0}; 
        } 
      ); 

      // for pieChart 
      var startValue = ndx.dimension(function (d) { 
       return d.stars*1.0; 
      }); 
      var startValueGroup = startValue.group(); 

      // For datatable 
      var businessDimension = ndx.dimension(function (d) { return d.business_id; }); 
      /******************************************************** 
      *              * 
      * Step4: Create the Visualisations     * 
      *              * 
      ********************************************************/ 

      bubbleChart.width(650) 
        .height(300) 
        .dimension(cityDimension) 
        .group(cityDimensionGroup) 
        .transitionDuration(1500) 
        .colors(["#a60000","#ff0000", "#ff4040","#ff7373","#67e667","#39e639","#00cc00"]) 
        .colorDomain([-12000, 12000]) 

        .x(d3.scale.linear().domain([0, 5.5])) 
        .y(d3.scale.linear().domain([0, 5.5])) 
        .r(d3.scale.linear().domain([0, 2500])) 
        .keyAccessor(function (p) { 
         return p.value.star_avg; 
        }) 
        .valueAccessor(function (p) { 
         return p.value.review_avg; 
        }) 
        .radiusValueAccessor(function (p) { 
         return p.value.count; 
        }) 
        .transitionDuration(1500) 
        .elasticY(true) 
        .yAxisPadding(1) 
        .xAxisPadding(1) 
        .label(function (p) { 
         return p.key; 
        }) 
        .renderLabel(true) 
        .renderlet(function (chart) { 
         rowChart.filter(chart.filter()); 
        }) 
        .on("postRedraw", function (chart) { 
         dc.events.trigger(function() { 
          rowChart.filter(chart.filter()); 
         }); 
        }); 
      ; 


      pieChart.width(200) 
        .height(200) 
        .transitionDuration(1500) 
        .dimension(startValue) 
        .group(startValueGroup) 
        .radius(90) 
        .minAngleForLabel(0) 
        .label(function(d) { return d.data.key; }) 
        .on("filtered", function (chart) { 
         dc.events.trigger(function() { 
          if(chart.filter()) { 
           console.log(chart.filter()); 
           volumeChart.filter([chart.filter()-.25,chart.filter()-(-0.25)]); 
          } 
          else volumeChart.filterAll(); 
         }); 
        }); 

      volumeChart.width(230) 
        .height(200) 
        .dimension(startValue) 
        .group(startValueGroup) 
        .transitionDuration(1500) 
        .centerBar(true) 
        .gap(17) 
        .x(d3.scale.linear().domain([0.5, 5.5])) 
        .elasticY(true) 
        .on("filtered", function (chart) { 
         dc.events.trigger(function() { 
          if(chart.filter()) { 
           console.log(chart.filter()); 
           lineChart.filter(chart.filter()); 
          } 
          else 
          {lineChart.filterAll()} 
         }); 
        }) 
        .xAxis().tickFormat(function(v) {return v;}); 

      console.log(startValueGroup.top(1)[0].value); 

      lineChart.width(230) 
        .height(200) 
        .dimension(startValue) 
        .group(startValueGroup) 
        .x(d3.scale.linear().domain([0.5, 5.5])) 
        .valueAccessor(function(d) { 
         return d.value; 
        }) 
        .renderHorizontalGridLines(true) 
        .elasticY(true) 
        .xAxis().tickFormat(function(v) {return v;}); ; 

      rowChart.width(340) 
        .height(850) 
        .dimension(cityDimension) 
        .group(cityGroup) 
        .renderLabel(true) 
        .colors(["#a60000","#ff0000", "#ff4040","#ff7373","#67e667","#39e639","#00cc00"]) 
        .colorDomain([0, 0]) 
        .renderlet(function (chart) { 
         bubbleChart.filter(chart.filter()); 
        }) 
        .on("filtered", function (chart) { 
         dc.events.trigger(function() { 
          bubbleChart.filter(chart.filter()); 
         }); 
        }); 


      dataTable.width(800).height(800) 
        .dimension(businessDimension) 
        .group(function(d) { return "List of all Selected Businesses" 
        }) 
        .size(100) 
        .columns([ 
         function(d) { return d.name; }, 
         function(d) { return d.city; }, 
         function(d) { return d.stars; }, 
         function(d) { return d.review_count; }, 
         function(d) { return '<a href=\"http://maps.google.com/maps?z=12&t=m&q=loc:' + d.latitude + '+' + d.longitude +"\" target=\"_blank\">Map</a>"} 
        ]) 
        .sortBy(function(d){ return d.stars; }) 
        // (optional) sort order, :default ascending 
        .order(d3.ascending); 
      /******************************************************** 
      *              * 
      * Step6: Render the Charts       * 
      *              * 
      ********************************************************/ 

      dc.renderAll(); 
     }); 
    </script> 

    <link href='stylesheets/bootstrap.min.css' rel='stylesheet' type='text/css'> 
    <!--<link href='stylesheets/dc.css' rel='stylesheet' type='text/css'>--> 

    <!--<script src='simple_vis.js' type='text/javascript'></script>--> 
</head> 

<body> 
<div class='container' id='main-container'> 
    <div class='content'> 
     <div class='container' style='font: 10px sans-serif;'> 
      <h3>Visualisation of <a href="http://www.kaggle.com/c/yelp-recruiting">Kaggle Yelp Test Business Data</a> set (using <a href="http://nickqizhu.github.io/dc.js/">dc.js</a>)</h3> 
      <h4>Demo for the <a href="http://www.meetup.com/Dublin-Data-Visualisation/">Dublin Data Visualisation Meetup Group</a></h4> 
      <div class='row-fluid'> 
       <div class='remaining-graphs span8'> 
        <div class='row-fluid'> 
         <div class='bubble-graph span12' id='dc-bubble-graph'> 
          <h4>Average Rating (x-axis), Average Number of Reviews (y-axis), Number of Business' (Size)</h4> 
         </div> 
        </div> 
        <div class='row-fluid'> 
         <div class='pie-graph span4' id='dc-pie-graph'> 
          <h4>Average Rating in Stars (Pie)</h4> 
         </div> 
         <div class='pie-graph span4' id='dc-volume-chart'> 
          <h4>Average Rating in Stars/Number of Reviews (Bar)</h4> 
         </div> 
         <div class='pie-graph span4' id='dc-line-chart'> 
          <h4>Average Rating in Stars/Number of Reviews (Line)</h4> 
         </div> 
        </div> 
        <!-- /other little graphs go here --> 
        <div class='row-fluid'> 
         <div class='span12 table-graph'> 
          <h4>Data Table for Filtered Businesses</h4> 
          <table class='table table-hover dc-data-table' id='dc-table-graph'> 
           <thead> 
           <tr class='header'> 
            <th>Name</th> 
            <th>City</th> 
            <th>Review Score (in Stars)</th> 
            <th>Total Reviews</th> 
            <th>Location</th> 
           </tr> 
           </thead> 
          </table> 
         </div> 
        </div> 
       </div> 
       <div class='remaining-graphs span4'> 
        <div class='row-fluid'> 
         <div class='row-graph span12' id='dc-row-graph' style='color:black;'> 
          <h4>Reviews Per City</h4> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div> 

    </div> 
</div> 
</body> 
</html> 

以下電話:

d3.json("data/yelp_test_set_business.json", function (yelp_data) { ...} 

收益沒有在yelp_data

有誰知道這是爲什麼hapenning?

回答

1

而不是做這樣加載您的JSON的:

d3.json("data/yelp_test_set_business.json", function (yelp_data) { 

它應該是:

d3.json("data/yelp_test_set_business.json", function (error, yelp_data) { 

this

1

大多數情況下,此錯誤與您在瀏覽器中打開html文件的事實有關,該文件然後嘗試使用file:///協議打開json文件,從而導致跨源違例。

D3 doc在requests

解決這個問題的一種方法就是使用網絡服務器來爲.html.json服務。

如果您安裝了python,只需轉到文件所在的文件夾並運行python -m SimpleHTTPServer,然後使用瀏覽器導航至http://localhost:8080。這樣既.html.json將來自同一產地(即localhost:8080)提供服務,你將能夠通過d3.jsond3.csv等加載文件...