javascript Highcharts 和 Mysql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3722784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Highcharts & Mysql
提问by hch
I need help with this code,I manage to extract data from a mysql database and convert them to a format required by Highcharts.
我需要此代码的帮助,我设法从 mysql 数据库中提取数据并将它们转换为 Highcharts 所需的格式。
<?php
$query =mysql_query("select
date_format(connect_time,'%Y-%m-%d %H %i') AS date,
Customers.name as customer,
Sum(duration) as secondes
from CDR_Vendors
inner join Customers on (CDR_Vendors.i_customer = Customers.i_customer)
where
i_vendor='32'
and
connect_time between '2010-09-01 00:00:00' and '2010-09-01 00:10:00'
group by date
ORDER BY date", $link) or die(mysql_error());
$row = mysql_fetch_assoc($query);
$customer[] = $row['customer'];
$json_secondes = array();
$json_date = array();
do{
$secondes[] = $row['secondes'];
array_push($json_secondes, $row['secondes']);
array_push($json_date, $row['date']);
}
while($row = mysql_fetch_assoc($query));
//echo json_encode($json_secondes,$row);
//echo json_encode($json_date,$row);
//echo join($secondes, ', ');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<!-- 1. Add these JavaScript inclusions in the head of your page -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="../js/highcharts.js"></script>
<!-- 1a) Optional: the exporting module -->
<script type="text/javascript" src="../js/modules/exporting.js"></script>
<!-- 2. Add the JavaScript to initialize the chart on document ready -->
<script type="text/javascript">
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'column'
},
title: {
text: 'Monthly Average Rainfall'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: <?php echo json_encode($json_date,$row);?>
},
yAxis: {
min: 0,
title: {
text: 'Rainfall (mm)'
}
},
legend: {
layout: 'vertical',
backgroundColor: '#FFFFFF',
align: 'center',
verticalAlign: 'top',
x: 100,
y: 70
},
tooltip: {
formatter: function() {
return ''+
this.x +': '+ this.y +' Min';
}
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: '<?php echo join($customer, ', ');?>',
data: [<?php echo join($secondes, ', ');?>]
}]
});
});
</script>
</head>
<body>
<!-- 3. Add the container -->
<div id="container" style="width: 1300px; height: 500px; margin: 0 auto"></div>
</body>
</html>
The problem with this code is that it's only displays data from a single customer, so that the query returns data from multiple Customers.
此代码的问题在于它仅显示来自单个客户的数据,因此查询会返回来自多个客户的数据。
this method is good or there is another easier way to do that?
这种方法很好还是有另一种更简单的方法来做到这一点?
回答by Jono
Try using implode(', ', $customer);and implode(', ', $secondes);instead of the joinfunctions.
尝试使用implode(', ', $customer);和implode(', ', $secondes);代替join函数。
回答by Vasil Dakov
You can check this:
你可以检查这个:
Create a CSV File for a user in PHP
I hope this will help you with the CSV
我希望这会对您的 CSV 有所帮助

