高图折线图值从数据库


HighChart Line Graph value from database

我想用HighChart绘制一个页面。我从数据库中获取数据。我用json_encode编写它。我可以看到xxx的价值。它的json格式。但是我的图表不起作用。我认为var data = <?php echo json_encode($xxx); ?>不工作和返回值。我哪里错了?

这是我的php代码数据库值:

<?php
$var = "SELECT SUBSTRING(KayitTarihi,1,4) AS year,SUBSTRING(KayitTarihi,6,2) AS month,SUBSTRING(KayitTarihi,9,2) AS day,SUBSTRING(KayitTarihi,12,2) AS saat,SUBSTRING(KayitTarihi,15,2) AS dakika,Guc FROM Urun WHERE Date(KayitTarihi)='"".$link_m."'"";
$result = $mysqli->query($var);
$data = array();
if ($result->num_rows > 0) { 
    while($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
}
$no = 1;
$total_deger=count($data); 
foreach($data as $dat)
{
$xxx ="[Date.UTC(".$dat['year'].",".$dat['month'].",".$dat['day'].",".$dat['saat'].",".$dat['dakika'].",00),".$dat['Guc']."]";
if($no < $total_deger)
{
    echo ",";
}
echo json_encode($xxx);
}

//free memory associated with result
$result->close();

//close connection
$mysqli->close();
?>

下面是highchart脚本:

<script>
$(function () {
var data = <?php echo $xxx; ?>;
        Highcharts.chart('container', {
            chart: {
                zoomType: 'x'
            },
            title: {
                text: 'USD to EUR exchange rate over time'
            },
            subtitle: {
                text: document.ontouchstart === undefined ?
                        'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in'
            },
            xAxis: {
                type: 'datetime'
            },
            yAxis: {
                title: {
                    text: 'Exchange rate'
                }
            },
            legend: {
                enabled: false
            },
            plotOptions: {
                area: {
                    fillColor: {
                        linearGradient: {
                            x1: 0,
                            y1: 0,
                            x2: 0,
                            y2: 1
                        },
                        stops: [
                            [0, Highcharts.getOptions().colors[0]],
                            [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
                        ]
                    },
                    marker: {
                        radius: 2
                    },
                    lineWidth: 1,
                    states: {
                        hover: {
                            lineWidth: 1
                        }
                    },
                    threshold: null
                }
            },
            series: [{
                type: 'area',
                name: 'USD to EUR',
                data: [data]
                }]
        });
    });
</script>

编辑:我编辑json格式。当我写echo时。它只看到一个值。编辑2:我有9个价值。输出是:

,,,,,,,,[Date.UTC(2016,11,15,10,28,00),0]
[Date.UTC(2016,11,15,14,25,00),0]
[Date.UTC(2016,11,15,14,25,00),0]
[Date.UTC(2016,11,15,14,27,00),17]
[Date.UTC(2016,11,15,18,32,00),54]
[Date.UTC(2016,11,15,18,32,00),54]
[Date.UTC(2016,11,15,18,33,00),93]
[Date.UTC(2016,11,15,18,33,00),34]
[Date.UTC(2016,11,15,18,34,00),34]

但是当我读取docs for highchart时,我的值必须是

([
[Date.UTC(2016,11,15,10,28,00),0],
[Date.UTC(2016,11,15,14,25,00),0],
[Date.UTC(2016,11,15,14,25,00),0],
[Date.UTC(2016,11,15,14,27,00),17],
[Date.UTC(2016,11,15,18,32,00),54],
[Date.UTC(2016,11,15,18,32,00),54],
[Date.UTC(2016,11,15,18,33,00),93],
[Date.UTC(2016,11,15,18,33,00),34],
[Date.UTC(2016,11,15,18,34,00),34]
]);

试着用引号括起来

var data = "<?php echo json_encode($xxx); ?>"

您应该在控制台

中看到此错误。

你也没有正确连接。这意味着每次循环运行时,您都会覆盖存储在$xxx

中的最后一项。
$xxx = "[Date.UTC(".$dat['year'].",".$dat['month'].",".$dat['day'].",".$dat['saat'].",".$dat['dakika'].",00),".$dat['Guc']."]";

应为

$xxx .= "[Date.UTC(".$dat['year'].",".$dat['month'].",".$dat['day'].",".$dat['saat'].",".$dat['dakika'].",00),".$dat['Guc']."]";

最后,把你的循环改成这样

$xxx = '';
foreach($data as $dat){
    // if not a blank string i.e. something added, add a comma to the end read for the next concatenation
    if($xxx != '') $xxx .= ",";
    // concatenate
    $xxx .= "[Date.UTC(".$dat['year'].",".$dat['month'].",".$dat['day'].",".$dat['saat'].",".$dat['dakika'].",00),".$dat['Guc']."]";
}
// if not blank string, echo
if($xxx != ''){
    echo json_encode($xxx);    
}