echarts:从URL获取数据

时间:2019-01-09 11:43:43

标签: php mysql ajax echarts

我正在其演示页上测试Echarts功能:https://ecomfe.github.io/echarts-examples/public/editor.html?c=line-simple

现在,我希望该图表显示从URL收集的数据。该网址返回“ [820、932、901、934、1290、1330、1320]”。

演示代码(正在运行)是:

option = {
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line'
    }]
};

我的代码(不起作用)是:

option = {
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        url: 'https://www.myurl.com/echartstest.php',
        type: 'line'
    }]
};  

当我在代码中使用URL时,不会返回任何错误,但不会显示该图表。

如有必要,我可以在项目中使用AJAX。

1 个答案:

答案 0 :(得分:1)

var dataArr = [];
     $.get('https://www.myurl.com/echartstest.php', {}, function(response) {
            dataArr = JSON.parse(response);
              initEchart();
            });
// make sure dataArr should be in array like [1,2,3],


 function initEchart(){
       option = {
            xAxis: {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: dataArr          
                type: 'line'
            }]
        };  
      echarts.init(document.getElementById('youtchartId')).setOption(option);
   }
相关问题