0

I have a trouble with loading CSV to highcharts. I don't know what to do to get something like this: http://jsfiddle.net/3bQne/885/ (my code is based on: How do I select which columns from my CSV to chart with HighChart?). It is my sample CSV:

1,24,38
3,26,62
4,16,42
5,17,36

And this is code which should show me chart on webpage:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="width:100%; height:400px;"></div>
<script>
var options = {
    chart: {
        renderTo: 'container',
        defaultSeriesType: 'line'
    },
    title: {
        text: 'Temperatura'
    },
    xAxis: {
        categories: []
    },
    yAxis: {
        title: {
            text: 'temperatura'
        }
    },
    series: [{
        data: []
    }]
};

$.get('data.csv', function(data) {
    var lines = data.split('\n');
    $.each(lines, function (lineNo, line) {
        var items = lines.split(',');
        if(lineNo !== 0) {
           var x = items[0];
               temp = parseFloat(items[1]);
            if(!isNaN(temp)){
                options.series[0].data.push([x,temp]);
            }
        }
    });
});
new Highcharts.Chart(options);
</script>

I have sit whole day trying to make chart which interests me and I have only this... Thank you for any help!

Community
  • 1
  • 1
ixjakub
  • 9
  • 1
  • So, what's the question? What's not working? Can you not `$.get` the file? Can you get it but not parse it? Can you parse it but now call highcharts? – Mark Jan 31 '14 at 19:59
  • I can get it but there's probably problem with parsing. How it looks now:http://imageshack.com/a/img30/1706/jvm3.jpg and how it should look around: http://imageshack.com/a/img543/251/655i.jpg What can I do to put correctly my data on chart? – ixjakub Jan 31 '14 at 20:55
  • Any errors in your javascript console? – Mark Jan 31 '14 at 20:59
  • Only this: http://imageshack.com/a/img542/663/g9u5.jpg Check please my code starting from "var lines = data.split('\n');", because there is big mess I think... – ixjakub Jan 31 '14 at 21:04
  • According to that error, you aren't evening loading the `csv` file. Start your reading here: http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource-w – Mark Jan 31 '14 at 21:17
  • Corrected. I've just copyied files to server. Now in console: http://imageshack.com/a/img819/5044/3pkn.jpg It relates to: var items = lines.split(','); As I read there http://stackoverflow.com/questions/10475458/jquery-uncaught-typeerror-object-0-0-has-no-method-split-trying-to-split, when I split something, I get an array. So, I get array before by splitting 'data'.. Help. – ixjakub Jan 31 '14 at 22:01
  • Use console and set console.log(data) above var lines = data.split('\n'); then you will ensure if wile is lodaded. Secondly var chart = new Highcharts.Chart(options); should be in $.get function (after $.each loop). – Sebastian Bochan Feb 03 '14 at 10:41
  • @SebastianBochan, my CSV is loaded. What about this: Uncaught TypeError: Object [object Array] has no method 'split' ? What should I change? – ixjakub Feb 03 '14 at 14:16
  • But if you use console.log, as I mentioned, file's content is printed? – Sebastian Bochan Feb 03 '14 at 14:43
  • Yes, it looks exactly as I wrote in my first post. – ixjakub Feb 03 '14 at 15:26
  • Did you get this working? If yes, can you share an example for the same? – Pravin Dec 01 '15 at 14:59

0 Answers0