如何使用动态数据创建AngularJS图表

Sum*_*tha 6 html charts angularjs zingchart angular-controller

嗨我正在尝试一个显示政治推文情绪的网页.我正在尝试为情绪统计创建一个图表.我正在使用Zingchart,我能够创建静态数据,但我无法创建动态数据.

这是我用于插入带有数组chartdata的静态数据的代码.

   <div class="row">
    <div class="col-sm-12">
      <div ng-init="chartdata = [[1167],[456],[990]]">
        <zingchart id="chart-1" zc-values="chartdata" zc-json="myJson" 
         zc-width="100%" zc-height="568px"></zingchart>
      </div>
    </div>
  </div>
Run Code Online (Sandbox Code Playgroud)

静态饼图

它工作正常,但我想使用来自angularjs的数据而不是静态值.例如:我试图做这样的事情,但它不起作用

  <div class="row">
      <div class="col-sm-12">
        <div ng-init="chartdata =[[{{politicianData.stats.total_positive}}],[{{politicianData.stats.total_negative}}],[{{politicianData.stats.total_neutral}}]]">
          <zingchart id="chart-1" zc-values="chartdata" zc-json="myJson" zc-width="100%" zc-height="568px"></zingchart>
        </div>
      </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

如何从角度控制器获取promise数组数据中的数据?

这是我的政治家 - 控制者:

    angular.module('myapp')
    .controller('PoliticianController', function($scope, PoliticianData, Politician, searchService, utilService) {
var politicianData = new Politician(PoliticianData);

        politicianData.$promise.then(function (result) {

            $scope.politicianData = result;
  });
$scope.myJson = {
                            globals: {
                            shadow: false,
                            fontFamily: "Verdana",
                            fontWeight: "100"
                                        },
                                        type: "pie",
                                        backgroundColor: "#fff",

                                        legend: {
                                            layout: "x5",
                                            position: "50%",
                                            borderColor: "transparent",
                                            marker: {
                                                borderRadius: 10,
                                                borderColor: "transparent"
                                            }
                                        },
                                        tooltip: {
                                            text: "%v requests"
                                        },
                                        plot: {
                                            refAngle: "-90",
                                            borderWidth: "0px",
                                            valueBox: {
                                                placement: "in",
                                                text: "%npv %",
                                                fontSize: "15px",
                                                textAlpha: 1,
                                            }
                                        },
                                        series: [{
                                            text: "Positive",
                                            backgroundColor: "#FA6E6E",
                                        }, {
                                            text: "Negative",
                                            backgroundColor: "#D2D6DE"
                                        }, {
                                            text: "Neutral",
                                            backgroundColor: "#28C2D1"
                                        }]
                                    };

    });
Run Code Online (Sandbox Code Playgroud)

这是politician.html页面

<span>{{politician.first_name}} {{politician.last_name}}</span>
                    </td>
                    <td>{{politicianData.stats.total}}</td>
                    <td>{{politicianData.stats.twitter.total}}</td>
                    <td>{{politicianData.stats.rss.total}}</td>
                   <td>{{politicianData.stats.total_negative}}</td>
                   <td>{{politicianData.stats.total_positive}}</td>
                   <td>{{politicianData.stats.total_neutral}}</td>
Run Code Online (Sandbox Code Playgroud)

PoliticianData.stats.total_positive正确显示计数,我想使用相同的数据作为输入值到我的图表.我怎么做?

nar*_*cky 6

完全披露,我是ZingChart团队的成员.

您正在寻找的这个确切功能是数据绑定.我们在angularjs-charts页面上有关于图表数据绑定元素的文档.

在您的情况下,有几个步骤需要实现您想要的.我的第一个建议是在ng-init中从视图中取出不必要的控制器逻辑,并将其保存在指令中.只是一个建议,没有必要.为了这个答案,我的格式将保留控制器内的大部分内容.

HTML:

<body ng-app="myApp">
  <div ng-controller="MainController">
    <div zingchart id="chart-1" zc-json="myJson" zc-width="100%" zc-height="568px" zc-values="aValues"></div>
  </div>
</body>
Run Code Online (Sandbox Code Playgroud)

应用逻辑:

var app = angular.module('myApp',['zingchart-angularjs']);

app.controller('MainController', function($scope, $timeout) {

// mimick your promise
(function(){
  $scope.data = {};
  $scope.data.valuesOne = [1,2,3,4,5];
  $scope.data.valuesTwo = [1,2,3,4,5];
  $scope.aValues = [$scope.data.valuesOne,$scope.data.valuesTwo];
})();

$scope.myJson = {
  type : "bar",
  title:{
    backgroundColor : "transparent",
    fontColor :"black",
    text : "Hello world"
  },
  backgroundColor : "white",
  series : [
    {
      backgroundColor : '#00baf2'
    },
    {
      backgroundColor : '#4caf4f'
    }
  ]
};

 // automatically wraps code in $apply
 $timeout(function(){

   // wont reflect changes in aValues because its an object
   $scope.data.valuesOne = [5]; 

   // must force the reflection of the changes
   $scope.aValues = [$scope.data.valuesOne, $scope.data.valuesTwo];

   // will reflect changes with one line, not the above two lines
   //$scope.aValues[0] = [5];

 },1500);

});
Run Code Online (Sandbox Code Playgroud)

您可以看到我设置了$ timeout以反映1.5秒后的图形更改.这个$ timeout是模仿数据库更新中的某种对象更改.有两种方法可以更新值,但要了解为什么我们必须首先了解有关ZingChart Angular指令的更多信息.

您必须zc-values在HTML中使用,而不仅仅是zc-json因为代码具有深度相等的监视zc-values功能$watchCollection().

"$ watchCollection()函数是上面两个$ watch()配置之间的中间位置.它比vanilla $ watch()函数更深入;但是,它并不像深度那样昂贵 - 等于$ watch()函数.与$ watch()函数一样,$ watchCollection()通过比较物理对象引用来工作;但是,与$ watch()函数不同,$ watchCollection()深入一级并执行对集合中顶级项目进行额外的浅层参考检查." - 在这里引用.

这就是为什么当我更改aValues数组中的引用时它只能在我的示例中工作.

您可以看到$ timeout内有两种方法来更新图表的值.因为你有一个数组数组,所以对内部数组的更改不会被反映,因为它们是深层对象属性.您可以看到更新单个数组必须强制分配回父级以反映更改.直接更改父级将更新引用,并将反映一行中的更改.

Codepen演示在这里.

更多Angular/ZingChart链接

  1. 角度页面
  2. ZingChart Codepen


hur*_*ane 3

  • 使用工厂创建服务
  • 调用该服务获取数据
  • 仅使用 zingchart 指令
  • 当范围数据发生变化时,图表将打开。

例子:

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

myModule.controller('myController', function($scope, chartService) {
  $scope.chartdata = chartService.getDataWithService();
});

myModule.factory('chartService', function($http) {
  return {
    getDataWithService: function() {
      var url = "yourServiceURL";
      return $http.get(url);
    },
    getData: function() {
      return {
        type: 'line',
        series: [{
          values: [54, 23, 34, 23, 43]
        }, {
          values: [10, 15, 16, 20, 40]
        }]
      };
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

html:

 <zingchart id="chart-1" zc-values="chartdata" zc-json="myJson" zc-width="100%" zc-height="568px"></zingchart>
Run Code Online (Sandbox Code Playgroud)