加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

基于canvasJS在PHP中制作动态图表分析

发布时间:2022-07-21 13:37:26 所属栏目:PHP教程 来源:互联网
导读:CanvasJS是一个JavaScript库,用于轻松为网页创建其他类型的图表。例如条形图,饼图,柱形图,面积图,折线图等。 让我们以需要创建一个图表的示例为例,在该图表中我们可以显示每月销售和购买的产品。我们将考虑两个数组,我们也可以从数据库中考虑它们。一
  CanvasJS是一个JavaScript库,用于轻松为网页创建其他类型的图表。例如条形图,饼图,柱形图,面积图,折线图等。
 
  让我们以需要创建一个图表的示例为例,在该图表中我们可以显示每月销售和购买的产品。我们将考虑两个数组,我们也可以从数据库中考虑它们。一旦我们从数据库中获取数据并将其存储在数组中,它就可以使用canvasJS轻松绘制动态图形。
 
  // 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"=> "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])
 
  <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>
 
    <p id="chartContainer" style="height: 300px; width: 100%;"></p>
 
  </body>
 
  </html>

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读