【问题标题】:Morris chart with JSON data带有 JSON 数据的莫里斯图
【发布时间】:2016-06-16 22:17:07
【问题描述】:

我有此代码用于将 db 查询(sql server)输出到本地目录上的 .json 文件。

    $connectionInfo = array("");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    if( $conn )
        {
      }else
        { echo "Connection could not be established.\n";
                die( print_r( sqlsrv_errors(), true));
      }

   $sql="";
   $query = sqlsrv_query( $conn, $sql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

    if( $query === false ) {
        echo "Error in executing query.</br>";
            die( print_r( sqlsrv_errors(), true));
    }

    $json = array();

    do {
        while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
            $json[] = $row;
        }
    } while ( sqlsrv_next_result($query) );

        file_put_contents('data.json', json_encode($json));

这部分工作正常,它以这种格式输出 json 数据。

[{"month":1,"a":43,"b":10},{"month":2,"a":43,"b":87},{"month":3,"a":52,"b":11},{"month":4,"a":66,"b":25},{"month":5,"a":30,"b":11},{"month":6,"a":29,"b":10},{"month":7,"a":32,"b":41},{"month":8,"a":27,"b":36},{"month":9,"a":36,"b":32},{"month":10,"a":57,"b":57},{"month":11,"a":32,"b":20},{"month":12,"a":53,"b":65}]

在另一个页面上,我正在使用 JSON 数据初始化此 Morris-area-chart,如下所示:

首先我正在读取 JSON 文件并循环遍历数组。

$final = json_decode(file_get_contents('data.json'));
    $new_final = array();

    foreach($final as $value) {
        foreach($value as $sub_value) {
            $new_final[] = $sub_value;
        }
    }

        echo json_encode($new_final);
        exit;
    }

第二次使用 (document.url) 在同一页面上使用 Ajax 初始化莫里斯图:

$(document).ready(function(){

    $.ajax({
        url: document.URL,
        dataType: 'JSON',
        type: 'POST',
        data: {get_values: true},
        success: function(response) {

         Morris.Area({
                element: 'area-chart',
                data: response,
                xkey: 'month',
                ykeys: ['a','b'],
                labels: ['Month', 'Total']
            });
        }
    });

});

它没有输出图表。我确实发现,当您操作 .JSON 文件并添加额外的“[]”时,它可以正常工作并按预期输出,如下所示:

[[{"month":1,"a":43,"b":10},{"month":2,"a":43,"b":87},{"month":3,"a":52,"b":11},{"month":4,"a":66,"b":25},{"month":5,"a":30,"b":11},{"month":6,"a":29,"b":10},{"month":7,"a":32,"b":41},{"month":8,"a":27,"b":36},{"month":9,"a":36,"b":32},{"month":10,"a":57,"b":57},{"month":11,"a":32,"b":20},{"month":12,"a":53,"b":65}]]

谁能告诉我我在这里监督什么?看起来我需要第二个数组,但我不确定.. 提前致谢。

【问题讨论】:

    标签: php json morris.js


    【解决方案1】:

    我想通了,

    $final = json_decode(file_get_contents('data.json'));
        $new_final = array();
    
        foreach($final as $value) {
            foreach($value as $sub_value) {
                $new_final[] = $sub_value;
            }
        }
    
            echo json_encode($new_final);
            exit;
        }
    

    应该是

    $final = json_decode(file_get_contents('../php/data.json'));
        $new_final = array($final);
    
        foreach($final as $value) {
    
        }
            echo json_encode($final);
            exit;
        }
    

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多