File loading a Csv file into highcharts

让人想犯罪 __ 提交于 2019-12-24 21:52:06

问题


I'm plotting Csv column data in highcharts. Instead of the:

$.get('5.csv', function(data)

I want input a local desktop Csv file using:

function handleFileSelect(evt) {
var files = evt.target.files; // FileList object

My current Javascript code is below :

var options = {
chart: {
    renderTo: 'container',
    defaultSeriesType: 'line'
},
title: {
    text: 'Test'
},
xAxis: {
    categories: []
},
yAxis: {
    title: {
        text: 'Units',

    }
},
series: []
};

// $.get('5.csv', function(data) {

var file = event.target.file;
var reader = new FileReader();
var txt=reader.readAsText(file);

    var lines = txt.split('\n');
    var c = [], d = [];
    $.each(lines, function(lineNo, line) {
            if(lineNo > 0 ){
                var items = line.split(','); 
                var strTemp = items[0];
                c = [parseFloat(items[0]), parseFloat(items[1])];
                d.push(c);
                console.log(c);
            }
    });


    options.xAxis.categories = c;
    options.series = [{
            data: d
    }];
    chart = new Highcharts.Chart(options);
});

How would I go about doing this ? I want to upload a Csv file from a local desktop machine. How do I link the File Reader upload of the file to highcharts to plot, instead of using the $.get(5.csv', function(data) { ? Or am I better using jquery-csv (https://github.com/evanplaice/jquery-csv). I know there are browser security issues. My file is a 3 column Csv with a one line header, column 1 is the x-axis, 2 is the y-axis, 3 will be the error bar, which I haven't yet implemented:

Q,I,E
0.009,2.40E-01,5.67E-02
0.011,2.13E-01,3.83E-02
0.013,2.82E-01,2.28E-02

etc ....


回答1:


This works now upload by File API

function processFiles(files) {
    var chart;
    options = {
            chart: {
                zoomType: 'x',
                renderTo: 'container',
                type: 'line',
                zoomType: 'x'

            },
            title: {
                text: ''
            },
            subtitle: {
                text: '' 
            },
            xAxis: {
                type: 'linear',
                minorTickInterval: 0.1,
                title: {
                    text: 'Q'}
            },
            yAxis: {
                type: 'linear',
                minorTickInterval: 0.1,
                title: {
                    text: 'I(ntensity)'
                },
            },
            tooltip: {
                shared: true
            },
            legend: {
                enabled: true
            },
            plotOptions: {
                area: {
                    fillColor: {
                        linearGradient: [0, 0, 0, 300],
                        stops: [
                            [0,      Highcharts.getOptions().colors[0]],
                            [0, 'rgba(2,0,0,0)']
                        ]
                    },
                    lineWidth: 1,
                    marker: {
                        enabled: false,
                        states: {
                            hover: {
                                enabled: true,
                                radius: 5
                            }
                        }
                    },
                    shadow: false,
                    states: {
                        hover: {
                            lineWidth: 1
                        }
                    }
                }
            },
            series: [{
                name: 'Series'}]
    };



var file = files[0]
var reader = new FileReader();
reader.onload = function (e) {

str = e.target.result;
    var lines = str.split("\n");
    var c = [], d = [], er = [];
    $.each(lines, function(lineNo, line) {
            if(lineNo > 0 ){
                var items = line.split(','); 
                var strTemp = items[0];
                er = parseFloat(items[2])
                a = parseFloat(items[0])
                b = parseFloat(items[1])
                min = (b - (er/2))
                max = b + ((er/2))
                c = [a , b];
                var q = [], e = [];
                q = [min, max]
                e.push(q);

                d.push(c);

                console.log(c);
                console.log(q);
            }
    });

    options.xAxis.categories = c.name;
    lineWidth: 1
    options.series = [{
            data: d,
            type: 'scatter'
    }, {
        name: 'standard deviation',
        type: 'errorbar',
        color: 'black',
        data : e }
];
    $("#Linear").click(function(){
        $('#container').highcharts().yAxis[0].update({ type: 'linear'});
    });

    $("#Log").click(function(){
        $('#container').highcharts().yAxis[0].update({ type: 'logarithmic'});
    });

    $("#Guinier").click(function(){
        $('#container').highcharts().yAxis[0].update({ data: Math.log(d)});
        options.xAxis.categories = c.name;
        lineWidth: 1
        options.series = [{
            data: d
    }]
    });

  chart = new Highcharts.Chart(options);
}

    reader.readAsText(file)
    var output = document.getElementById("fileOutput")

};



回答2:


Due to security reasons you can't load a file directly on the client-side

To do this you need to use the HTML5 File API which will give the user a file dialog to select the file.

If you plan to use jquery-csv here's an example that demonstrates how to do that.

  • File Handling Demo

I'm biased but I say use jquery-csv to parse the data, trying to write a CSV parser comes with a lot of nasty edge cases.

Source: I'm the author of jquery-csv

As an alternative, if jquery-csv doesn't meet your needs, PapaParse is very good too.



来源:https://stackoverflow.com/questions/21409505/file-loading-a-csv-file-into-highcharts

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