简体   繁体   中英

Create a Google Chart in javascript with JSON data

I need a column chart data with JSON, in order to automate this chart, but i dont no how do this.

So this is my JS Code:

function drawChart() {

    const container = document.querySelector('#chart');

    let chart = new google.visualization.ColumnChart(container);

    //Informações data.json

    let dataRequest = new Request("./datas/data.json");

    fetch(dataRequest)
        .then(function(resp) {
            return resp.json();
        })
        .then(function(data) {
            console.log(data);
        });

    // Informações gráficos

    let data = new google.visualization.arrayToDataTable([
        ["Volume", "Valor"],
        ["DA", 67],
        ["CIF", 23],
        ["Expurgo", 45]
    ]);

    const options = {
        titlePosition: 'none',
        colors: ['#ed4a0e', '#15466e'],
        backgroundColor: 'transparent',
        animation: {
            startup: true,
            duration: 1000,
            easing: 'out',
        },
        hAxis: { textStyle: { color: 'white' }, titleTextStyle: { color: 'white', fontSize: 15 } },
        vAxis: { textStyle: { color: 'white' }, titleTextStyle: { color: 'white', fontSize: 15 } },
        chartArea: { width: 250, height: 200 },
        legend: {
            textStyle: { color: '#ffffff', fontSize: 13 }
        }
    };



    chart.draw(data, options)
};

My json:

[
    ["Volume", "Valor"],
    ["DA", 67],
    ["CIF", 23],
    ["Expurgo", 45]
]

I never do this before, and need a little help,if someone help whit this (Sorry for my bad english)

Add "false" as last parameter

var data = google.visualization.arrayToDataTable([
    ["Volume", "Valor"],
    ["DA", 67],
    ["CIF", 23],
    ["Expurgo", 45]
],
false); // 'false' means that the first row contains labels, not data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM