gpt4 book ai didi

php - 折线图不显示数据库中的数据

转载 作者:行者123 更新时间:2023-11-29 11:39:45 25 4
gpt4 key购买 nike

我正在尝试使用 Highcharts 绘制折线图。我面临一个奇怪的问题,即从数据库获取的内容或数据没有显示,或者图表根本没有绘制。没有任何 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>

数据.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);

最佳答案

您需要检查系列对象中设置的json格式。

series: json

首先,Series 对象的数据必须是数字格式(例如 0.23 或 6)。

我不明白你想在哪里设置显示这些内容(图表上的标题或工具提示?)

无论如何,如果您设置Series对象的名称值或xAxis的类别对象,下面的内容将显示在图表中。

“直销”、“搜索引擎营销”、“PPC 广告”、“网站营销”、“博客营销”、“社交媒体营销”、“电子邮件营销”、“在线公关”、“多媒体营销”、“移动营销”、“展示广告”、“直销”、“搜索引擎营销”、“PPC 广告”、“网站营销”、“博客营销”、“社交媒体营销”、“电子邮件营销”、 “在线公关”、“多媒体营销”、“移动营销”、“展示广告”

I have wrote chart's example below. hope that it works for you.

http://jsfiddle.net/uv7cvfsb/1/

also, maybe this picture will help to understand about the structure of Highchart.

http://4.bp.blogspot.com/-8U9LxYsbp1k/UDUYsSJxN_I/AAAAAAAAAtk/SNWsEJUIGu8/s1600/highcharts.png

如果您想问不同的问题,请发表评论。

关于php - 折线图不显示数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36028721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com