javascript Chart.js 和 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21558845/
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
Chart.js and PHP
提问by user3271851
I am using Chart.jsto create charts on my site. The data needs to come from a database so PHP needs to be inside the javascript.
我正在使用Chart.js在我的网站上创建图表。数据需要来自数据库,所以 PHP 需要在 javascript 中。
This is what I have: (as just testing not the database part yet)
这就是我所拥有的:(因为还没有测试数据库部分)
<?php
$set1 = 20;
$set2 = 30;
$set3 = 40;
?>
And then to create the pie chart I have:
然后创建饼图我有:
var pieData = [
{
value: <?php echo $set1; ?>,
color:"#F38630"
},
{
value : <?php echo $set2; ?>,
color : "#E0E4CC"
},
{
value : <?php echo $set3; ?>,
color : "#69D2E7"
}
];
var myPie = new Chart(document.getElementById("canvas").getContext("2d")).Pie(pieData);
With this code I recieve this error:
使用此代码,我收到此错误:
Uncaught SyntaxError: Unexpected token < when it reached the first <?php call.
回答by Langusten Gustel
I guess you are doing it in an .js file. I guess your webserver ist not interpreting php code. Instead it is serving it as static javascript code.
我猜你是在一个 .js 文件中做的。我猜你的网络服务器不会解释 php 代码。相反,它将它作为静态 javascript 代码提供。
Do it in a <script></script>
in an .php file.
<script></script>
在 .php 文件中执行此操作。
If you dont want to pass variable by variable to javascript:
Pass it as json
.
如果您不想将变量逐个传递给 javascript:将其作为json
.
An example can be found here:
一个例子可以在这里找到:
回答by Vaibhav
Make the array of colors and pass your $row inside a foreach loop just like this.
制作颜色数组并将您的 $row 传递到 foreach 循环中,就像这样。
foreach($row as $key=>$value)
{
$data[$key]['value']=(int)$value['id'];
$data[$key]['color'] = $color[$key];
}
And after that convert data into json like this
然后像这样将数据转换为json
var Data = <?php echo json_encode($dataArr['data']); ?>;
回答by enrigama
This was helpfull!
这很有帮助!
I just changed my HTML doc to PHP, and then IT WORKS!
我只是将我的 HTML 文档更改为 PHP,然后就可以了!
I am a rookie in code-develeper stuff, but my tip por MySQL cx is this.
我是代码开发人员的菜鸟,但我的 MySQL cx 提示是这样的。
Query:
询问:
<?php
session_start();
include_once "con.php"; #my conex in another doc
$query= "SELECT * FROM data ORDER BY id";
$result= mysql_query($query);
#For a simple first look, or rookies like me... I show the query result w/ an echo.
while ($row= mysql_fetch_assoc($result)){
echo $row['id'] . ' | ' . $row['mes'] . ' | ' . $row['valor1'] . ' % | ' . $row['valor2'] . ' % | ' . $row['valor3']. ' %' ."\n";
#This is what we need: I put data from my query in $setx, and then I use user3271851's code... it works!
$set1= $row['valor1'];
$set2= $row['valor2'];
$set3= $row['valor3'];
}
?>
For the Chart, just like the post:
对于图表,就像帖子一样:
var pieData = [
{
value: <?php echo $set1; ?>,
color:"#F38630"
},
{
value : <?php echo $set2; ?>,
color : "#E0E4CC"
},
{
value : <?php echo $set3; ?>,
color : "#69D2E7"
}
];
var myPie = new Chart(document.getElementById("canvas").getContext("2d")).Pie(pieData);
P.S.: Sorry, my english is not good at all. :)
PS:对不起,我的英语一点都不好。:)