首页 > 解决方案 > 将数据从mysql动态加载到Javascript表

问题描述

我用 JavaScript(Canvas Js) 制作了这张图表

图表

代码如下

    <script>

        window.onload = function () {

            var chart = new CanvasJS.Chart("chartContainer", {
                theme:"light2",
                animationEnabled: true,
                title:{
                    text: "PM"
                },
                axisY :{
                    includeZero: false,
                    title: "Heures",
                    suffix: "H"
                },
                toolTip: {
                    shared: "true"
                },
                legend:{
                    cursor:"pointer",
                    itemclick : toggleDataSeries
                },
                data: [
                    {
                        type: "spline",
                        showInLegend: true,
                        yValueFormatString: "##.00H",
                        name: "Preventive",
                        dataPoints: [
                            { label: "ABC", y: 2 },
                            { label: "DEF", y: 2 },
                            { label: "AAA", y: 2 }
                        ]
                    },
                    {
                        type: "spline",
                        showInLegend: true,
                        yValueFormatString: "##.00H",
                        name: "Curative",
                        dataPoints: [
                            { label: "ABC", y: 10 },
                            { label: "DEF", y: 2.27 },
                            { label: "AAA", y: 1.25 }
                        ]
                    }]
            });
            chart.render();

            function toggleDataSeries(e) {
                if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible ){
                    e.dataSeries.visible = false;
                } else {
                    e.dataSeries.visible = true;
                }
                chart.render();
            }
        }
    </script>
</head>
<body>

<div id="chartContainer" style="height: 370px; max-width: 920px; margin: 0px auto;"></div>
<script src="../../canvasjs.min.js"></script>
</body>
</html>

正如您所说,数据是静态的。

但是,当我想用​​ mysql 查询动态加载数据时

 dataPoints: [
                            <?php

                            $sq = "select machine,timediff from action_archiv_tech WHERE (date_tech BETWEEN '2018-09-10' AND '2018-10-06')";
                            $r = mysql_query($sq) or (die(mysql_error()));
                            while($tab = mysql_fetch_assoc($r)) {
                            $machine = $tab['machine'];
                            $diff = $tab['diff'];
                            ?>
                            { label: "<?php echo $machine ?>", y: <?php echo $diff ?> }

                            <?php } ?>
                        ]

页面一片空白,你能帮我找出我哪里出错了吗?

标签: javascriptphpmysqlcharts

解决方案


推荐阅读