我正在尝试使用高级图表绘制折线图。我面临一个奇怪的问题,即从数据库中取出的内容或数据未显示或图形根本没有绘制。 我已经通过错误报告检查了没有任何 PHP 错误。控制台上也没有任何错误。 但是当我访问我的data.php文件时。它显示了这样的内容
[{"name":"Month","data":["Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Mar","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","Apr","","Jul","Jul","Jul","Jul","Jul","Jul","Jul","Jul","Jul","Jul","Jul","Jul","Jul","Jul"]},
{"name":"Name","data":["Direct Sales","Search Engine Marketing","PPC Advertising","Website Marketing","Blog Marketing","Social Media Marketing","Email Marketing","Online PR","Multimedia Marketing","Mobile Marketing","Display Advertising","Direct Sales","Search Engine Marketing","PPC Advertising","Website Marketing","Blog Marketing","Social Media Marketing","Email Marketing","Online PR","Multimedia Marketing","Mobile Marketing","Display Advertising"]}]
实际上这是我希望在图表上显示的内容。 我没有找到任何解决方案。 请帮助我知道我哪里出错了。 希望我已经很好地解释了我的问题!
的index.php
<?php
ini_set('display_errors', 1);
include './includes/db_connection.php';
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var chart;
$(document).ready(function() {
$.getJSON("data.php", function(json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Month Vs Names',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Amount'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: json
});
});
});
});
</script>
</head>
<body>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>
data.php
ini_set('display_errors', 1);
include './includes/db_connection.php';
//$check = "SELECT month FROM activities";
$sth1 = mysqli_query($link,"SELECT month FROM activities");
//print_r($sth1);
//die($check."<br/><br/>".mysql_error());
$rows = array();
$rows['name'] = 'Month';
while($r = mysqli_fetch_array($sth1)) {
$rows['data'][] = $r['month'];
}
//$check1 = ;
$sth = mysqli_query($link,"SELECT name FROM web_marketing");
//print_r($sth);
//die($check."<br/><br/>".mysql_error());
$rows1 = array();
$rows1['name'] = 'Name';
while($rr = mysqli_fetch_assoc($sth)) {
$rows1['data'][] = $rr['name'];
}
$result = array();
array_push($result,$rows);
array_push($result,$rows1);
echo json_encode($result,JSON_NUMERIC_CHECK);
答案 0 :(得分:1)
您需要检查在系列对象中设置的json
的格式。
series: json
首先,Series对象的数据必须是数字格式(等等0.23或6)。
我并不知道你想在哪里设置显示那些内容(图表上的标题或工具提示?),
无论如何,如果您设置 name's value
系列对象或类别对象xAxis ,则内容将显示在图表中。
&#34;直接销售&#34;,&#34;搜索引擎营销&#34;,&#34; PPC广告&#34;,&#34;网站营销&#34;,&#34 ;博客营销&#34;,&#34;社交媒体营销&#34;,&#34;电子邮件营销&#34;,&#34;在线公关&#34;,&#34;多媒体营销&#34;,&# 34;移动营销&#34;,&#34;展示广告&#34;,&#34;直销&#34;,&#34;搜索引擎营销&#34;,&#34; PPC广告&#34;,& #34;网站营销&#34;,#34;博客营销&#34;,#34;社交媒体营销&#34;,&#34;电子邮件营销&#34;,&#34;在线公关&#34;, &#34;多媒体营销&#34;,&#34;移动营销&#34;,&#34;展示广告&#34;
我在下面写了一个图表示例。希望它对你有用。
http://jsfiddle.net/uv7cvfsb/1/
另外,也许这张照片有助于理解Highchart的结构。
http://4.bp.blogspot.com/-8U9LxYsbp1k/UDUYsSJxN_I/AAAAAAAAAtk/SNWsEJUIGu8/s1600/highcharts.png
如果您想询问不同的事情,请发表评论。