C3: Charting dinamicamente usando arquivo JSON externo

Criado em 15 out. 2016  ·  3Comentários  ·  Fonte: c3js/c3

Gostaria de agradecer a você por criar esta ótima biblioteca de código aberto.
Quero criar gráficos dinâmicos usando um arquivo JSON externo no qual o gráfico analisa os dados json cada um por vez.
Meu código atual:
var chart = c3.generate({ bindto: '#chart', size: { height: 240, width: 480 }, data: { url: 'static/data/trainp.json', mimeType: 'json', keys: { x: 'Series', value: ['Setting1'] }, type: 'line' }, subchart: { show: true } });

Exemplo de gráfico obrigatório: https://jsfiddle.net/cy2owjva/

Por favor, deixe-me saber quais mudanças eu tenho que fazer em https://jsfiddle.net/cy2owjva/ .
Obrigada

Comentários muito úteis

@AskSaikatSinha Experimente isso
`
$ (function () {

$.getJSON('./linejson.json', function (externaldata) {
var chart = c3.generate({
    title: {
        text:'External JSON loading via C3.js'
    },
    bindto: '#chart',
    data: {
       json: externaldata,
        type: 'area-spline',

    }
})

setInterval(function(){
    chart.flow({
        columns:
                [
                    ['data1',Math.round(Math.random()*200)]
                ],
        type:'area-spline'
    })
}, 1500)

});
})
`

e ter seu arquivo JSON externo assim

arquivo linejson.json

{ "data1": [30, 20, 50, 40, 60, 50] }

Todos 3 comentários

@AskSaikatSinha Experimente isso
`
$ (function () {

$.getJSON('./linejson.json', function (externaldata) {
var chart = c3.generate({
    title: {
        text:'External JSON loading via C3.js'
    },
    bindto: '#chart',
    data: {
       json: externaldata,
        type: 'area-spline',

    }
})

setInterval(function(){
    chart.flow({
        columns:
                [
                    ['data1',Math.round(Math.random()*200)]
                ],
        type:'area-spline'
    })
}, 1500)

});
})
`

e ter seu arquivo JSON externo assim

arquivo linejson.json

{ "data1": [30, 20, 50, 40, 60, 50] }

Estou com o mesmo problema. Tentando animar um gráfico com um fluxo de dados em tempo real.
tentei isso:

var xhr = new XMLHttpRequest();
function reload(){
    //each call on this url changes the json
    xhr.open('GET', "http://127.0.0.1:5000/json/", true).send(); 
    chart.flow('http://127.0.0.1:5000/json/');
    chart.flush()
}

setInterval(reload, 500);

Tentei fluir, gerar, carregar e nenhum desses recarrega o gráfico sem a necessidade de recarregar o navegador.

Alguma dica?

@HawiCaesar Obrigado, seu código funciona.

Esta página foi útil?
0 / 5 - 0 avaliações