如何在chart.js中使用JSON数据? [英] how to use JSON Data in chart.js?
问题描述
我一直在尝试使用MYSQL数据库中的数据,并使用它们通过chart.js创建图形图表.我将数据编码为JSON数据(通过php文件名data1.php),现在我需要使用Jquery或javascript将这些Json数据转换回数组. ?
hi i have been trying to use data from MYSQL database and use them to create graphical chart with chart.js. i encoded data into JSON data( through a php file name data1.php), now i need to convert these Json data back to array using Jquery or javascript.. i dont have much knowledge about AJAX.. so could u help me out??
data1.php生成JSON数据为
data1.php produces JSON data as
[{"company_name":"project A","present_worth":"81531.946062978"},{"company_name":"project B","present_worth":"67313.916593765"},{"company_name":"project c","present_worth":"92440.723376746"}]
我需要数组中的present_worth值
i need value of present_worth in an array
这是用于创建条形图的脚本..而不是自定义数据(例如,数据:[65,59,90,81,56,55,40]),在这里我想要一个带有JSON数据的数组. /p>
this is the script used to create bargraph.. instead of custom data (for eg data : [65,59,90,81,56,55,40])given here i want an array here with JSON data.
<script type="text/javascript">
function bar(){
var barChartData = {
labels :["January","February","March","April","May","June","July"],datasets : [
{
fillColor : "rgba(220,280,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
data : [65,59,90,81,56,55,40]
},
{
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
data : [28,48,40,19,96,27,100]
}
]
};
var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData);
}
</script>
推荐答案
我不太确定你是不是这个意思:
I am not quite sure if you mean this:
var chartjsData = [];
for (var i = 0; i < json.length; i++) {
chartjsData.push(json[i].present_worth);
}
否则评论;)
这篇关于如何在chart.js中使用JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!