HighCharts从CSV基本示例中读取

时间:2013-03-20 21:07:49

标签: javascript jquery highcharts

是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

然而,这并没有给出一个图表(只是给出了标题)。

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

由于

1 个答案:

答案 0 :(得分:1)

排队

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);
});