Graph data already read from .xls files in html5

4

This button must be programmed to read a file in Excel with two columns, the first with data (x) and the second with data (y). the new records must be graphed but I have not yet achieved it.

var lineChartData = {
"datasets": [{
    "data": [
        "85",
        "87",
        "70",
        "80",
        "78",
        "100",],
        "pointStrokeColor": "#1A81C5",
        "fillColor": "rgba(254, 254, 254, 0.4)",
        "pointColor": "#1A81C5",
        "strokeColor": "#5D9CC6"
}],
    "labels": [
    "10",
    "20",
    "30",
    "40",
    "50",
    "60"],
};

var options = {showTooltips: true};

Chart.types.Line.extend({
name: "LineAlt",
highlightPoints: function(datasetIndex, pointIndexArray){
    var activePoints = [];
    var points = this.datasets[datasetIndex].points;
    for(i in pointIndexArray){
        if(points[pointIndexArray[i]]){
        activePoints.push(points[pointIndexArray[i]]);
      }
    }
    this.showTooltip(activePoints);
}
});

var myLine = new Chart(document.getElementById("canvas").getContext("2d")).LineAlt(lineChartData, options);

var highlight = function(index){
myLine.highlightPoints(0, [index]);
}
$("#slider").slider({
max: lineChartData.datasets[0].data.length-1,
slide: function( event, ui ) { highlight(ui.value); },
});

The contents of the Excel file when read with the code I add in my question is as follows:

DEMO HERE

    
asked by JBS 05.10.2016 в 23:21
source

3 answers

1

Thank you all for wanting to help me, but I have managed to solve the problem several days ago that when loading the excel file will take those data and plot them, I will leave a demo in case someone should need it later.
DEMO

    
answered by 10.10.2016 / 19:07
source
1

Have you tried using the kendo libraries? I work wonderfully (I've only made tables but you can make graphics perfectly).

You should only have a dataSource () in json format, it will be perfect for you that the lineChartData can already read it, but use some parse.JSON ().

Then you mount the kendoChart with and see it in the div that you have chosen. It is always better to use libraries if those do the work for us, it will take much less.

I'll give you an example:

<div id="chart" style="width: 350px; height: 250px;"></div>
<script>
var seriesData = [{
    date: new Date("2011/12/30"),
    value: 20
}, {
    date: new Date("2011/12/31"),
    value: 40
}, {
    date: new Date("2012/01/01"),
    value: 45
}, {
    date: new Date("2012/01/02"),
    value: 30
}, {
    date: new Date("2012/01/03"),
    value: 50
}];

$("#chart").kendoChart({
    dataSource: {
        data: seriesData
    },
    series: [{
        type: "column",
        field: "value",
        categoryField: "date",
        aggregate: "sum"
    }],
    categoryAxis: {
        baseUnit: "years"
    }
});
</script>

Here is the API link

    
answered by 06.10.2016 в 16:32
0

Modify your logic in the following way:

At this point, I assume that you can get the information in a variable whose value would be like this:

// Variables.
var contenido_excel = ""; // Contiene el la información del archivo Excel.
var arr_contenido_excel = ""; // arreglo de la variable "contenido_excel".
var valor = ""; // Valor individual leído en el ciclo "Ejemplo: 2,7"
var x_arreglo_grafica; // Contendrá la información que se usará para la gráfica (posición X).
var y_arreglo_grafica; // Contendrá la información que se usará para la gráfica (posición Y).

// Asignar valor a la variable.
contenido_excel = 
"SHEET: Hoja 1\n" + 
"\n" + 
"columna1,columna2\n"+
"2,7\n"+
"3,6\n"+
"4,4\n"+
"5,3\n"+
"6,6\n"+
"7,4\n"+
"8,7\n";

// Imprimir la información del archivo Excel.
console.log(contenido_excel);

// Estos son los resultados:
//SHEET: Hoja 1
//
//columna1,columna2
//2,7
//3,6
//4,4
//5,3
//6,6
//7,4
//8,7   

// Separa la información en un arreglo. 
arr_contenido_excel = contenido_excel.trim().split('\n');

// El resultado sería:  
console.log(arr_contenido_excel);
// ["SHEET: Hoja 1", "", "columna1,columna2", "2,7", "3,6", "4,4", "5,3", "6,6", "7,4", "8,7"]

// Recorrer el arreglo y obtener la información relevante para la gráfica.
for (var i = 0; i < arr_contenido_excel.length; i++) {
    // Solo vamos a obteneer la información relevante para la gráfica.
    if (arr_contenido_excel[i].trim() != "") {
        switch (i) {

            // Ignorar nombre de la hoja y encabezado
            // (a menos que lo necesites, modifica el código).
            case 0:
            case 2:
                break;

            // Obtener valores numéricos (según el arreglo).
            default:
                valor = arr_contenido_excel[i].trim();
                console.log(valor.toString());
                //x_arreglo_grafica.push(valor.toString().split(',')[0]);
                //y_arreglo_grafica.push(valor.toString().split(',')[1]);
                break;
        }
    }
}

// Final:
console.log(x_arreglo_grafica);
console.log(y_arreglo_grafica);

// A partir de este punto, ya podrías ir mirando cómo
// armar el objeto JSON requerido para la gráfica.
    
answered by 06.10.2016 в 23:44