【发布时间】:2023-04-03 12:48:01
【问题描述】:
我正在学习 JavaScript 并且有一些编程知识,并且通常可以最终解决问题,但是我遇到了一个关于 highcharts 的问题。
<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>
现在,我已经成功地完成了这项工作,并附上了下面的屏幕截图。
但正如您在底部看到的那样,有一个名为“系列 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,19.26,16.2,13.29,10.52,13.25,14.04,11.44,12.97,21.4,18.81,16.33,13.95,11.67,9.48 里程 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,19.26,16.2,13.29,10.52,13.25,14.04,11.44,12.97,21.4,18.81,16.33,13.95,11.67,9.48
正如您在图像中看到的那样,这可以很好地绘制图表。我制作了两组数据来突出显示多个系列的工作,并且拥有一个系列不是问题。所有的数据点都是从数字 1-42 用适当的 y 值绘制的。
我不知道额外系列的来源和原因。我已经尽可能多地关注了 highcharts 演示,但我现在迷路了。
【问题讨论】:
-
你确定是这张图片吗?这看起来像 Minecraft 皮肤。
-
抱歉,不知道那个 URL 来自哪里,这是图片 (i.imgur.com/3aJ8PKd.png),我现在添加正确的 URL。感谢您的提醒
-
已为您添加了图片。祝你的问题好运!
-
一个非常简单的事情是去除每一行的前导/尾随空白,然后检查该行是否具有非零长度。如果是,则处理它。如果没有,则跳过它。我最近帮助了遇到同样问题的其他人:stackoverflow.com/questions/17860040/…
-
谢谢 Jack.R.Abbit,解决了这个问题!我知道要检查所有 CSV,就像你说的那样添加空格一定是 excel。多么痛苦。我很快就会将此标记为已回答。周末愉快,再次感谢您的宝贵时间:)
标签: javascript csv highcharts