我想用CSV文件数据绘制一个饼图,但什么都没有
我使用Highcharts.ajax
从本地文件中读取数据,并将push
结果导入options.series
。
这是我的测试CSV数据:
BRCA,60
LUAD,30
LIHC,10
和JS代码:
<div id="container2" style="width: 550px; height: 400px; margin: 0 auto"></div>
<script language="JavaScript">
var options = {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
defaultSeriesType: 'pie'
},
title: {
text: 'Browser market shares in January, 2018'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: []
};
Highcharts.ajax({
url: 'CSV_file_path',
dataType: 'text',
success: function(data) {
// Split the lines
var lines = data.split('\n');
var items = "";
var hash = {};
var series = [{
name: 'aaa',
colorByPoint: true,
data: []
}]
lines.forEach(function(line, lineNo) {
items = line.split(',');
if (lineNo < (lines.length - 1)) {
hash['name'] = items[0];
hash['y'] = items[1];
series[0].data.push(hash);
hash = {};
}
});
options.series = series;
alert(options.series[0].data[0].name)
Highcharts.chart('container2', options);
},
error: function (e, t) {
console.error(e, t);
}
});
</script>