我正在使用 php 包装器“highroller”来尝试绘制我在数据库表中的一些数据。
现在,此代码有效(来自示例):
<?php
require_once('../HighRoller/HighRoller.php');
require_once('../HighRoller/HighRollerSeriesData.php');
require_once('../HighRoller/HighRollerLineChart.php');
$chartData = array(5324, 7534, 6234, 7234, 8251, 10324);
$series1 = new HighRollerSeriesData();
$series1->addName('myData')->addData($chartData);
$linechart = new HighRollerLineChart();
$linechart->chart->renderTo = 'linechart';
$linechart->title->text = 'Hello HighRoller';
$linechart->yAxis->title->text = 'Total';
$linechart->addSeries($series1);
?>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<!-- HighRoller: set the location of Highcharts library -->
<?php echo HighRoller::setHighChartsLocation("../highcharts/highcharts.js");?>
</head>
<body>
<div id="linechart"></div>
<script type="text/javascript">
<?php echo $linechart->renderChart();?>
</script>
</body>
但是,我正在尝试编辑 $chartdata = array(5324,...) 以便我可以从数据库中提取它。这是我的尝试:
$connection = mysql_connect(#,#,#) or die("Couldn't connect to the server.");
$database = mysql_select_db(#, $connection) or die ("Couldn't select the correct database.");
$result = mysql_query("SELECT price FROM highroller");
while ($row = mysql_fetch_array($result)){
$priceData[] = $row['price'];
}
$chartData = $priceData;
$linechart = new HighRollerLineChart();
$linechart->chart->renderTo = 'linechart';
$linechart->title->text = 'Line Chart';
$series1 = new HighRollerSeriesData();
$series1->addName('myData')->addData($chartData);
$linechart->addSeries($series1);
现在,我不确定为什么这不起作用,因为 $priceData 是一个数组,就像之前的静态数组一样。
此外,作为参考,这里是库中“addData”的函数:
/** add data to plot in your chart
* @param $chartdata - array, data provided in 1 of 3 HighCharts supported array formats (array, assoc array or mult-dimensional array)
* @return void
*/
public function addData($chartdata){
if(!is_array($chartdata)){
die("HighRoller::addData() - data format must be an array.");
}
$this->series = array($chartdata);
}