[英]Line chart is not showing the data on clicking the radio button
I have created a line chart using chart.js and there is radio button as well, so it should show data according to the selected radio button.我使用 chart.js 创建了一个折线图,并且还有单选按钮,所以它应该根据选定的单选按钮显示数据。
HTML code: HTML代码:
<div ng-controller="lineChartCtrl as chart">
<div>
<input type="radio" value="Month" name="optionsRadios" ng-model="graph" ng-click="months()"> Month
</div>
<div>
<input type="radio" checked= "true" value="Quarter" name="optionsRadios" ng-model="graph" ng-click="quarters()" >Quarter
</div>
<canvas linechart options="chart.lineOptions" data="chart.lineData" height="140" responsive=true >
</canvas>
</div>
My controller code:我的控制器代码:
function lineChartCtrl($scope,$http,$state)
{
this.lineOptions = {
scaleShowGridLines : true,
scaleGridLineColor : "rgba(0,0,0,.05)",
scaleGridLineWidth : 1,
bezierCurve : true,
bezierCurveTension : 0.4,
pointDot : true,
pointDotRadius : 4,
pointDotStrokeWidth : 1,
pointHitDetectionRadius : 20,
datasetStroke : true,
datasetStrokeWidth : 2,
datasetFill : true
};
this.lineData = {
labels: ["Q2-18","Q3-18","Q4-18","Q1-19"],
datasets: [
{
label: "Example dataset",
fillColor: "rgba(26,179,148,0.5)",
strokeColor: "rgba(26,179,148,0.7)",
pointColor: "rgba(26,179,148,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: quarter_score
},
]
};
$scope.quarters = function(){
this.lineData = {
labels: ["Q2-18","Q3-18","Q4-18","Q1-19"],
datasets: [
{
label: "Quarter_Wise",
fillColor: "rgba(26,179,148,0.5)",
strokeColor: "rgba(26,179,148,0.7)",
pointColor: "rgba(26,179,148,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(26,179,148,1)",
data: quarter_score
},
]
};
this.lineOptions = {
scaleShowGridLines : true,
scaleGridLineColor : "rgba(0,0,0,.05)",
scaleGridLineWidth : 1,
bezierCurve : true,
bezierCurveTension : 0.4,
pointDot : true,
pointDotRadius : 4,
pointDotStrokeWidth : 1,
pointHitDetectionRadius : 20,
datasetStroke : true,
datasetStrokeWidth : 2,
datasetFill : true
};
}
$scope.months = function(){
console.log("inside month");
this.lineData = {
labels: ["APR-18","MAY-18","JUN-18","JUL-18","AUG-18","SEP-18","OCT-18","NOV-18","DEC-18","JAN-19","FEB-19","MAR-19"],
datasets: [
{
label: "Month_Wise",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(26,179,148,1)",
data: month_score
},
]
};
this.lineOptions = {
scaleShowGridLines : true,
scaleGridLineColor : "rgba(0,0,0,.05)",
scaleGridLineWidth : 1,
bezierCurve : true,
bezierCurveTension : 0.4,
pointDot : true,
pointDotRadius : 4,
pointDotStrokeWidth : 1,
pointHitDetectionRadius : 20,
datasetStroke : true,
datasetStrokeWidth : 2,
datasetFill : true
};
}
}
So by default, it is showing the quarter wise graph which is fine, but when I am selecting the month-wise from the radio button the graph is not changing, and on selecting quarter-wise from the radio button it is not taking any values from inside the function, only default value is shown on the graph (which is defined above the functions)因此,默认情况下,它显示的是季度明智的图表,这很好,但是当我从单选按钮中选择月份时,图表没有改变,并且在从单选按钮中选择季度时,它没有采用任何值在函数内部,图形上仅显示默认值(在函数上方定义)
So where I am doing wrong.Please Help所以我做错的地方请帮助
Thanks谢谢
You could try using arrow functions (() => {})
instead of anonymous functions function {}
or .bind(this)
on anonymous functions.您可以尝试在匿名函数上使用箭头函数
(() => {})
而不是匿名函数function {}
或.bind(this)
。 Maybe you access wrong this
when calling it from inside an anonymous function.也许你访问错误
this
从一个匿名函数内部调用它时。
You could also try updating the data directly on the chart.js
object.您也可以尝试直接在
chart.js
对象上更新数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.