【发布时间】:2019-03-13 02:35:26
【问题描述】:
我需要在我的页面上使用 ChartJS 创建动态数量的圆环图。我已经完成了这项工作,但使用的是静态数据(2 个图表和它们使用的数据)。
标记是:
<div class="row" id="divChartGroup">
<div class="col-md-3 col-lg-3">
<div class="panel panel-default">
<div class="panel-body">
<button type="button" class="close" data-target="#copyright-wrap" data-dismiss="alert" ng-click="ConfirmRemove('Guage1')"> <span aria-hidden="true">×</span><span class="sr-only" style="color:#000 !important">Close</span> </button>
<h4>Gauge1</h4>
<canvas id="CanvasGauge1"></canvas>
</div>
</div>
</div>
<div class="col-md-3 col-lg-3">
<div class="panel panel-default">
<div class="panel-body">
<button type="button" class="close" data-target="#copyright-wrap" data-dismiss="alert" ng-click="ConfirmRemove('Guage2')"> <span aria-hidden="true">×</span><span class="sr-only" style="color:#000 !important">Close</span> </button>
<h4>Gauge2</h4>
<canvas id="CanvasGauge2"></canvas>
</div>
</div>
</div>
<!-- Can be many more gauge charts here -->
</div>
现在一切都是静态的。我需要将其转换为动态。 “divChartGroup”内可以有N个仪表图。我为此使用AngularJs。这是我使用 AngularJS 获取数据的代码:
APIService.GetChartData()
.then(function (response) {
var data = response.data.result;
//Gets data in the format provided elow
//Need to write logic to generate charts dynamically here.
}
JSON 响应(数据)是这样的:
"result": [
{
"chartName": "Gauge1",
"score": 87
},
{
"chartName": "Gauge2",
"score": 87
},
{
"chartName": "Gauge3",
"score": 89
},
{
"chartName": "Gauge4",
"score": 88
},
.
.
.
]
创建图表的代码:
function DrawGauge(element, value) {
var _config = {
type: 'doughnut',
data: {
labels: [
"",
""
],
datasets: [{
data: [value, 100 - value],
backgroundColor: [
'#3d9447',
'#a7adba'
],
hoverBackgroundColor: [
'#3d9447',
'#a7adba'
]
}]
},
options: {
cutoutPercentage: 80,
legend: {
display: false
},
tooltips: {
enabled: true
},
elements: {
center: {
text: value.toFixed(2),
color: '#000000',
fontStyle: 'Arial',
sidePadding: 20,
fontSize: 50,
textAlign: 'center'
}
}
}
};
new Chart(element, _config);
}
如何生成所需的 HTML,并将事件绑定到按钮并动态创建图表?
【问题讨论】:
-
value参数是json分数?
标签: javascript angularjs chart.js