receive json to make my Echart dynamic

Asked

Viewed 317 times

1

Aqui esta imagem do grafico

 var echartLine = echarts.init(document.getElementById('echart_line'), theme);

  echartLine.setOption({
    title: {
      text: 'Line ',
      subtext: 'Mes'
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      x: 220,
      y: 40,
      data: ['actions1', 'actions2', 'actions3']
    },
    toolbox: {
      show: true,
      feature: {
        magicType: {
          show: true,
          title: {
            line: 'Line',
            bar: 'Bar',
            stack: 'Stack',
            tiled: 'Tiled'
          },
          type: ['line', 'bar', 'stack', 'tiled']
        },
        restore: {
          show: true,
          title: "Restore"
        },
        saveAsImage: {
          show: true,
          title: "Save Image"
        }
      }
    },
    calculable: true,
    xAxis: [{
      type: 'category',
      boundaryGap: false,
     // scale:true,
     // splitNumber:12,

      data: ['Mon', Tue', 'Wed', 'Thu', 'Fri'] //Quero receber aqui em JSON
    }],
    yAxis: [{
      type: 'value'
    }],
    series: [{
      name: 'stuff',
      type: 'line',
      smooth: true,
      itemStyle: {
        normal: {
          areaStyle: {
            type: 'default'
          }
        }
      },
      data: [10, 12, 21, 54, 260, 830, 710] // Quero receber aqui em JSON
    }, {
      name: 'stuff',
      type: 'line',
      smooth: true,
      itemStyle: {
        normal: {
          areaStyle: {
            type: 'default'
          }
        }
      },
      data: [30, 182, 434, 791, 390, 30, 10]
    }, {
      name: 'things',
      type: 'line',
      smooth: true,
      itemStyle: {
        normal: {
          areaStyle: {
            type: 'default'
          }
        }
      },
      data: [1320, 1132, 601, 234, 120, 90, 20]
    }]
  });

How can I get my "data" array of PHP querys to make my chart dynamic? I am in urgent need of help. So far I have not used JSON. Thank you

2 answers

0

  • 2

    Welcome to Stackoverflow Andre. Please always put the code directly in your answer, rather than putting a link with the solution, because if in the future the link breaks, your answer will no longer be useful to the community.

  • 1

    Whoa, what’s up, Andre? It would be interesting to explain a little about the operation and how to use in your reply, leaving only the link can leave your answer vague in the future when that link ceases to exist.

0

It all depends on how you are providing this information in PHP.

A simple way would be to add a chamda to jQuery.get() to seek such information.

var echartLine = echarts.init(document.getElementById('echart_line'), theme),
    chartData;

// Faz a chamada ao servidor para buscar os dados do PHP
$.get('/caminho/da/url.php', {dataType: 'json'}, function(retorno) {
    chartData = retorno;
});
...
data: chartData

and in PHP you process the data and return with:

$dados = array(); // aqui um array com os dados que você precisa
header('Content-Type: application/json');
echo json_encode($dados);
exit();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.