Highchart from CSV file with JavaScript

青春壹個敷衍的年華 提交于 2019-12-13 08:21:15

问题


I made a JavaScript function that makes a Highchart, reading from a CSV file.

<script>
function Showgraph(){
var options = {
    chart: {
        renderTo: 'container',
        defaultSeriesType: 'line'
    },
    title: {
        text: 'Measurement data'
    },
    xAxis: {
        categories: [],
        title: {
            text: 'Measurement number'
            },
            labels: {
                enable: false,
            y:20, rotation: -45, allign: 'right'
            }
    },
    yAxis: {
        title: {
            text: 'Retro Reflection'
        }
    },
    series: []
};

$.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(',');

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

        }

    });

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

The CSV file where the x are numbers.

Categories,xxx,
0.2,xxx,
0.33,xxx,
0.5,xxx,
0.7,xxx,
1.0,xxx,
1.5,xxx,
2.0,xxx,

I would then like to remove the value: Categories, xxx, from the CSV file and modify the JavaScript function so that it still works but the chart doesn't show any values on the X-axis.

The new CSV file

0.2,xxx,
0.33,xxx,
0.5,xxx,
0.7,xxx,
1.0,xxx,
1.5,xxx,
2.0,xxx,

Basically to make a Highchart from the last that CSV file.


回答1:


Use this parser:

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


        var series = {
            data: []
        };
        $.each(items, function(itemNo, item) {
                series.data.push(parseFloat(item));
        });

        options.series.push(series);


});

And remove a categories: [] from xAxis options.



来源:https://stackoverflow.com/questions/26779758/highchart-from-csv-file-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!