基于canvasJS在PHP中制作动态图表详解

CanvasJS是一个JavaScript库,用于轻松为网页创建其他类型的图表。例如条形图,饼图,柱形图,面积图,折线图等。

让我们以需要创建一个图表的示例为例,在该图表中我们可以显示每月销售和购买的产品。我们将考虑两个数组,我们也可以从数据库中考虑它们。一旦我们从数据库中获取数据并将其存储在数组中,它就可以使用canvasJS轻松绘制动态图形。

创建一个文件并将其保存在项目文件夹中。文件名chart_sample.php包含数组形式的数据,其中第一个数组代表购买的产品,第二个数组代表sols产品列表。现在,使用canvasJS绘制图形。

例如:

  1. <?php
  2. // First array for purchased product
  3. $purchased= array(10, 15, 19, 0, 5, 7, 0, 0, 12, 13, 10, 1);
  4. // Second array for sold product
  5. $sold= array(7, 12, 14, 0, 3, 7, 0, 0, 10, 7, 5, 0);
  6. // Data to draw graph for purchased products
  7. $dataPoints = array(
  8. array("label"=> "Jan", "y"=> $purchased[0]),
  9. array("label"=> "Feb", "y"=> $purchased[1]),
  10. array("label"=> "March", "y"=> $purchased[2]),
  11. array("label"=> "April", "y"=> $purchased[3]),
  12. array("label"=> "May", "y"=> $purchased[4]),
  13. array("label"=> "Jun", "y"=> $purchased[5]),
  14. array("label"=> "July", "y"=> $purchased[6]),
  15. array("label"=> "Aug", "y"=> $purchased[7]),
  16. array("label"=> "Sep", "y"=> $purchased[8]),
  17. array("label"=> "Oct", "y"=> $purchased[9]),
  18. array("label"=> "Nov", "y"=> $purchased[10]),
  19. array("label"=> "Dec", "y"=> $purchased[11])
  20. );
  21. // Data to draw graph for sold products
  22. $dataPoints2 = array(
  23. array("label"=> "Jan", "y"=> $sold[0]),
  24. array("label"=> "Feb", "y"=> $sold[1]),
  25. array("label"=> "March", "y"=> $sold[2]),
  26. array("label"=> "April", "y"=> $sold[3]),
  27. array("label"=> "May", "y"=> $sold[4]),
  28. array("label"=> "Jun", "y"=> $sold[5]),
  29. array("label"=> "July", "y"=> $sold[6]),
  30. array("label"=> "Aug", "y"=> $sold[7]),
  31. array("label"=> "Sep", "y"=> $sold[8]),
  32. array("label"=> "Oct", "y"=> $sold[9]),
  33. array("label"=> "Nov", "y"=> $sold[10]),
  34. array("label"=> "Dec", "y"=> $sold[11])
  35. );
  36. ?>
  37. <!DOCTYPE HTML>
  38. <html>
  39. <head>
  40. <script src="https://canvasjs.com/assets/script/canvasjs.min.js">
  41. </script>
  42. <script>
  43. window.onload = function () {
  44. var chart = new CanvasJS.Chart("chartContainer", {
  45. animationEnabled: true,
  46. title:{
  47. text: "Monthly Purchased and Sold Product"
  48. },
  49. axisY: {
  50. title: "Purchased",
  51. titleFontColor: "#4F81BC",
  52. lineColor: "#4F81BC",
  53. labelFontColor: "#4F81BC",
  54. tickColor: "#4F81BC"
  55. },
  56. axisY2: {
  57. title: "Sold",
  58. titleFontColor: "#C0504E",
  59. lineColor: "#C0504E",
  60. labelFontColor: "#C0504E",
  61. tickColor: "#C0504E"
  62. },
  63. toolTip: {
  64. shared: true
  65. },
  66. legend: {
  67. cursor:"pointer",
  68. itemclick: toggleDataSeries
  69. },
  70. data: [{
  71. type: "column",
  72. name: "Purchased",
  73. legendText: "Purchased",
  74. showInLegend: true,
  75. dataPoints:<?php echo json_encode($dataPoints,
  76. JSON_NUMERIC_CHECK); ?>
  77. },
  78. {
  79. type: "column",
  80. name: "Sold",
  81. legendText: "Sold",
  82. axisYType: "secondary",
  83. showInLegend: true,
  84. dataPoints:<?php echo json_encode($dataPoints2,
  85. JSON_NUMERIC_CHECK); ?>
  86. }]
  87. });
  88. chart.render();
  89. function toggleDataSeries(e) {
  90. if (typeof(e.dataSeries.visible) === "undefined"
  91. || e.dataSeries.visible) {
  92. e.dataSeries.visible = false;
  93. }
  94. else {
  95. e.dataSeries.visible = true;
  96. }
  97. chart.render();
  98. }
  99. }
  100. </script>
  101. </head>
  102. <body>
  103. <p ></p>
  104. </body>
  105. </html>