如何使用angularjs在HighChart中传递响应数据

时间:2018-03-28 13:49:06

标签: javascript angularjs highcharts

我正在 highchart 上显示客户数据,他们将根据月份和年份在网站上注册。我从服务器获取响应以显示。但我没有得到如何将这些数据注入高图。 任何帮助/建议都非常感谢。

Angularjs:

.controller('ctrl',['$scope','$http',function($scope,$http){            
$http({
    url: '//customerinformation',
    method: 'GET'
})
.then(
    function successCallback(response){
        $scope.users = response.data.customers;
    },
    function errorCallback(response){
        console.log('Error:' + response.data);
});

$scope.chartOptions =  {
chart: { type: 'column' },
title: { text: 'Customer Information' },
xAxis: { categories: ['Jan', 'Feb', 'Mar','Apr','May','June','July','Aug','Sep','Oct','Nov','Dec'] },
yAxis: { title: { text: 'No.of customer' } },
series: [{ 
         name: 'customer', 
         data: []  <-- 'Here I need to pass the response data'
      }]
    };
}]);

2 个答案:

答案 0 :(得分:2)

你可以:

  • 设定系列&#39;从 __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { ActXList = new TList; TMyActX *TempActX; for(int i=0; i<10; i++) { TempActX = new TMyActX(Owner); TempActX->Parent = this; //Tried also with NULL and a new TForm(this), same result ActXList->Add(TempActX); TempActX->Init(i); //This adds i to a string in the created instance } MyActX1->Init(20); //Adds 20 to a string in the design time created instance MyActX2->Init(21); //Adds 21 to a string in the instance }
  • 生成图表之前的数据

  • 使用series.setData()动态更新图表。要做到这一点,你需要图表变量 - 在评论包装https://github.com/pablojim/highcharts-ng中提到可以帮助你。您也可以使用图表load event,其中void __fastcall TForm1::Button1Click(TObject *Sender) { TMyActX *TempActX; //A Combobox chooses the instance to show if(ComboBox1->ItemIndex < 10) { TempActX = (TMyActX *)ActXList->Items[ComboBox1->ItemIndex]; TempActX->ShowForm(); } else if(ComboBoxTerminaler->ItemIndex == 10) MyActX1->ShowForm(); else if(ComboBoxTerminaler->ItemIndex == 11) MyActX2->ShowForm(); //No matter wich instance is choosen to be shown, //the label with the init-string shows "0 1 2 3...9 20 21" for all of them } 是图表。

答案 1 :(得分:0)

您必须将响应数据推送到数组中,然后在数据中传递该数组。

.controller('ctrl',['$scope','$http',function($scope,$http){            
$http({
    url: '//customerinformation',
    method: 'GET'
  })
  .then(
    function successCallback(response){
       $scope.users = response.data;
       var CustomersData= [];
       for (var i = 0; i < $scope.users.length; i++) {
          CustomersData.push($scope.users[i].customers);   
       }
    },
    function errorCallback(response){
        console.log('Error:' + response.data);
  });



 $scope.chartOptions =  {
chart: { type: 'column' },
title: { text: 'Customer Information' },
xAxis: { categories: ['Jan', 'Feb', 'Mar','Apr','May','June','July','Aug','Sep','Oct','Nov','Dec'] },
yAxis: { title: { text: 'No.of customer' } },
series: [{ 
         name: 'customer', 
         data: CustomersData  <--- here you get your data.
        }]
      };
  }