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=[0,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,0,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();
    context.fillText("每日库存变化折线,红点意味着低于安全库存,黄点意味着超储",400,50);
    context.fillText("库存",10,20);
    context.fillText("日期",1200,235);
}
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();
    // 2
    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;
}
//-->
</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 ... 
- 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 在不久前正式成为推荐标准,标志着全新的 Web 时代已经来临.在众多 HTML5 特性中,Canvas 元素用于在网页上绘制图形,该元素标签强大之处在于可以直接在 HTML 上进行图形操作 ... 
- 使用html5 Canvas绘制线条(直线、折线等)
		使用html5 Canvas绘制直线所需的CanvasRenderingContext2D对象的主要属性和方法(有"()"者为方法)如下: 属性或方法 基本描述 strokeSty ... 
- 使用html5 canvas绘制圆形或弧线
		注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ... 
- 学习笔记:HTML5 Canvas绘制简单图形
		HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ... 
- 使用html5 canvas绘制图片
		注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ... 
随机推荐
- unbuntu下mount windows共享目录
			1)sudo apt-get install smbclient 2)sudo mount -t cifs -o username=wcf@fitme.ai,password=Wsy123456 // ... 
- 在表达式和脚本中将bean实例暴露出来
			默认情况下,在activiti.cfg.xml中的所有bean和Spring配置文件中的所有bean都可以用于表达式和脚本.如果要限制配置文件中的bean的可见性,可以在流程引擎配置文件中配置一个名为 ... 
- hdu 3666(差分约束,手动栈解决超时问题)
			THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ... 
- php使用gd库将文字转换成图片(转)
			GD库是干什么用的呢!它是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片. <?php header("Content-type: ... 
- [BZOJ2653]middle 主席树+二分
			2653: middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2042 Solved: 1123[Submit][Status][Disc ... 
- #!bin/sh是啥
			第一句的#!是对脚本的解释器程序路径,脚本的内容是由解释器解释的,我们可以用各种各样的解释器来写对应的脚本,比如说/bin/csh脚本,/bin/perl脚本,/bin/awk脚本,/bin/sed脚 ... 
- js跳转整理(简记)
			location.replace(URL)跳转脱离历史记录流: location.href=url;在历史记录中 子刷新父级 parent.location.replace(parent.locati ... 
- Linux环境安装python3
			linux 安装Python3 1.python下载 请在终端输入如下命令: cd /home wget http://cdn.npm.taobao.org/dist/python/3.6.5/Pyt ... 
- Python3 数字
			layout: post title: Python3 数字 author: "luowentaoaa" catalog: true tags: mathjax: true Pyt ... 
- [BZOJ4316]小C的独立集(圆方树DP)
			题意:求仙人掌图直径. 算法:建出仙人掌圆方树,对于圆点直接做普通的树上DP(忽略方点儿子),方点做环上DP并将值直接赋给父亲. 建图时有一个很好的性质,就是一个方点在邻接表里的点的顺序正好就是从环的 ... 
