【问题标题】:Highcharts - How to populate date with Mysql and PHP?Highcharts - 如何使用 Mysql 和 PHP 填充日期?
【发布时间】:2014-01-19 14:18:06
【问题描述】:

我是 highcharts 和 php 的新手。我试图用数据库中的数据填充高图。我试图播放代码以获得我想要的结果,但我总是失败。当我运行时,我总是得到空白页结果。

请帮助我。我非常需要它。谢谢!

这是我的highchart的完整代码

  <!DOCTYPE HTML>
  <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>POS RESULT</title>

    <script type="text/javascript"   src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="../../js/highcharts.js"></script>
    <script src="../../js/modules/exporting.js"></script>

</head>

        <body>

    <?php
include "config.php";

$SQL1 =     "SELECT * FROM pos";

$result1 = mysql_query($SQL1);
$data1 = array();
while ($row = mysql_fetch_array($result1)) {
   $data1[] = $row['name'];
   $data2[] = $row['Qty'];

}
?>

<script type="text/javascript">
$(function () {
    $('#container').highcharts({
        chart: {
            type: 'column',
            margin: [ 50, 50, 100, 80]
        },
        title: {
            text: 'List of POS'
        },
    credits: {
    enabled: false
    },
        xAxis: {
            categories: [<?php echo join($data1, "','"); ?>],
            labels: {
                rotation: -45,
                align: 'right',
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'No. of Ticket'
            }
        },
        legend: {
            enabled: false,
    layout: 'vertical',
                        backgroundColor: '#FFFFFF',
                        align: 'left',
                        verticalAlign: 'top',
                        x: 50,
                        y: 35,
                        floating: true,
                        shadow: true
        },
        tooltip: {
            pointFormat: '<b>{point.y:.1f} tickets</b>',
        },
     plotOptions: {
                            column: {
                                        pointPadding: 0.2,
                                        borderWidth: 0
                                    }
                        },
        series: [{
            name: 'Qty',
            data: ['<?php echo join($data2, "','"); ?>'],
    dataLabels: {
                enabled: true,
                rotation: -90,
                color: '#FFFFFF',
                align: 'right',
                x: 4,
                y: 10,
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif',
                    textShadow: '0 0 3px black',

                }
            }
        }]
    });
});

    </script>

<div id="container" style="min-width: 500px; height: 400px; margin: 0 auto"></div>

</body>
   </html>

这是我的数据库连接:

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "pos";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not  connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");

?>

【问题讨论】:

    标签: php mysql highcharts


    【解决方案1】:

    确保您拥有data 格式的x,y 格式的高图

    data = [{x:1,y:4500},{x:2,y:20}]
    

    FIDDLE

    【讨论】:

      【解决方案2】:

      在这个系列中你有类似的东西

      ['<?php echo join($data2, "','"); ?>']
      

      这意味着在数组的第一个元素中有字符串,因此不会调用 php。我建议熟悉文章http://www.highcharts.com/docs/working-with-data/data-intro。此外,我建议使用允许 sto 返回 json 的 json_encode,然后在不加入/解析的情况下进入 javascript。

      【讨论】:

        【解决方案3】:

        请尝试以下示例。我觉得可以帮到你

        SQL 表

        CREATE TABLE IF NOT EXISTS `sales` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `month` varchar(200) DEFAULT NULL,
          `amount` varchar(11) DEFAULT NULL,
          PRIMARY KEY (`id`)
        
        ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=118 ;
        
        
        INSERT INTO `sales` (`id`, `month`, `amount`) VALUES
        (24, 'Apr', '15'),
        (25, 'May', '40'),
        (26, 'Jun', '26'),
        (27, 'Jul', '31'),
        (28, 'Aug', '39'),
        (29, 'Sep', '25'),
        (30, 'Oct', '27'),
        (31, 'Nov', ' 32'),
        (32, 'Dec', NULL);
        

        这里我们创建新表并在其中插入一些数据。 现在数据将如下所示

        index.php

        <head>
            <meta name="Gopal Joshi" content="Highchart with Mysql" />
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <title>Highchart with Mysql Database</title>
            <script type="text/javascript" src="js/jquery.min.js"></script>
            <script type="text/javascript" src="js/setup.js"></script>
            <script type="text/javascript" src="js/test.js"></script>    
        </head>
        
        <body>
            <script src="js/highcharts.js"></script>
            <div id="sales" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
        </body>
        

        setup.js

        var chart;
         $(document).ready(function() {
                var cursan = {
           chart: {
            renderTo: 'sales',
            defaultSeriesType: 'area',
            marginRight: 10,
            marginBottom: 20
           },
           title: {
            text: 'Highchart With Mysql',
           },
           subtitle: {
            text: 'www.spjoshis.blogspot.com',
           },
           xAxis: {
            categories: ['Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar']
           },
           yAxis: {
            title: {
             text: 'Average'
            },
            plotLines: [{
             value: 0,
             width: 1,
             color: '#808080'
            }]
           },
           tooltip: {
               crosshairs: true,
                        shared: true
           },
           legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -10,
            y: 30,
            borderWidth: 0
           },
        
                    plotOptions: {
        
                        series: {
                            cursor: 'pointer',  
                            marker: {
                                lineWidth: 1
                            }
                        }
                    },
        
                 series: [{
               color: Highcharts.getOptions().colors[2],
            name: 'Test Colomn',
                        marker: {
                            fillColor: '#FFFFFF',
                            lineWidth: 3,
                            lineColor: null // inherit from series
                        },
                        dataLabels: {
                            enabled: true,
                            rotation: 0,
                            color: '#666666',
                            align: 'top',
                            x: -10,
                            y: -10,
                            style: {
                                fontSize: '9px',
                                fontFamily: 'Verdana, sans-serif',
                                textShadow: '0 0 0px black'
                            }
                        }
           }],
                }
        
                //Fetch MySql Records
                jQuery.get('js/data.php', null, function(tsv) {
           var lines = [];
           traffic = [];
           try {
            // split the data return into lines and parse them
            tsv = tsv.split(/\n/g);
            jQuery.each(tsv, function(i, line) {
             line = line.split(/\t/);
             date = line[0] ;
                                                amo=parseFloat(line[1].replace(',', ''));
                                                if (isNaN(amo)) {
                                                           amo = null;
                                                }
             traffic.push([
              date,
              amo
             ]);
            });
           } catch (e) {  }
           cursan.series[0].data = traffic;
           chart = new Highcharts.Chart(cursan);
          });
         });
        

        js 将从data.php 的mysql 中导入数据并添加到之前在js 中创建的图表中。

        data.php

        $con=mysql_connect('localhost','root','');
        mysql_select_db("test", $con);
        $result=mysql_query('select * from sales order by id');
        while($row = mysql_fetch_array($result)) {
          echo $row['month'] . "\t" . $row['amount']. "\n";
        }
        

        我们的图表已完全加载。带有 mysql 记录,输出将如下所示

        输出

        它的面积图示例,你可以通过更改defaultSeriesType: 'area'来改变图表类型

        Click Here 获取更多源代码示例。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-13
          • 2014-11-29
          相关资源
          最近更新 更多