【问题标题】:HighChart Line Graph value from database数据库中的 HighChart 折线图值
【发布时间】:2016-11-18 12:32:29
【问题描述】:

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

这是我的数据库值的 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 格式。当我写只是回声。它只看到一个值。 Edit2:我有 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]

但是当我阅读 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]
]);

【问题讨论】:

    标签: javascript php highcharts


    【解决方案1】:

    试着用引号括起来

    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);    
    }
    

    【讨论】:

    • 我的图表仍然无法正常工作。我认为我的价值应该像 ?([ [[Date.UTC(2013,5,2),0.7695], [Date.UTC(2013,5,3),0.7648],]);但我的价值是:,,,,,,,,,,[Date.UTC(2016,11,03,08,18,00),12][Date.UTC(2016,11,03,08,32, 00),12][日期.UTC(2016,11,03,08,32,00),12]
    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多