使用JavaScript使用JSON数据创建Google图表

问题描述 投票:0回答:1

我需要带有JSON的柱形图数据,以便使此图表自动化,但是我不知道该怎么做。

这是我的JS代码:

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

我的json:

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

我以前从未这样做过,如果有人帮助我,需要一点帮助(对不起,我的英语不好)

javascript pygooglechart
1个回答
0
投票

添加“ false”作为最后一个参数

var data = google.visualization.arrayToDataTable([
    ["Volume", "Valor"],
    ["DA", 67],
    ["CIF", 23],
    ["Expurgo", 45]
],
false); // 'false' means that the first row contains labels, not data.
© www.soinside.com 2019 - 2024. All rights reserved.