doq8211 2013-11-10 13:24
浏览 42
已采纳

Highcharts无法使用装有ajax的数组进行渲染

I just started using Highcharts but already got a problem. I want to fill my graph with data from an Ajax call but it just shows a blank graph.

First i want to show you how I get my data:

<?php

require_once("mysql_config.php");
$query = "SELECT temp FROM daten ORDER BY daten.id DESC LIMIT 0,6";

$answer = mysqli_query($mysqli,$query);
$row;

while($row = $answer->fetch_array()){
    echo $row['temp'] . ",";
}

?>

The output of this looks like: -20,0,0,7,0,0,100

And my AJAX Function:

function ajax(website, element, callback) {
    var http = null;
    var http = new XMLHttpRequest();
    var url = website;

    if(http!=null){
        http.open("POST",url,true);
        http.onreadystatechange = function(){

            if(http.readyState == 4){

                var text = http.responseText;
                callback(text, element);

            }

        }//Ende readychange

        http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        http.send();

    }//Ende if(http2!=null)

}//Ende Funktion

the callback is my create_chart function

function create_chart(rawdata, element){

newdata = rawdata.split(",");

//var newdata = [-20,0,0,7,0,0,100];

var chart = new Highcharts.Chart({
        chart: {
            type: 'line',
            renderTo: 'test'
        },
        title: {
            text: 'Testchart'
        },
        subtitle:{
            text: 'Subtitle'
        },
        xAxis: {
            categories: ['A','B','C','D','E','F','G']
        },
        yAxis: {
            title: {
                text: 'This is it'
            }
        },
        series: [{
            name: "Test",
            data: newdata
        }]
});

 }

The problem is, that it only draws a graph without data while using ajax but when I use an "prefilled" array (like the the one I commented out) it works fine.

  • 写回答

2条回答 默认 最新

  • dongxibo2095 2013-11-10 14:23
    关注

    newdata is a string and when you split it its pieces are strings.

    "-20,0,0,7,0,0,100".split(",") = ["-20","0","0","7","0","0","100"]

    Highcharts needs numbers, so after the split, parseInt them into numbers:

    var rawdata = "-20,0,0,7,0,0,100";
    var strdata = rawdata.split(",");
    var newdata = []
    for (var i = 0; i < strdata.length; i++)
    {
       newdata.push(parseInt(strdata[i]));
    } 
    

    Fiddle here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看