gpt4 book ai didi

javascript - HighCharts 从 CSV 读取基本示例

转载 作者:行者123 更新时间:2023-11-28 02:21:53 24 4
gpt4 key购买 nike

我是 highcharts 和 JS 的新手,正在尝试从 csv 文件 (data3.csv) 绘制数据。

这是目前的代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<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.3.2/jquery.min.js"></script>
<script type="text/javascript" src="highcharts.js"></script>
<!--[if IE]>
<script type="text/javascript" src="../js/excanvas.compiled.js"></script>
<![endif]-->


<!-- 2. Add the JavaScript to initialize the chart on document ready -->
<script type="text/javascript">
$(document).ready(function() {


var options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'line'
},
title: {
text: 'Stock Chart'
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Price'
}
},
series: []
};

$.get('data3.csv', function(data) {

$.each(lines, function(lineNo, line) {
var items = line.split(',');

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: 800px; height: 400px; margin: 0 auto"></div>


</body>
</html>

csv文件的内容是:

Date    Open
29/01/2010 538.49
28/01/2010 544.49
27/01/2010 541.27
26/01/2010 537.97
25/01/2010 546.59

但是,这并没有给出图表(只是给出了标题)。

有人可以建议我哪里出错了吗?

谢谢

最佳答案

排队

var items = line.split(',');

您应该用逗号对 csv 进行样条绘制,但您有空格。因此您可以将此行替换为:

var items = line.split(' ');

或生成 csv,其中的项目将用逗号分隔。

因此,您的解析器应该如下所示:

$.get('data.csv', function(data) {
// Split the lines
var lines = data.split('\n');
// Iterate over the lines and add categories or series
$.each(lines, function(lineNo, line) {
var items = line.split(',');
if(lineNo>0)
{
options.xAxis.categories.push(items[0]); //set first column from CSV as categorie
options.series[0].data.push(parseFloat(items[1])); //set second column from CSV as point value
}

});

// Create the chart
var chart = new Highcharts.Chart(options);
});

关于javascript - HighCharts 从 CSV 读取基本示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15534735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com