轴#0的数据列不能是字符串类型 - 如何将JSON响应转换为int

时间:2016-11-15 17:27:37

标签: angularjs charts google-visualization

我正在使用区块链api:https://api.blockchain.info/charts/market-price?timespan=5weeks&rollingAverage=8hours&format=json来获取数据。

数据采用JSON格式:

   {
      "status": "ok",
      "name": "Market Price (USD)",
      "unit": "USD",
      "period": "day",
      "description": "Average USD market price across major bitcoin exchanges.",
      "values": [
        {
          "x": 1476230400,
          "y": 635.0139375
        },
        {
          "x": 1476316800,
          "y": 635.9650875
        },
        {
          "x": 1476403200,
          "y": 639.5656428571428
        },
        {
          "x": 1476489600,
          "y": 637.9498571428572
        },
        {
          "x": 1476576000,
          "y": 641.425125
        },
        {
          "x": 1476662400,
          "y": 638.1833875
        },
        {
          "x": 1476748800,
          "y": 636.2965
        },
        {
          "x": 1476835200,
          "y": 629.253675
        },
        {
          "x": 1476921600,
          "y": 630.22705
        },
        {
          "x": 1477008000,
          "y": 631.9242125
        },
        {
          "x": 1477094400,
          "y": 655.4886625
        },
        {
          "x": 1477180800,
          "y": 653.0028625
        },
        {
          "x": 1477267200,
          "y": 651.396425
        },
        {
          "x": 1477353600,
          "y": 655.3199500000001
        },
        {
          "x": 1477440000,
          "y": 672.2214125
        },
        {
          "x": 1477526400,
          "y": 682.2239625
        },
        {
          "x": 1477612800,
          "y": 687.6883375
        },
        {
          "x": 1477699200,
          "y": 714.89545
        },
        {
          "x": 1477785600,
          "y": 698.0033999999999
        },
        {
          "x": 1477872000,
          "y": 702.0015125
        },
        {
          "x": 1477958400,
          "y": 728.2068875
        },
        {
          "x": 1478044800,
          "y": 733.336125
        },
        {
          "x": 1478131200,
          "y": 686.170875
        },
        {
          "x": 1478217600,
          "y": 703.6940875
        },
        {
          "x": 1478304000,
          "y": 704.7909
        },
        {
          "x": 1478390400,
          "y": 712.00325
        },
        {
          "x": 1478476800,
          "y": 703.819
        },
        {
          "x": 1478563200,
          "y": 708.974875
        },
        {
          "x": 1478649600,
          "y": 720.9301
        },
        {
          "x": 1478736000,
          "y": 713.690125
        },
        {
          "x": 1478822400,
          "y": 715.4593
        },
        {
          "x": 1478908800,
          "y": 703.718
        },
        {
          "x": 1478995200,
          "y": 701.901125
        },
        {
          "x": 1479081600,
          "y": 706.467875
        }
      ]
    }

在我的角度代码中,我正在转换x& y使用arry到数据表坐标到数据表:

function drawChart() {
                alert("drawchart $scope.lobs.length"+$scope.lobs.length);
                alert("drawchart $scope.lobs"+$scope.lobs);

            var jsonData = google.visualization.arrayToDataTable([$scope.lobs]);

            alert("drawchart jsonData"+JSON.stringify(jsonData));

            var options = {
            title: 'Company Performance',
            curveType: 'function',
            legend: { position: 'bottom' }
            };
            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
            chart.draw(tdata, options);
            };

我打印时

alert("drawchart jsonData"+JSON.stringify(jsonData));

我可以看到数据以字符串形式出现。我收到错误

轴#0的数据列不能是字符串类型。

有人可以建议如何更改来自JSON响应的数据,以便正确显示图表吗?

1 个答案:

答案 0 :(得分:0)

当第二列值以错误的格式提供时,通常会发生此类错误:它必须是 number 类型。

根据您的输入数据,它可以转换为兼容的Google图表格式,如下所示:

var data = {
      "status": "ok",
      "name": "Market Price (USD)",
      "unit": "USD",
      "period": "day",
      "description": "Average USD market price across major bitcoin exchanges.",
      "values": [
        {
          "x": 1476230400,
          "y": 635.0139375
        },
        {
          "x": 1476316800,
          "y": 635.9650875
        },
        {
          "x": 1476403200,
          "y": 639.5656428571428
        }
        //the remaining data is ommited for clarity
    ]    
};

 var chartData = [];
 chartData.push(['Number','USD']);
 data.values.forEach(function(item){
      chartData.push([item.x,item.y]);    
 });

document.getElementById('output').innerHTML = JSON.stringify(chartData,null,2);
<pre id="output"/>

Demo