MSSQL Server到PHP数组到JSON编码到Highcharts

问题描述 投票:0回答:1

我有从MSSQL Server到JSON_Encode的非常简单的数据。

这是我的PHP代码(位于myPHPFile.php中:):

<?php
$serverName = "MyServer";
$connectionInfo = array( "Database"=>"MyDatabase", "UID"=>"MyUID", "PWD"=>"MyPWD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);


$tsql = "SELECT * FROM [MyDatabase].[dbo].[MyView] ORDER BY Year";  

$stmt = sqlsrv_query( $conn, $tsql); 

$rows = array();
while($r = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    $res[] = $r;
}

print json_encode($res, JSON_NUMERIC_CHECK);

sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);  
?> 

给我以下打印:

[{"Year":2016,"Number":41},{"Year":2017,"Number":512},{"Year":2018,"Number":1895},{"Year":2019,"Number":3132}] 

太好了。有数据。

我已经尝试了每一个教程,每一个highcharts论坛帖子以及每一个stackoverflow问题,以JSON格式将我的php文件中的这些简单数据都转换成Highcharts图表。也许我缺少明显的东西。

所以,让我们看一下我的HTML文件:

在头上:

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script type="text/javascript">
$(function () {
    var chart;
    $(document).ready(function() {
        $.getJSON("myPHPFile.php", function(json) {

            chart = new Highcharts.Chart({
                chart: {
                    renderTo: 'container',
                    type: 'line',
                },
                xAxis: {
                    title: { text: 'Year'}
                },
                yAxis: {
                    title: {
                        text: 'Number'
                    },
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                series: json
            });
        });

    });

});
        </script>

然后显然是我的div

<div id="container"></div>

我想念的是什么? HTML窗口只是空白。未显示图表。

javascript php json sql-server highcharts
1个回答
0
投票

Highchart examples显示了另一个设置图表的示例。可以使用以下格式:

$(document).ready(function() {
  $.getJSON("myPHPFile.php", function(json) {
    var series = json.map(function(record){
        return [record.Year, record.Number];
    })

    Highcharts.chart('container', {
      chart: {
        renderTo: 'container',
        type: 'line',
      },
      xAxis: {
        title: {
          text: 'Year'
        }
      },
      yAxis: {
        title: {
          text: 'Number'
        },
      },
      series: [{
        data: series
      }],
    });

  });
});

查看下面的实时演示:

const data = [{
  "Year": 2016,
  "Number": 41
}, {
  "Year": 2017,
  "Number": 512
}, {
  "Year": 2018,
  "Number": 1895
}, {
  "Year": 2019,
  "Number": 3132
}];

const series = data.map(record => [record.Year, record.Number])

Highcharts.chart('container', {
  chart: {
    renderTo: 'container',
    type: 'line',
  },
  xAxis: {
    title: {
      text: 'Year'
    }
  },
  yAxis: {
    title: {
      text: 'Number'
    },
  },
  series: [{
    data: series
  }],
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container"></div>
© www.soinside.com 2019 - 2024. All rights reserved.