gpt4 book ai didi

php - highchart 无法从 mysql 获取数据

转载 作者:行者123 更新时间:2023-11-29 13:21:54 24 4
gpt4 key购买 nike

我正在创建漏斗图,它使用jquery从mysql表获取数据。它适用于其他所有highcharts图表,但在漏斗图中它失败了。这是我的代码

index.php

<script type="text/javascript" src="high-js/funnel-chart.js"></script>    
<div id="topcustomer" style="height: 500px;"></div>

漏斗图.js

$(function () {

var curcust = new Highcharts.Chart({
chart: {
renderTo:'topcustomer',
type: 'funnel',
marginRight: 100
},
title: {
text: 'Top 5 Customer',
x: -50
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
format: '<b>{point.name}</b> ({point.y:,.0f})',
color: 'black',
softConnector: true
},
neckWidth: '30%',
neckHeight: '25%'

//-- Other available options
// height: pixels or percent
// width: pixels or percent
}
},
legend: {
enabled: false
},
series: [{
name: 'Current Month'
}]
});

jQuery.get('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/);
cust = line[0] ;
traffic.push([
cust,
line[1]
]);
});
} catch (e) { }
curcust.series[0].data = traffic;
chart = new Highcharts.Chart(curcust);
});
});

data.php

$result = mysql_query("SELECT customer, SUM(amount) AS amo FROM `yearly_sales` GROUP BY customer LIMIT 5");
while($row = mysql_fetch_array($result)) {
echo $row['customer'] . "\t" . $row['amo']. "\n";
}

它没有显示任何错误,也没有生成图表。我的代码有什么错误吗?请帮我解决这个问题谢谢

最佳答案

编辑 - php 应该类似于

$data = array();
while($row = mysql_fetch_array($result)) {
$data[] = array( $row['customer'], $row['amo'] );
}
echo json_encode($data);

这仍然有点困惑,但您可以在获得数据后尝试实例化图表。

$(function () {

jQuery.get('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/);
cust = line[0] ;
traffic.push([
cust,
line[1]
]);
});
} catch (e) { }

$('#topcustomer').highcharts({
chart: {
type: 'funnel',
marginRight: 100
},
title: {
text: 'Top 5 Customer',
x: -50
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
format: '<b>{point.name}</b> ({point.y:,.0f})',
color: 'black',
softConnector: true
},
neckWidth: '30%',
neckHeight: '25%'

//-- Other available options
// height: pixels or percent
// width: pixels or percent
}
},
legend: {
enabled: false
},
series: [{
name: 'Current Month',
data: traffic
}]
});
});
});

关于php - highchart 无法从 mysql 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20702459/

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