Javascript Highcharts从csv添加其他空白系列
我正在学习JavaScript,有一些编程知识,最终通常可以解决问题,但我在highcharts上遇到了一个问题Javascript Highcharts从csv添加其他空白系列,javascript,csv,highcharts,Javascript,Csv,Highcharts,我正在学习JavaScript,有一些编程知识,最终通常可以解决问题,但我在highcharts上遇到了一个问题 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Highcharts Example</title> <!-- 1. Add the
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<!-- 1. Add these JavaScript inclusions in the head of your page -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>
<!-- 2. Add the JavaScript to initialize the chart on document ready -->
<script type="text/javascript">
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'line'
},
title: {
text: 'Betting Performance'
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Yield (%)'
}
},
series: []
};
/*
Load the data from the CSV file. This is the contents of the file:
Apples,Pears,Oranges,Bananas,Plums
John,8,4,6,5
Jane,3,4,2,3
Joe,86,76,79,77
Janet,3,16,13,15
*/
$.get('data.csv', function(data) {
// Split the lines
var lines = data.split('\n');
$.each(lines, function(lineNo, line) {
var items = line.split(',');
// header line containes categories
if (lineNo == 0) {
$.each(items, function(itemNo, item) {
if (itemNo > 0)
options.xAxis.categories.push(item);
});
}
// the rest of the lines contain data with their name in the first position
else {
var series = {
data: []
};
$.each(items, function(itemNo, item) {
if (itemNo == 0) {
series.name = item;
} else {
series.data.push(parseFloat(item));
}
});
options.series.push(series);
}
});
var chart = new Highcharts.Chart(options);
});
});
</script>
</head>
<body>
<!-- 3. Add the container -->
<div id="container" style="width: 1200px; height: 800px; margin: 0 auto"></div>
</body>
</html>
海图示例
$(文档).ready(函数(){
变量选项={
图表:{
renderTo:'容器',
类型:“行”
},
标题:{
文字:“博彩表现”
},
xAxis:{
类别:[]
},
亚克斯:{
标题:{
正文:“收益率(%)”
}
},
系列:[]
};
/*
从CSV文件加载数据。这是文件的内容:
苹果、梨、桔子、香蕉、李子
约翰,8,4,6,5岁
简,3,4,2,3
乔,86,76,79,77
珍妮特,3,16,13,15岁
*/
$.get('data.csv',函数(数据){
//分道扬镳
变量行=data.split('\n');
$.each(行,函数)(行号,行){
var items=line.split(',');
//标题行包含类别
如果(行号==0){
$。每个(项目,功能(项目编号,项目){
如果(itemNo>0)
选项.xAxis.categories.push(项目);
});
}
//其余行包含名称位于第一个位置的数据
否则{
变量系列={
数据:[]
};
$。每个(项目,功能(项目编号,项目){
如果(itemNo==0){
series.name=项目;
}否则{
数据推送(parseFloat(item));
}
});
选项。系列。推送(系列);
}
});
var图表=新的Highcharts.图表(选项);
});
});
现在,我已经设法让这个工作,并附上下面的截图
但正如你在底部看到的,有一个系列叫做“系列3”
接下来是我的CSV文件,它是纯文本
,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42
英里,-100,-100,-100,-58.75,-31,-9.17,9.63,18.3,29.08,38.5,41.19,43.56,46.79,39.45,32.81,26.77,28.64,18.71,24.52,28.53,29.04,24.87,20.96,17.28,20.02,18.27,20.52,17.16.16,19.26,16.2,13.29,10.52,13.25,14.04,11.44,12.97,21.4,18.33,11.67,9
英里2,-100,-100,-100,-58.75,-22,-9.17,9.63,18.3,29.08,38.5,41.19,43.56,46.79,39.45,32.81,26.77,48.64,18.71,24.52,28.53,29.04,24.87,20.96,17.28,20.02,18.27,20.52,17.16.16,19.26,16.2,13.29,10.52,13.25,14.04,11.44,12.97,21.4,18.81,16.33,9
如图中所示,这将很好地绘制出图表。我制作了两组数据来强调多个系列的工作,而一个系列不是问题所在。所有数据点均从1-42号开始绘制,并具有适当的y值
我不知道额外的系列是从哪里来的,为什么。我已经尽可能多地关注highcharts演示,但现在我迷路了。正如jack R Abbit在评论中所说,excel在CSV末尾添加了空格。天知道为什么。删除空格解决了问题,帅哥多莉!再次感谢那些帮助过我们的人,希望其他人也会遇到这个问题并得到帮助 问候
迈尔斯你确定这是图像吗?这看起来像是Minecraft的皮肤。抱歉,不知道URL来自哪里,这是图像(),我现在添加正确的URL。谢谢你的提醒,为你添加了图片。祝你的问题好运!一件非常简单的事情是去掉每一行的前导/尾随空格,然后检查该行的长度是否为非零。如果有,就处理它。如果没有,则跳过它。我最近帮助了另一个有同样问题的人:谢谢你,杰克·R·阿巴特,它解决了这个问题!我知道要检查所有CSV,一定是excel像你说的那样添加了空格。多痛苦啊。我会尽快将此标记为已答复。祝您周末愉快,再次感谢您抽出时间:)