HTML5 Canvas 绘制库存变化折线 画入库出库柱状图

代码:
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>显示入库出库柱状图</title>
</head>
<body onload="draw()">
<canvas id="myCanvus" width="1240px" height="240px" style="border:1px dashed black;">
出现文字表示你的浏览器不支持HTML5
</canvas>
</body>
</html>
<script type="text/javascript">
<!--
function draw(){
var canvas=document.getElementById("myCanvus");
var canvasWidth=1240;
var canvasHeight=240;
var context=canvas.getContext("2d");
context.fillStyle = "white";
context.fillRect(0, 0, canvasWidth, canvasHeight);
context.strokeStyle = "black";
context.fillStyle = "black";
context.save();
// 进行坐标变换:把原点放在左下角,东方为X轴正向,北方为Y轴正向
var offset=20;// 偏移值,用来画坐标轴
context.save();
context.translate(0+offset,canvasHeight-offset);
context.rotate(getRad(180));
context.scale(-1,1);
drawAxisX(context,canvasWidth-40);
drawAxisY(context);
// 出库数据,这是主动数据
var outbounds=[10,70,0,70,0,60,0,60,0,70,0,70,0,70,0,70,0,70,0,70,0,70,0,70,];
var daysales=0;// 日销售量
var sum=0;
// 日销售量=出库数据均值
for(var i=0;i<outbounds.length;i++){
sum+=outbounds[i];
}
daysales=sum/outbounds.length;
console.log("日销售量="+daysales);
// 零件对象,固有属性数据
var piece=new Object();
piece.actualStock=100;// 当前实际库存量,单位个
piece.leadtime=1;// 到货天数,单位天
piece.safeday=0.5;// 安全系数,单位天
piece.supplyGap=2;//供应间隔日数,单位天
piece.reorganizeDay=2;//整理准备日数,单位天
// 最低库存量
var minStorage=daysales*(piece.leadtime+piece.safeday);
console.log("最低库存量="+minStorage);
// 最高库存量
var maxStorage=daysales*(piece.supplyGap+piece.reorganizeDay+piece.safeday);
console.log("最高库存量="+maxStorage);
// 入库数据,这是被动数据
var inbounds=[50,0,50,0,50,0,50,20,50,0,90,0,90,0,90,0,90,0,40,0,60,0,70,0,];
drawStockCurve(context,piece.actualStock,inbounds,outbounds,minStorage,maxStorage);
drawBounds(context,minStorage,maxStorage,canvasWidth-40);
context.restore();
// 计算库存周转率
var outTotal=0;// 出库总金额
// 出库总金额=出库量累计
for(var i=0;i<outbounds.length;i++){
outTotal+=outbounds[i];
}
console.log("出库总金额="+outTotal);
// 总库存金额
var inStock=piece.actualStock;
sum=0;
for(var i=0;i<inbounds.length;i++){
inStock=inStock+inbounds[i]-outbounds[i];
sum+=inStock;
}
console.log("inStock="+inStock);
var inAvg=sum/inbounds.length;// 平均库存量/平均库存金额
console.log("平均库存金额="+inAvg);
var storageRate=outTotal/inAvg*100;
console.log("库存周转率="+storageRate);
context.fillText("库存周转率="+toCurrency(""+storageRate+"")+"%",1000,40);
context.fillText("库存",10,20);
context.fillText("日期",1200,235);
context.font="bold 18px 楷体";
context.fillStyle="blue";
context.fillText("每日库存变化折线,红点意味着低于安全库存,黄点意味着超储,紫红色条表示入库,绿色条表示出库",100,30);
}
function drawBounds(ctx,minStorage,maxStorage,axisLength){
ctx.save();
ctx.lineWidth=0.5;
ctx.strokeStyle='red';
// 画underage
ctx.beginPath();
ctx.moveTo(0, minStorage);
ctx.lineTo(axisLength, minStorage);
ctx.stroke();
ctx.closePath();
ctx.save();
ctx.translate(-10,minStorage);
ctx.rotate(getRad(180));
ctx.scale(-1,1);
ctx.fillText("告罄",0,0);
ctx.restore();
ctx.restore();
ctx.save();
ctx.lineWidth=0.5;
ctx.strokeStyle='red';
// 画underage
ctx.beginPath();
ctx.moveTo(0, maxStorage);
ctx.lineTo(axisLength, maxStorage);
ctx.stroke();
ctx.closePath();
ctx.save();
ctx.translate(-10,maxStorage);
ctx.rotate(getRad(180));
ctx.scale(-1,1);
ctx.fillText("超储",0,0);
ctx.restore();
ctx.restore();
}
function drawStockCurve(ctx,actualStock,inbounds,outbounds,minStorage,maxStorage){
ctx.save();
ctx.lineWidth=1;
ctx.strokeStyle='black';
ctx.fillStyle='black';
// 显示折线图
var y=actualStock;
var x;
ctx.beginPath();
for(var i=0;i<inbounds.length;i++){
y=y+inbounds[i]-outbounds[i];
x=i*50;
ctx.lineTo(x, y);
ctx.save();
// 因坐标变换会导致文字错位,故采用位移+旋转+缩放的方式恢复
ctx.translate(x,y);
ctx.rotate(getRad(180));
ctx.scale(-1,1);
ctx.fillText("("+i+","+y+")",0,0);
ctx.restore();
}
ctx.stroke();
ctx.closePath();
// 显示当日出库入库柱状图
y=actualStock;
x=0;
var barWidth=5;
for(var i=0;i<inbounds.length;i++){
y=inbounds[i];
x=i*50;
ctx.fillStyle = "fuchsia";
ctx.fillRect(x-barWidth, 0, barWidth, y);
y=outbounds[i];
x=i*50;
ctx.fillStyle = "green";
ctx.fillRect(x, 0, barWidth, y);
}
// 显示超储、告罄标志
y=actualStock;
x=0;
for(var i=0;i<inbounds.length;i++){
y=y+inbounds[i]-outbounds[i];
x=i*50;
ctx.lineTo(x, y);
if(y>maxStorage){
ctx.beginPath();
ctx.strokeStyle='yellow';
ctx.arc(x,y,5,0,Math.PI*2,false);
ctx.stroke();
ctx.closePath();
}
if(y<minStorage){
ctx.beginPath();
ctx.strokeStyle='red';
ctx.arc(x,y,3,0,Math.PI*2,false);
ctx.stroke();
ctx.closePath();
}
}
ctx.restore();
}
function drawAxisX(ctx,axisLength){
ctx.save();
ctx.lineWidth=0.5;
ctx.strokeStyle='navy';
ctx.fillStyle='navy';
// 画轴
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(axisLength, 0);
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.moveTo(axisLength-Math.cos(getRad(15))*10, Math.sin(getRad(15))*10);
ctx.lineTo(axisLength, 0);
ctx.lineTo(axisLength-Math.cos(getRad(15))*10, -Math.sin(getRad(15))*10);
ctx.stroke();
ctx.closePath();
// 画刻度
var x,y;
y=5;
for(x=50;x<axisLength;x+=50){
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, y);
ctx.stroke();
ctx.closePath();
}
// 写文字
var i=0;
for(x=0;x<axisLength;x+=50){
ctx.save();
ctx.scale(1,-1);
ctx.fillText(i,x,y+10);
ctx.restore();
i++;
}
ctx.restore();
}
function drawAxisY(ctx){
ctx.save();
ctx.lineWidth=0.5;
ctx.strokeStyle='navy';
ctx.fillStyle='navy';
// 画轴
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(0, 200);
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.moveTo(Math.sin(getRad(15))*10, 200-Math.cos(getRad(15))*10);
ctx.lineTo(0, 200);
ctx.lineTo(-Math.sin(getRad(15))*10, 200-Math.cos(getRad(15))*10);
ctx.stroke();
ctx.closePath();
// 画刻度
var x,y;
x=5;
for(y=50;y<200;y+=50){
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(0, y);
ctx.stroke();
ctx.closePath();
}
// 写文字
x=-19;
for(y=50;y<200;y+=50){
ctx.save();
ctx.scale(1,-1);
ctx.translate(0,-200);
ctx.fillText(200-y,x,y);
ctx.restore();
}
ctx.restore();
}
function getRad(degree){
return degree/180*Math.PI;
}
function toCurrency(money) {
if (/[^0-9\.]/.test(money)){
return '0.00';
}
money = money.replace(/^(\d*)$/, "$1.");
money = (money + "00").replace(/(\d*\.\d\d)\d*/, "$1");
money = money.replace(".", ",");
var re = /(\d)(\d{3},)/;
while (re.test(money)) {
money = money.replace(re, "$1,$2");
}
money = money.replace(/,(\d\d)$/, ".$1");
return '' + money.replace(/^\./, "0.")+" ";
}
//-->
</script>
HTML5 Canvas 绘制库存变化折线 画入库出库柱状图的更多相关文章
- HTML5 Canvas 绘制库存变化折线 计算出库存周转率
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...
- HTML5 Canvas 绘制库存变化折线 计算出最高最低库存
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...
- HTML5 Canvas 绘制库存变化折线 增加超储告罄线
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...
- HTML5 Canvas 绘制库存变化折线
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...
- 使用html5 Canvas绘制线条(直线、折线等)
使用html5 Canvas绘制直线所需的CanvasRenderingContext2D对象的主要属性和方法(有"()"者为方法)如下: 属性或方法 基本描述 strokeSty ...
- 使用html5 canvas绘制圆形或弧线
注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...
- html5 canvas绘制环形进度条,环形渐变色仪表图
html5 canvas绘制环形进度条,环形渐变色仪表图 在绘制圆环前,我们需要知道canvas arc() 方 ...
- 怎样用JavaScript和HTML5 Canvas绘制图表
原文:https://code.tutsplus.com/zh-...原作:John Negoita翻译:Stypstive 在这篇教程中,我将展示用JavaScript和canvas作为手段,在饼状 ...
- 学习笔记:HTML5 Canvas绘制简单图形
HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...
随机推荐
- python--celery
有些时候我们的一些任务比较耗时,比如我们写了一个网站,用户注册的时候需要发送邮件.但是发送邮件的过程比较耗时,用户必须要等到我们将邮件发送成功之后才会得到响应.那么有没有一种办法,当用户点击发送邮件的 ...
- [ Python - 13 ] 批量管理主机必备模块
批量管理程序必备模块 optparse configparser paramiko optparse模块 简介: optparse模块主要用来为脚本传递命令参数功能 使用步骤: 1. i ...
- MATLAB的简单动画制作
这里介绍两种类型的动画实现,一种使用getframe和movie命令实现帧动画,另一种使用comet(comet3)命令实现画图过程的动画. ①getframe和movie命令实现帧动画 例如,创建一 ...
- Laravel查询技巧
1.同时增加几个字段的数量 DB::table('project') ->where('id',$yewuid) ->increment('count', 1, [ 'click'=> ...
- sourceforge的FTP镜像
https://www.mirrorservice.org/sites/ftp.sourceforge.net/
- 数学结论【p1463】[POI2002][HAOI2007]反素数
Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数 ...
- 洛谷——P1754 球迷购票问题
题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的钱币,另有N个人手持面值1 ...
- Windows 环境下 Redis 安装
1.redis官方下载地址:https://redis.io/download,redis 64位下载地址:https://github.com/MicrosoftArchive/redis/rele ...
- [SPOJ]COT2
题意:给一棵带点权的树,多次询问两点间路径上的不同权值数 学习了一下莫队上树(雾 先求出栈入栈序$p_{1\cdots 2n}$,记$st_x$为$x$在$p$中第一次出现的位置,$ed_x$为$x$ ...
- Djanog|requirements.txt生成
Django | requirement.txt 生成 pip django 1 pip 通常我们熟悉使用的都是 pip, 这个工具确实方便项目管理依赖包.当想把当前项目依赖的包的名称和版本导入指 ...