<!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 绘制库存变化折线 增加超储告罄线的更多相关文章

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

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  2. HTML5 Canvas 绘制库存变化折线 计算出库存周转率

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  3. HTML5 Canvas 绘制库存变化折线 计算出最高最低库存

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  4. HTML5 Canvas 绘制库存变化折线

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  5. 使用html5 Canvas绘制线条(直线、折线等)

    使用html5 Canvas绘制直线所需的CanvasRenderingContext2D对象的主要属性和方法(有"()"者为方法)如下: 属性或方法 基本描述 strokeSty ...

  6. html5 Canvas绘制图形入门详解

    html5,这个应该就不需要多作介绍了,只要是开发人员应该都不会陌生.html5是「新兴」的网页技术标准,目前,除IE8及其以下版本的IE浏览器之外,几乎所有主流浏览器(FireFox.Chrome. ...

  7. 怎样用JavaScript和HTML5 Canvas绘制图表

    原文:https://code.tutsplus.com/zh-...原作:John Negoita翻译:Stypstive 在这篇教程中,我将展示用JavaScript和canvas作为手段,在饼状 ...

  8. 学习笔记:HTML5 Canvas绘制简单图形

    HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...

  9. 使用 HTML5 Canvas 绘制出惊艳的水滴效果

    HTML5 在不久前正式成为推荐标准,标志着全新的 Web 时代已经来临.在众多 HTML5 特性中,Canvas 元素用于在网页上绘制图形,该元素标签强大之处在于可以直接在 HTML 上进行图形操作 ...

随机推荐

  1. Tortoise svn 冲突解决主要办法

    Tortoise svn 冲突解决主要办法 1.先备份自己的修改文件后,然后revert自己的更新内容,然后提交,再以更新后的代码为基准,将备份的代码移入进来.在这种方式下不需要使用svn resol ...

  2. Mysql缺少可执行的命令

    MySQL问题解决:-bash:mysql:command not found 问题:       [root@linux115 /]# mysql -uroot -p        -bash: m ...

  3. MATLAB求解方程与方程组

    1.      solve函数 ①求解单个一元方程的数值解 syms x; x0 = double(solve(x +2 - exp(x),x)); 求x+2 = exp(x)的解,结果用double ...

  4. 虚拟机vmware下安装Ghost XP——正确的解决方案

    http://hi.baidu.com/xjl456852/item/fd466e9935b2da8859146111 在虚拟机中启动系统,出现"Operating System not f ...

  5. 【转】Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址

    Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址 关注finddreams,一起分享,一起进步: http://blog.csdn.net/finddr ...

  6. AppScan8.7的两个细节亮点

    1.增加了对红极一时的Struts2的远程代码执行漏洞的检测 2.增加了对篡改价格这类应用逻辑缺陷的检测

  7. 认识createDocumentFragment

    今天在看vue源码解析时候发现一个api没有见过,一查是原生的,赶紧补漏. DocumentFragments 是DOM节点.它们不是主DOM树的一部分.通常的用例是创建文档片段,将元素附加到文档片段 ...

  8. Floyd-弗洛伊德算法

    今天,研究一下谁都能看懂的弗洛伊德算法. 首先,弗洛伊德算法是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法. 这个算法需要一个用到一个二维数组啊a[][],而a[i][j]表示的就 ...

  9. [BZOJ 4720] 换教室

    Link: BZOJ 4720 传送门 Solution: 2016年$NOIP$考的一道语文题 题面虽长,但思路并不难想 对于这类期望问题,大多数时候都用期望$dp$来解决 根据询问:在$n$个时间 ...

  10. Scala实战高手****第16课:Scala implicits编程彻底实战及Spark源码鉴赏

    隐式转换:当某个类没有具体的方法时,可以在该类的伴生对象或上下文中查找是否存在隐式转换,将其转换为可以调用该方法的类,通过代码简单的描述下 一:隐式转换 1.定义类Man class Man(val ...