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="840px" height="240px" style="border:1px dashed black;">
            出现文字表示你的浏览器不支持HTML5
        </canvas>
     </body>
</html>
<script type="text/javascript">
<!--
    function draw(){
        var canvas=document.getElementById("myCanvus");
        var canvasWidth=840;
        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);
        drawAxisY(context);
        var actualStock=100;
        var inbounds=[50,0,50,0,50,0,50,0,50,0,90,0,90,0,90,0,90,0,];
        var outbounds=[0,70,0,70,0,70,0,70,0,70,0,70,0,70,0,70,0,70,];
        drawStockCurve(context,actualStock,inbounds,outbounds);
        drawBounds(context);
        context.restore();
        context.fillText("每日库存变化折线",400,50);
        context.fillText("库存",10,20);
        context.fillText("日期",800,235);
    }
    function drawBounds(ctx){
        ctx.save();
        ctx.lineWidth=0.5;
        ctx.strokeStyle='red';
        // 画underage
        ctx.beginPath();
        ctx.moveTo(0, 25);
        ctx.lineTo(800, 25);
        ctx.stroke();
        ctx.closePath();
        ctx.save();
        ctx.translate(-10,25);
        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, 125);
        ctx.lineTo(800, 125);
        ctx.stroke();
        ctx.closePath();
        ctx.save();
        ctx.translate(-10,125);
        ctx.rotate(getRad(180));
        ctx.scale(-1,1);
        ctx.fillText("超储",0,0);
        ctx.restore();
        ctx.restore();
    }
    function drawStockCurve(ctx,actualStock,inbounds,outbounds){
        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();
        ctx.restore();
    }
    function drawAxisX(ctx){
        ctx.save();
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';
        // 画轴
        ctx.beginPath();
        ctx.moveTo(0, 0);
        ctx.lineTo(800, 0);
        ctx.stroke();
        ctx.closePath();
        ctx.beginPath();
        ctx.moveTo(800-Math.cos(getRad(15))*10, Math.sin(getRad(15))*10);
        ctx.lineTo(800, 0);
        ctx.lineTo(800-Math.cos(getRad(15))*10, -Math.sin(getRad(15))*10);
        ctx.stroke();
        ctx.closePath();
        // 画刻度
        var x,y;
        y=5;
        for(x=50;x<800;x+=50){
            ctx.beginPath();
            ctx.moveTo(x, 0);
            ctx.lineTo(x, y);
            ctx.stroke();
            ctx.closePath();
        }
        // 写文字
        var i=0;
        for(x=0;x<800;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 ... 
- 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,这个应该就不需要多作介绍了,只要是开发人员应该都不会陌生.html5是「新兴」的网页技术标准,目前,除IE8及其以下版本的IE浏览器之外,几乎所有主流浏览器(FireFox.Chrome. ... 
- 怎样用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" ... 
- 使用 HTML5 Canvas 绘制出惊艳的水滴效果
		HTML5 在不久前正式成为推荐标准,标志着全新的 Web 时代已经来临.在众多 HTML5 特性中,Canvas 元素用于在网页上绘制图形,该元素标签强大之处在于可以直接在 HTML 上进行图形操作 ... 
随机推荐
- 《Java编程思想》笔记 第二章 一切都是对象
			1.对象存储位置 对象的引用存在栈中,对象存在堆中.new 出来的对象都在堆中存储.栈的存取速度较快. 所有局部变量都放在栈内存里,不管是基本类型变量还是引用类型变量,都存储在各自的方法栈中: 但是引 ... 
- hadoop3.1伪分布式部署
			1.环境准备 系统版本:CentOS7.5 主机名:node01 hadoop3.1 的下载地址: http://mirror.bit.edu.cn/apache/hadoop/common/hado ... 
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext读取配置文件的方法
			先说:ClassPathXmlApplicationContext 这个类,默认获取的是WEB-INF/classes/下的路径,也就是在myeclipse的src下的路径,所以用这个是获取不到WEB ... 
- 看不到Harbor我也睡不着觉啊
			上午打球,下午陪小孩子看上海科技展,晚上搞定harbor. 完美!!!:) 参考文档: https://www.dwhd.org/20161023_110618.html http://blog.cs ... 
- [bzoj3218]a + b Problem  网络流+主席树优化建图
			3218: a + b Problem Time Limit: 20 Sec Memory Limit: 40 MBSubmit: 2229 Solved: 836[Submit][Status] ... 
- .NET Core Runtime ARM32 builds now available
			原文地址:传送门 .NET Core Runtime ARM32 builds now available The .NET Core team is now producing ARM32 buil ... 
- 只用120行Java代码写一个自己的区块链-3挖矿算法
			在本系列前两篇文章中,我们向大家展示了如何通过精炼的Java代码实现一个简单的区块链.包括生成块,验证块数据,广播通信等等,这一篇让我们聚焦在如何实现 PoW算法. 大家都无不惊呼比特币.以太坊及其他 ... 
- 洛谷——P2556 [AHOI2002]黑白图像压缩
			P2556 [AHOI2002]黑白图像压缩 题目描述 选修基础生物基因学的时候, 小可可在家里做了一次图像学试验. 她知道:整个图像其实就是若干个图像点(称作像素)的序列,假定序列中像素的个数总是 ... 
- POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)
			The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27141 Accepted: 9712 D ... 
- 【分块答案】【最小生成树】【kruscal】bzoj1196 [HNOI2006]公路修建问题
			二分(分块)枚举 边权上限.用kruscal判可行性. #include<cstdio> #include<algorithm> #include<cstring> ... 
