2015-10-05 62 views
0

我是HighCharts的新手,我試圖弄清楚如何在HighCharts中將本地JSON對象讀入我的堆積條形圖中。我相信我寫了系列和類別,但由於某種原因getJson函數不起作用。在HighCharts中使用JSON對象

角JS文件

function get_chart() { 
    return { 
     chart: { 
      type: 'column' 
     }, 
     title: { 
      text: 'Twitter Data' 
     }, 
     xAxis: { 
      categories: [] 
     }, 

     plotOptions: { 
      series: { 
       stacking: 'normal', 
       dataLabels: { 
        enabled: true, 
        style: { 
         textShadow: '0 0 3px black' 
        } 
       }, point: { 
        events: { 
         click: function() { 
          alert('Category: ' + this.category + ', value: ' + this.y); 
         } 
        } 
       } 
      } 

     }, 

     series: [] 

    }, 
    $.getJSON("js/data.json", function(json) { 
     options.xAxis.categories = json[0]['data']; 
     options.series[0] = json[1]; 
     options.series[1] = json[2]; 
     chart = new Highcharts.Chart(options); 
    }); 
} 

var app = angular.module('charts', []); 

app.directive('highchart', [function() { 
    return { 
     restrict: 'E', 
     template: '<div></div>', 
     replace: true, 
     link: function (scope, element, attrs) { 

      scope.$watch(attrs.chart, function() { 

       if (!attrs.chart) return; 

       var chart = scope.$eval(attrs.chart); 

       angular.element(element).highcharts(chart); 
      }); 

     } 
    } 
}]); 

function Ctrl($scope) { 
    $scope.example_chart = get_chart(); 
} 

JSON對象

[ 
    { 
    "name": "Month", 
    "data": [ 
     "Jan", 
     "Feb", 
     "Mar", 
     "Apr", 
     "May", 
     "Jun", 
     "Jul", 
     "Aug", 
     "Sep", 
     "Oct", 
     "Nov", 
     "Dec" 
    ] 
    }, 
    [ 
    { 
     "name": "Overhead", 
     "data": [ 
     21990, 
     22365, 
     21987, 
     22369, 
     22558, 
     22987, 
     23521, 
     23003, 
     22756, 
     23112, 
     22987, 
     22897 
     ] 
    } 
    ], 
    { 
    "name": "Revenue", 
    "data": [ 
     23987, 
     24784, 
     25899, 
     25569, 
     25897, 
     25668, 
     24114, 
     23899, 
     24987, 
     25111, 
     25899, 
     23221 
    ] 
    } 
] 
+1

什麼錯誤,如果讓? –

+0

根本沒有錯誤 – user2402107

+0

「不工作」是什麼意思? – anjruu

回答

0

通過外部JSON文件調用See Plunker demo

function get_chart() { 
var options = { 
    chart: { 
     type: 'column', 
     renderTo:'container' 
    }, 
    title: { 
     text: 'Twitter Data' 
    }, 
    xAxis: { 
     categories: [] 
    }, 

    plotOptions: { 
     series: { 
      stacking: 'normal', 
      dataLabels: { 
       enabled: true, 
       style: { 
        textShadow: '0 0 3px black' 
       } 
      }, point: { 
       events: { 
        click: function() { 
         alert('Category: ' + this.category + ', value: ' + this.y); 
        } 
       } 
      } 
     } 

    }, 

    series: [] 

}; 
$.getJSON("results.json", function(json) { 
    options.xAxis.categories = json[0]['data']; 
    options.series[0] = json[1]; 
    options.series[1] = json[2]; 
    chart = new Highcharts.Chart(options); 
    }); 
    } 
    get_chart(); //call this get_chart function in your controller where you want, I called it here. 
+0

這必須是外部json對象 – user2402107

+0

沒有問題,取消註釋$ .getjson部分 –

+0

getJson無法訪問,並且它不再是堆積的條形圖 – user2402107