-
基于canvasJS在PHP中制作动态图表详解
CanvasJS是一个JavaScript库,用于轻松为网页创建其他类型的图表。例如条形图,饼图,柱形图,面积图,折线图等。
让我们以需要创建一个图表的示例为例,在该图表中我们可以显示每月销售和购买的产品。我们将考虑两个数组,我们也可以从数据库中考虑它们。一旦我们从数据库中获取数据并将其存储在数组中,它就可以使用canvasJS轻松绘制动态图形。
创建一个文件并将其保存在项目文件夹中。文件名chart_sample.php包含数组形式的数据,其中第一个数组代表购买的产品,第二个数组代表sols产品列表。现在,使用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>
- <p id="chartContainer" style="height: 300px; width: 100%;"></p>
- </body>
- </html>
原文链接:http://www.phpfensi.com/php/20220720/21405.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式