php - highcharts不显示图表

标签 php html mysql highcharts

我尝试使用 php 和 highcharts api 打印存储在我的数据库中的结果。但是图表没有显示在屏幕上。甚至没有显示轴。数据是从 mysql 数据库获取的。我尝试使用与在 highcharts 演示中,但没有运气。
这是我的代码

<?php
require_once ('connection.php');
session_start();
$username =  $_SESSION['username'];
$quizes=null;
$score=array();
$i=0;

$result = mysql_query("SELECT * FROM `score` WHERE `username`='$username'") or die(mysql_error);
while($rows=mysql_fetch_array($result)) {
$quizes= $quizes. "'".$rows['quiz']."',";
$score[$i]=$rows['score'];
$i=$i+1;
}
print_r($score);
echo $quizes;
?>
<html>
<body>
<script src="js/jquery.js"></script>
<script src="highcharts/js/highcharts.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//passing php variables to javascript variables
   //eg  var mk1=<?php echo $mark1 ?>;
 var mk1=<?php echo $score[1] ?>;
 var mk2=<?php echo $score[2] ?>;
 var mk3=<?php echo $score[3] ?>;
 var mk4=<?php echo $score[4] ?>;
 var mk5=<?php echo $score[5] ?>;
 var mk6=<?php echo $score[6] ?>;
 var mk7=<?php echo $score[7] ?>;
 var mk8=<?php echo $score[8] ?>;    
  var chart1 = new Highcharts.Chart({
    chart: {
       renderTo: 'graphDiv',
       defaultSeriesType: 'column'
    },
     title: {
       text: 'SEMESTER'
    },
    xAxis: {
      categories: ['QUIZ A', 'QUIZ B',  'QUIZ C', 'QUIZ D', 'QUIZ E', 'QUIZ F','QUIZ G','QUIZ  H']
    },
    yAxis: {
       title: {
          text: 'Percentage'
      }
    },
    series: [{
       name: ['Quiz Progress'],
       data: [mk1, mk2, mk3, mk4, mk5, mk6, mk7, mk8]
    },]
   });
 });
 </script>
 <div id="graphDiv" style="width: 700px; height: 400px; float: left"></div>
  </body>
  </html>

最佳答案

这里是从 highchart 中的 mysql 数据库获取数据的示例。 以免开始于 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导入数据并将其提供给我们的图表

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

data.php 将简单地在页面上打印我们将用于图表的值。 您可以将此方法用于同一页面上的多个图表,并且多个图表不再需要更多文件。

它将输出类似 enter image description here

Click Here获取更多帮助或下载示例。

关于php - highcharts不显示图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22858706/

相关文章:

php - 如何获取 PHP 到 AJAX 的响应?

html - 居中容器并使宽度等于其 float 内容

php - 获取 3 天前发布的帖子的正确方法?

mysql - 在 ORDER BY 中使用聚合函数与已经聚合的列之间是否存在性能差异?

php - PDO 请求导致 500 内部服务器错误

php - 如何在 PHP 中获取已连接客户端的 MAC 和 IP 地址?

php - ServiceStack JsonSerializer.DeserializeFromString 不适用于 UTF-8 字符串

javascript - 下拉菜单落后于 'content' div

css - 为什么 IE 7 中的 div 显示与其他浏览器不同?

mysql - MySQL 查询错误