php - Google 图表 - 绘制空值

标签 php javascript json google-visualization

我创建了一个折线图,它使用 php 和 json 加载数据。

问题是图表将 NULL 值绘制为 0,看起来非常难看。

我最好的猜测是我以错误的方式创建了 json,我真正想要的输出是 {"v":} 而不是 {"v":""}。

现在的折线图: enter image description here

我想要的: enter image description here

php代码

   $reports['cols'][] = array(label => "Time", type => "string");
        $reports['cols'][] = array(label => "Today", type => "number");
        $reports['cols'][] = array(label => "Yesterday", type => "number");
        $reports['cols'][] = array(label => "Budget", type => "number");

            //Some mysql code that populates values ($today, $yesterday, $budget)

        $rows = array();
        for($i=10; $i <= 21; $i++){

            $temp = array();
            $temp[] = array('v' => (int) $i);
                    //Format string will return "" and not "0" if value is blank
            $temp[] = array('v' => (string) $today["$i"]);
            $temp[] = array('v' => (string) $yesterday["$i"]);
            $temp[] = array('v' => (string) $budget);
            $rows[]['c'] = $temp;
        }

        $reports['rows'] = $rows;
        $string=json_encode($reports);
        echo $string;

php 的输出:

{
    "cols":[
        {"label":"Time","type":"string"},
        {"label":"Today","type":"number"},
        {"label":"Yesterday","type":"number"},
        {"label":"Budget","type":"number"}
    ],

    "rows":[
        {"c":[{"v":10},{"v":"0"},{"v":"0"},{"v":"90000"}]},
        {"c":[{"v":11},{"v":"22491"},{"v":"7420"},{"v":"90000"}]},
        {"c":[{"v":12},{"v":""},{"v":"50082"},{"v":"90000"}]},
        {"c":[{"v":13},{"v":""},{"v":"91660"},{"v":"90000"}]},
        {"c":[{"v":14},{"v":""},{"v":"109204"},{"v":"90000"}]},
        {"c":[{"v":15},{"v":""},{"v":"115280"},{"v":"90000"}]},
        {"c":[{"v":16},{"v":""},{"v":"111853"},{"v":"90000"}]},
        {"c":[{"v":17},{"v":""},{"v":"87368"},{"v":"90000"}]},
        {"c":[{"v":18},{"v":""},{"v":"33063"},{"v":"90000"}]},
        {"c":[{"v":19},{"v":""},{"v":"14903"},{"v":"90000"}]},
        {"c":[{"v":20},{"v":""},{"v":"1441"},{"v":"90000"}]},
        {"c":[{"v":21},{"v":""},{"v":"0"},{"v":"90000"}]}
    ]
}

Javascript

google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);
    function drawChart() {

    var jsonData = $.ajax({
        url: "functions/load.php?q=salesChart",
        dataType:"json",
        async: false
    }).responseText;

    // Create our data table out of JSON data loaded from server.
    var data = new google.visualization.DataTable(jsonData);

    var options = {
        title: 'Money / Hour',
        colors: ['#4BA15A', '#45559D', 'red'],
        legend: {position: 'top'},
        lineWidth: 2,
        pointSize: 3,
        fontName: 'Helvetica',
        fontSize: 10,
        interpolateNulls: false,
        backgroundColor: {strokeWidth: 1},
        chartArea: {left: 70, top: 60, width: "85%", height: "70%"} 
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

最佳答案

我用 {"v":null} 替换了 {"v":""} 并且知道它看起来非常好!

jsonData=jsonData.replace(/\"\"/g, 'null');

结果是没有绘制 0 的图表 enter image description here

关于php - Google 图表 - 绘制空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18570975/

相关文章:

php - 如何将数据从 JavaScript 发送到 PHP,反之亦然?

php - 根据需要更新信息

javascript - Chrome 扩展用 newtab.html 覆盖 newtab,然后访问 cookie

php - MySQL 查询问题 - INSERT INTO

php - 在 $wpdb 查询中传递 PHP 变量

php - Symfony Generator Forms、Doctrine 和 M :N Relationships

php - 返回数据库中插入的 n 行

javascript - 如何在 flutter 项目的 web 文件夹中正确使用 js 文件中的 env 变量

php - 来自 php 的 Google Table Chart 创建了 Json

php - 使用带有 php 和 mysql 的 http 请求创建 android 应用程序