New_programmer
New_programmer

Reputation: 285

HighCharts reading from CSV basic example

Am new to highcharts and JS and am trying to plot data from a csv file (data3.csv).

Here is the code at the moment:

<!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>

And the contents of the csv file are:

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

However, this is not giving a chart (just gives the title).

Could anyone suggest where I am going wrong?

Thanks

Upvotes: 1

Views: 12142

Answers (1)

Sebastian Bochan
Sebastian Bochan

Reputation: 37588

In line

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

You should spline csv by commas, but you have space. So you can replace this line with:

var items = line.split(' ');

or generate csv which items will separated by comma.

As a result your parser should looks like:

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

Upvotes: 1

Related Questions