英文:
Uncaught TypeError: Cannot read property '' of undefined
问题
我正在尝试基于jSon
分组创建动态图表,如下所示:
var data = [{
"France": {
"val1": [10, 20, 30, 40]
},
"Croatia": {
"val2": [50, 60, 70, 80]
}
}];
for (var i = 0; i <= data.length; i++) {
console.log(data[i].France);
console.log(data[i].Croatia);
var val = i + 1;
var containerName = 'container' + val;
console.log(containerName);
Highcharts.chart(containerName, {
chart: {
type: 'column'
},
title: {
text: 'Monthly Average Rainfall'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
crosshair: true
},
yAxis: {
min: 0,
title: {
text: 'Rainfall (mm)'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'France',
data: data[i].France
}, {
name: 'Croatia',
data: data[i].Croatia
}]
});
}
在控制台中,您可以看到数据集,但在图表中,系列抛出以下异常:
Uncaught TypeError: Cannot read property 'France' of undefined
这是因为您的for
循环条件中使用了<=
,应该改为<
。修改后的代码如下:
for (var i = 0; i < data.length; i++) {
// ... 剩余部分不变
}
这样应该能够解决异常问题。
英文:
I am trying to create dynamic charts based on group by jSon
as follows:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var data = [{
"France": {
"val1": [10, 20, 30, 40]
},
"Croatia": {
"val2": [50, 60, 70, 80]}
}];
for(var i = 0; i <= data.length; i++) {
console.log(data[i].France);
console.log(data[i].Croatia);
var val = i + 1;
var containerName = 'container' + val;
console.log(containerName);
Highcharts.chart(containerName, {
chart: {
type: 'column'
},
title: {
text: 'Monthly Average Rainfall'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
crosshair: true
},
yAxis: {
min: 0,
title: {
text: 'Rainfall (mm)'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'France',
data: data[i].France
}, {
name: 'Croatia',
data: data[i].Croatia
}]
});
}
<!-- language: lang-css -->
.highcharts-figure,
.highcharts-data-table table {
min-width: 310px;
max-width: 800px;
margin: 1em auto;
}
#container {
height: 400px;
}
.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid #ebebeb;
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
.highcharts-data-table caption {
padding: 1em 0;
font-size: 1.2em;
color: #555;
}
.highcharts-data-table th {
font-weight: 600;
padding: 0.5em;
}
.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
padding: 0.5em;
}
.highcharts-data-table thead tr,
.highcharts-data-table tr:nth-child(even) {
background: #f8f8f8;
}
.highcharts-data-table tr:hover {
background: #f1f7ff;
}
<!-- language: lang-html -->
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<div id="container1">
</div><br/>
<div id="container2">
</div><br />
<div id="container3">
</div>
<!-- end snippet -->
I can show the dataset in the console but in the chart, the series throws an exception as follows:
Uncaught TypeError: Cannot read property 'France' of undefined
Is there anything that I am doing wrong here?
答案1
得分: 2
以下是您要的代码部分的中文翻译:
series: [{
name: 'France',
data: d[0].France.val1
}, {
name: 'Croatia',
data: d[0].Croatia.val2
}]
当您使用 data: d[0].France
时,您表示数据等于 val1: [10, 20, 30, 40]
。
当您使用 data: d[0].France.val1
时,您表示数据等于 [10, 20, 30, 40]
。
英文:
You need to do it this way:
series: [{
name: 'France',
data: d[0].France.val1
}, {
name: 'Croatia',
data: d[0].Croatia.val2
}]
When you do data: d[0].France
you say that data is equal "val1": [10, 20, 30, 40]
<br >
When you do data: d[0].France.val1
then you say that data is equal [10, 20, 30, 40]
Demo
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
var d = [{
"France": {
"val1": [10, 20, 30, 40]
},
"Croatia": {
"val2": [50, 60, 70, 80]
}
}];
for (var i = 0; i <= d.length; i++) {
var val = i + 1;
var containerName = 'container' + val;
Highcharts.chart(containerName, {
chart: {
type: 'column'
},
title: {
text: 'Monthly Average Rainfall'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
crosshair: true
},
yAxis: {
min: 0,
title: {
text: 'Rainfall (mm)'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'France',
data: d[0].France.val1
}, {
name: 'Croatia',
data: d[0].Croatia.val2
}]
});
}
<!-- language: lang-css -->
.highcharts-figure,
.highcharts-data-table table {
min-width: 310px;
max-width: 800px;
margin: 1em auto;
}
#container {
height: 400px;
}
.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid #ebebeb;
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
.highcharts-data-table caption {
padding: 1em 0;
font-size: 1.2em;
color: #555;
}
.highcharts-data-table th {
font-weight: 600;
padding: 0.5em;
}
.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
padding: 0.5em;
}
.highcharts-data-table thead tr,
.highcharts-data-table tr:nth-child(even) {
background: #f8f8f8;
}
.highcharts-data-table tr:hover {
background: #f1f7ff;
}
<!-- language: lang-html -->
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<div id="container1">
</div><br/>
<div id="container2">
</div><br />
<div id="container3">
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论