<!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. string与double的互相转换

    #include <iostream> #include <string> #include <sstream> string DoubleToString(dou ...

  2. Spring -- 注解事务 以及 7个传播行为

    注解事务: 1.开启注解事务配置: <!-- 事务管理器 --> <bean id="transactionManager" class="org.sp ...

  3. JSP 基础之 JSTL <c:choose>用法 if else

    <c:choose> <c:when test="${condition1}"> condition1为true </c:when> <c ...

  4. java 单元测试框架

    @Test:测试方法(A) (expected=XXEception.class)(B) (timeout=xxx)@Ignore: 被忽略的测试方法. //该方法 不会执行@Before: 每一个测 ...

  5. Servlet response原理

    首先web服务器 接受到http请求后转交给相应的servlet进行处理这个过程可以他通过配置web.xml来进行确定,然后web服务器将相应的信息封装到request和response对象,由相应的 ...

  6. Python 进阶 之 else块 巧(慎)用

    Python 的 else 模块和其他语言的else模块相比有那么一丢丢的特殊用法,有些用法虽然不推荐用,但是别人如果用了,最起码能看懂是不是? 1:快捷返回值: 格式: value1 if expr ...

  7. 【转】python argparse用法总结

    转自:https://www.jianshu.com/p/fef2d215b91d 1. argparse介绍 是python的一个命令行解析包,非常编写可读性非常好的程序 2. 基本用法 prog. ...

  8. xunsearch: 开启后台服务,索引……随笔记录

    重启后台服务: cd $prefix ; bin/xs-ctl.sh restart 索引: # 导入 MySQL 数据库的 dbname.tbl_post 表到 demo 项目中,并且平滑重建 ut ...

  9. centos 命令行修改主机名

    # vi /etc/sysconfig/network # 把localhost.localdomain 修改为 localhost.com # 保存退出 # vi /etc/hosts # 把loc ...

  10. api接口思路介绍

    现在很流行api了,但各种api做法不一样,下面我整理了一些自己的想法,也是看了各大门户网站开放的api应用想到的,与大家分享分享,高手跳过.   API(Application Programmin ...