gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:54:56 25 4
gpt4 key购买 nike

您好,我正在尝试制作一个网页来显示政客推文的观点。我正在尝试为情绪统计创建图表。我正在使用 Zingchart,我能够为静态数据创建,但我无法为动态数据创建。

这是我用来插入带有数组图表数据的静态数据的代码。

   <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>

static pie chart

它像这样工作得很好,但我想使用来 self 的 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>

如何从 Angular Controller 中获取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"
}]
};

});

这是 political.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>

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

最佳答案

坦白说,我是 ZingChart 团队的一员。

您正在寻找的确切功能是数据绑定(bind)。我们有关于 angularjs-charts 的文档数据绑定(bind)元素到图表的页面。

在您的情况下,需要几个步骤才能实现您想要的。我的第一个建议是在 ng-init 中将不必要的 Controller 逻辑从您的 View 中移除,并将其保留在指令中。只是建议,没必要。为了这个答案,我的格式会将大部分内容保留在 Controller 中。

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>

应用逻辑:

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);

});

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

您必须在 HTML 中使用 zc-values 而不仅仅是 zc-json 因为 code通过使用 $watchCollection()zc-values 进行深度平等观察。

“$watchCollection() 函数是上述两个 $watch() 配置之间的一种中间地带。它比普通的 $watch() 函数更深入;但是,它几乎没有像 $watch() 函数那么昂贵深度相等的 $watch() 函数。与 $watch() 函数一样,$watchCollection() 通过比较物理对象引用来工作;但是,与 $watch() 函数不同,$watchCollection() 深入一层并对集合中的顶级项目执行额外的浅层引用检查。” - referenced here.

这就是为什么它仅在我更改 aValues 数组中的引用时才在我的示例中起作用。

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

Codepen 演示 here.

更多 Angular/ZingChart 链接

  1. Angular Page
  2. ZingChart Codepen

关于html - 如何使用动态数据创建 AngularJS 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38589277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com