admin管理员组文章数量:1024660
I am trying to format numbers in chartjs chart. I am getting this error on my console and the numbers are not visible on the chart
Uncaught TypeError: Cannot read property 'format' of undefined
You can refer the fiddle here. Line 74 on the fiddle
for (var i = 0; i < firstDataSet.data.length; i++) {
var firstModel = firstDataSet._meta[Object.keys(firstDataSet._meta)[0]].data[i]._model;
var secondModel = secondDataSet._meta[Object.keys(secondDataSet._meta)[0]].data[i]._model;
var thirdModel = thirdDataSet._meta[Object.keys(thirdDataSet._meta)[0]].data[i]._model;
var fourthModel = fourthDataSet._meta[Object.keys(fourthDataSet._meta)[0]].data[i]._model;
var total = firstDataSet.data[i] + secondDataSet.data[i];
var total1 = thirdDataSet.data[i] + fourthDataSet.data[i];
// Line below is causing the error
ctx.fillText(formatter.format(Number(firstDataSet.data[i])) + " ", firstModel.x, firstModel.y + 20);
ctx.fillText((firstDataSet.data[i]) , firstModel.x, firstModel.y + 20);
ctx.fillText((secondDataSet.data[i] ) , secondModel.x, secondModel.y + 20);
ctx.fillText(total , secondModel.x, secondModel.y - 20);
ctx.fillText((thirdDataSet.data[i]) , thirdModel.x, thirdModel.y + 20);
ctx.fillText((fourthDataSet.data[i] ) , fourthModel.x, fourthModel.y + 20);
ctx.fillText(total1 , fourthModel.x, fourthModel.y - 20);
/*if (firstDataSet.data[i] >= secondDataSet.data[i]) {
ctx.fillText((firstDataSet.data[i] * 100 / total).toFixed(2) + '%', firstModel.x, firstModel.y + 30);
} else {
ctx.fillText((secondDataSet.data[i] * 100 / total).toFixed(2) + '%', secondModel.x, secondModel.y + 30);
}
*/
}
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
// the default value for minimumFractionDigits depends on the currency
// and is usually already 2
});
I am trying to format numbers in chartjs chart. I am getting this error on my console and the numbers are not visible on the chart
Uncaught TypeError: Cannot read property 'format' of undefined
You can refer the fiddle here. Line 74 on the fiddle
for (var i = 0; i < firstDataSet.data.length; i++) {
var firstModel = firstDataSet._meta[Object.keys(firstDataSet._meta)[0]].data[i]._model;
var secondModel = secondDataSet._meta[Object.keys(secondDataSet._meta)[0]].data[i]._model;
var thirdModel = thirdDataSet._meta[Object.keys(thirdDataSet._meta)[0]].data[i]._model;
var fourthModel = fourthDataSet._meta[Object.keys(fourthDataSet._meta)[0]].data[i]._model;
var total = firstDataSet.data[i] + secondDataSet.data[i];
var total1 = thirdDataSet.data[i] + fourthDataSet.data[i];
// Line below is causing the error
ctx.fillText(formatter.format(Number(firstDataSet.data[i])) + " ", firstModel.x, firstModel.y + 20);
ctx.fillText((firstDataSet.data[i]) , firstModel.x, firstModel.y + 20);
ctx.fillText((secondDataSet.data[i] ) , secondModel.x, secondModel.y + 20);
ctx.fillText(total , secondModel.x, secondModel.y - 20);
ctx.fillText((thirdDataSet.data[i]) , thirdModel.x, thirdModel.y + 20);
ctx.fillText((fourthDataSet.data[i] ) , fourthModel.x, fourthModel.y + 20);
ctx.fillText(total1 , fourthModel.x, fourthModel.y - 20);
/*if (firstDataSet.data[i] >= secondDataSet.data[i]) {
ctx.fillText((firstDataSet.data[i] * 100 / total).toFixed(2) + '%', firstModel.x, firstModel.y + 30);
} else {
ctx.fillText((secondDataSet.data[i] * 100 / total).toFixed(2) + '%', secondModel.x, secondModel.y + 30);
}
*/
}
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
// the default value for minimumFractionDigits depends on the currency
// and is usually already 2
});
Share
Improve this question
asked Sep 16, 2017 at 9:15
PradyPrady
11.3k41 gold badges127 silver badges177 bronze badges
2
-
Declare the
formatter
beforewindow.myBar = new Chart(ctx...
. Like so: fiddle – DavidDomain Commented Sep 16, 2017 at 9:28 - Oh great.. that did it. If you can add it as an answer i can accept it – Prady Commented Sep 16, 2017 at 9:30
1 Answer
Reset to default 1You have to declare the formatter
before calling the new Chart
constructor function, otherwise it will be undefined
inside the options
you are passing as the second parameter of new Chart
.
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
// the default value for minimumFractionDigits depends on the currency
// and is usually already 2
});
window.myBar = new Chart(ctx, {
type: 'bar',
data: data,
options: options
});
I am trying to format numbers in chartjs chart. I am getting this error on my console and the numbers are not visible on the chart
Uncaught TypeError: Cannot read property 'format' of undefined
You can refer the fiddle here. Line 74 on the fiddle
for (var i = 0; i < firstDataSet.data.length; i++) {
var firstModel = firstDataSet._meta[Object.keys(firstDataSet._meta)[0]].data[i]._model;
var secondModel = secondDataSet._meta[Object.keys(secondDataSet._meta)[0]].data[i]._model;
var thirdModel = thirdDataSet._meta[Object.keys(thirdDataSet._meta)[0]].data[i]._model;
var fourthModel = fourthDataSet._meta[Object.keys(fourthDataSet._meta)[0]].data[i]._model;
var total = firstDataSet.data[i] + secondDataSet.data[i];
var total1 = thirdDataSet.data[i] + fourthDataSet.data[i];
// Line below is causing the error
ctx.fillText(formatter.format(Number(firstDataSet.data[i])) + " ", firstModel.x, firstModel.y + 20);
ctx.fillText((firstDataSet.data[i]) , firstModel.x, firstModel.y + 20);
ctx.fillText((secondDataSet.data[i] ) , secondModel.x, secondModel.y + 20);
ctx.fillText(total , secondModel.x, secondModel.y - 20);
ctx.fillText((thirdDataSet.data[i]) , thirdModel.x, thirdModel.y + 20);
ctx.fillText((fourthDataSet.data[i] ) , fourthModel.x, fourthModel.y + 20);
ctx.fillText(total1 , fourthModel.x, fourthModel.y - 20);
/*if (firstDataSet.data[i] >= secondDataSet.data[i]) {
ctx.fillText((firstDataSet.data[i] * 100 / total).toFixed(2) + '%', firstModel.x, firstModel.y + 30);
} else {
ctx.fillText((secondDataSet.data[i] * 100 / total).toFixed(2) + '%', secondModel.x, secondModel.y + 30);
}
*/
}
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
// the default value for minimumFractionDigits depends on the currency
// and is usually already 2
});
I am trying to format numbers in chartjs chart. I am getting this error on my console and the numbers are not visible on the chart
Uncaught TypeError: Cannot read property 'format' of undefined
You can refer the fiddle here. Line 74 on the fiddle
for (var i = 0; i < firstDataSet.data.length; i++) {
var firstModel = firstDataSet._meta[Object.keys(firstDataSet._meta)[0]].data[i]._model;
var secondModel = secondDataSet._meta[Object.keys(secondDataSet._meta)[0]].data[i]._model;
var thirdModel = thirdDataSet._meta[Object.keys(thirdDataSet._meta)[0]].data[i]._model;
var fourthModel = fourthDataSet._meta[Object.keys(fourthDataSet._meta)[0]].data[i]._model;
var total = firstDataSet.data[i] + secondDataSet.data[i];
var total1 = thirdDataSet.data[i] + fourthDataSet.data[i];
// Line below is causing the error
ctx.fillText(formatter.format(Number(firstDataSet.data[i])) + " ", firstModel.x, firstModel.y + 20);
ctx.fillText((firstDataSet.data[i]) , firstModel.x, firstModel.y + 20);
ctx.fillText((secondDataSet.data[i] ) , secondModel.x, secondModel.y + 20);
ctx.fillText(total , secondModel.x, secondModel.y - 20);
ctx.fillText((thirdDataSet.data[i]) , thirdModel.x, thirdModel.y + 20);
ctx.fillText((fourthDataSet.data[i] ) , fourthModel.x, fourthModel.y + 20);
ctx.fillText(total1 , fourthModel.x, fourthModel.y - 20);
/*if (firstDataSet.data[i] >= secondDataSet.data[i]) {
ctx.fillText((firstDataSet.data[i] * 100 / total).toFixed(2) + '%', firstModel.x, firstModel.y + 30);
} else {
ctx.fillText((secondDataSet.data[i] * 100 / total).toFixed(2) + '%', secondModel.x, secondModel.y + 30);
}
*/
}
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
// the default value for minimumFractionDigits depends on the currency
// and is usually already 2
});
Share
Improve this question
asked Sep 16, 2017 at 9:15
PradyPrady
11.3k41 gold badges127 silver badges177 bronze badges
2
-
Declare the
formatter
beforewindow.myBar = new Chart(ctx...
. Like so: fiddle – DavidDomain Commented Sep 16, 2017 at 9:28 - Oh great.. that did it. If you can add it as an answer i can accept it – Prady Commented Sep 16, 2017 at 9:30
1 Answer
Reset to default 1You have to declare the formatter
before calling the new Chart
constructor function, otherwise it will be undefined
inside the options
you are passing as the second parameter of new Chart
.
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
// the default value for minimumFractionDigits depends on the currency
// and is usually already 2
});
window.myBar = new Chart(ctx, {
type: 'bar',
data: data,
options: options
});
本文标签: javascriptUncaught TypeError Cannot read property 39format39 of undefinedStack Overflow
版权声明:本文标题:javascript - Uncaught TypeError: Cannot read property 'format' of undefined - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745556015a2155880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论