基于canvasJS在PHP中制作動(dòng)態(tài)圖表
CanvasJS是一個(gè)JavaScript庫(kù),用于輕松為網(wǎng)頁(yè)創(chuàng)建其他類(lèi)型的圖表。例如條形圖,餅圖,柱形圖,面積圖,折線(xiàn)圖等。
讓我們以需要?jiǎng)?chuàng)建一個(gè)圖表的示例為例,在該圖表中我們可以顯示每月銷(xiāo)售和購(gòu)買(mǎi)的產(chǎn)品。我們將考慮兩個(gè)數(shù)組,我們也可以從數(shù)據(jù)庫(kù)中考慮它們。一旦我們從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)并將其存儲(chǔ)在數(shù)組中,它就可以使用canvasJS輕松繪制動(dòng)態(tài)圖形。
創(chuàng)建一個(gè)文件并將其保存在項(xiàng)目文件夾中。文件名chart_sample.php包含數(shù)組形式的數(shù)據(jù),其中第一個(gè)數(shù)組代表購(gòu)買(mǎi)的產(chǎn)品,第二個(gè)數(shù)組代表sols產(chǎn)品列表。現(xiàn)在,使用canvasJS繪制圖形。
例如:
<?php // First array for purchased product $purchased= array(10, 15, 19, 0, 5, 7, 0, 0, 12, 13, 10, 1); // Second array for sold product $sold= array(7, 12, 14, 0, 3, 7, 0, 0, 10, 7, 5, 0); // Data to draw graph for purchased products $dataPoints = array( array("label"=> "Jan", "y"=> $purchased[0]), array("label"=> "Feb", "y"=> $purchased[1]), array("label"=> "March", "y"=> $purchased[2]), array("label"=> "April", "y"=> $purchased[3]), array("label"=> "May", "y"=> $purchased[4]), array("label"=> "Jun", "y"=> $purchased[5]), array("label"=> "July", "y"=> $purchased[6]), array("label"=> "Aug", "y"=> $purchased[7]), array("label"=> "Sep", "y"=> $purchased[8]), array("label"=> "Oct", "y"=> $purchased[9]), array("label"=> "Nov", "y"=> $purchased[10]), array("label"=> "Dec", "y"=> $purchased[11]) ); // Data to draw graph for sold products $dataPoints2 = array( array("label"=> "Jan", "y"=> $sold[0]), array("label"=> "Feb", "y"=> $sold[1]), array("label"=> "March", "y"=> $sold[2]), array("label"=> "April", "y"=> $sold[3]), array("label"=> "May", "y"=> $sold[4]), array("label"=> "Jun", "y"=> $sold[5]), array("label"=> "July", "y"=> $sold[6]), array("label"=> "Aug", "y"=> $sold[7]), array("label"=> "Sep", "y"=> $sold[8]), array("label"=> "Oct", "y"=> $sold[9]), array("label"=> "Nov", "y"=> $sold[10]), array("label"=> "Dec", "y"=> $sold[11]) ); ?>
<!DOCTYPE HTML> <html> <head> <script src="https://canvasjs.com/assets/script/canvasjs.min.js"> </script> <script> window.onload = function () { var chart = new CanvasJS.Chart("chartContainer", { animationEnabled: true, title:{ text: "Monthly Purchased and Sold Product" }, axisY: { title: "Purchased", titleFontColor: "#4F81BC", lineColor: "#4F81BC", labelFontColor: "#4F81BC", tickColor: "#4F81BC" }, axisY2: { title: "Sold", titleFontColor: "#C0504E", lineColor: "#C0504E", labelFontColor: "#C0504E", tickColor: "#C0504E" }, toolTip: { shared: true }, legend: { cursor:"pointer", itemclick: toggleDataSeries }, data: [{ type: "column", name: "Purchased", legendText: "Purchased", showInLegend: true, dataPoints:<?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?> }, { type: "column", name: "Sold", legendText: "Sold", axisYType: "secondary", showInLegend: true, dataPoints:<?php echo json_encode($dataPoints2, JSON_NUMERIC_CHECK); ?> }] }); chart.render(); function toggleDataSeries(e) { if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) { e.dataSeries.visible = false; } else { e.dataSeries.visible = true; } chart.render(); } } </script> </head> <body> <div id="chartContainer" style="height: 300px; width: 100%;"></div> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js判斷undefined類(lèi)型,undefined,null, 的區(qū)別詳細(xì)解析
本篇文章主要是對(duì)js判斷undefined類(lèi)型,undefined,null,NaN的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12JavaScript canvas實(shí)現(xiàn)九宮格切圖效果
這篇文章主要為大家詳細(xì)介紹了JavaScript canvas實(shí)現(xiàn)九宮格切圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09JS實(shí)現(xiàn)圖片橫向滾動(dòng)效果示例代碼
圖片橫向滾動(dòng)效果,大家可能都已經(jīng)很熟悉了,本文為大家介紹下使用js實(shí)現(xiàn)橫向滾動(dòng)效果,喜歡的朋友可以參考下,希望對(duì)大家有所幫助2013-09-09使用bootstrap插件實(shí)現(xiàn)模態(tài)框效果
今天我們選擇使用著名的 bootstrap 庫(kù)的模態(tài)框插件 modal.js 來(lái)實(shí)現(xiàn)模態(tài)框效果,同時(shí)也使大家進(jìn)一步熟悉 bootstrap 的插件使用,需要的朋友可以參考下2017-05-05js如何獲取訪(fǎng)問(wèn)IP、地區(qū)、當(dāng)前操作瀏覽器
這篇文章主要介紹了js如何獲取訪(fǎng)問(wèn)IP、地區(qū)、當(dāng)前操作瀏覽器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07javascript實(shí)現(xiàn)獲取一個(gè)日期段內(nèi)每天不同的價(jià)格(計(jì)算入住總價(jià)格)
這篇文章主要介紹了javascript實(shí)現(xiàn)獲取一個(gè)日期段內(nèi)每天不同的價(jià)格(計(jì)算入住總價(jià)格)的相關(guān)資料,需要的朋友可以參考下2018-02-02收藏Javascript中常用的55個(gè)經(jīng)典技巧
收藏Javascript中常用的55個(gè)經(jīng)典技巧...2007-08-08javascript HTML+CSS實(shí)現(xiàn)經(jīng)典橙色導(dǎo)航菜單
這篇文章主要介紹了javascript HTML+CSS實(shí)現(xiàn)經(jīng)典橙色導(dǎo)航菜單的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02js+cavans實(shí)現(xiàn)圖片滑塊驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了js+cavans實(shí)現(xiàn)圖片滑塊驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09