chart.js绘制移动端图表

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <meta name="renderer" content="webkit">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta http-equiv="pragma" content="no-cache" />
<p style="margin-top: 20px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', 微软雅黑, arial, 宋体, sans-serif; font-size: 16px; line-height: 28px; text-align: justify;"><meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1.0" /></p><p style="margin-top: 20px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', 微软雅黑, arial, 宋体, sans-serif; font-size: 16px; line-height: 28px; text-align: justify;">    <meta content="telephone=no,email=no" name="format-detection" /></p><p style="margin-top: 20px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', 微软雅黑, arial, 宋体, sans-serif; font-size: 16px; line-height: 28px; text-align: justify;">    <title>Chart Demo</title></p><p style="margin-top: 20px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', 微软雅黑, arial, 宋体, sans-serif; font-size: 16px; line-height: 28px; text-align: justify;">    <style type="text/css"></p>


    

    html,body,h1,h2,h3,h4,h5,h6 {

        margin: 0;

        padding: 0;

    }

    .container {

        max-width: 1020px;

        margin: 0px auto;

        margin-bottom: 80px;

    }

    .chart-wrapper {

        background: #fff;

        padding: 15px;

        max-width: 1020px;

        margin: 0px auto 0px auto;

        box-sizing: border-box;

        overflow: auto;

        /*在手机,支持图表区域的滚动  -webkit-overflow-scrolling: touch*/

        overflow-scrolling: touch;

        -webkit-overflow-scrolling: touch;

    }

    h2 {

        margin: 20px 0px;

    }

    .chart-wrapper canvas {

        min-width: 100%;

        height: 260px;

    }

    .chart-title,

    .chart-wrapper + small {

        margin-left: 15px;

    }

    </style>

    <script src="Chart.js?"></script>

    <!--[if lte IE 8]>

    <script src="ie/excanvas.js"></script>

    <script>

    Chart.defaults.global.animation = false;

    //这里主要是为<=IE8做降级处理,因为动画在IE8效果很差

    </script>

    <![endif]-->

</head>

<body>

    <div class="container">

        <h2 class="chart-title">某品牌汽车销量走势</h2>

        <div class="chart-wrapper">

            <canvas id="sales-volume-chart"></canvas>

        </div>

        <small>单位:万辆</small>

    </div>

    <div class="container">

        <h2 class="chart-title">某品牌汽车销量走势</h2>

        <div class="chart-wrapper">

            <canvas id="sales-volume-bar-chart"></canvas>

        </div>

        <small>单位:万辆</small>

    </div>

</body>

下面是核心的javascript代码:

<script>

    function lineChart() {

        var ctx = document.getElementById('sales-volume-chart').getContext("2d")

        var data = {

            labels: ["2014-10", "2014-11", "2014-12", "2015-1", "2015-2", "2015-3"],

            datasets: [{

                label: "",

                fillColor: "rgba(220,220,220,0.2)",

                strokeColor: "rgba(0,102,51,1)",

                pointColor: "rgba(220,220,220,1)",

                pointStrokeColor: "#339933",

                pointHighlightFill: "#339933",

                pointHighlightStroke: "rgba(220,220,220,1)",

                data: [1.27, 1.30, 1.30, 1.41, 1.04, 1.29]

            }]

        };

        // var salesVolumeChart = new Chart(ctx).Line(data);

        var salesVolumeChart = new Chart(ctx).Line(data, {

            // 小提示的圆角

            // tooltipCornerRadius: 0,

            // 折线的曲线过渡,0是直线,默认0.4是曲线

            bezierCurveTension: 0,

            // bezierCurveTension: 0.4,

            // 关闭曲线功能

            bezierCurve: false,

            // 背景表格显示

            // scaleShowGridLines : false,

            // 点击的小提示

            tooltipTemplate: "<%if (label){%><%=label%> 销量:<%}%><%= value %>万辆",

            //自定义背景小方格、y轴每个格子的单位、起始坐标

            scaleOverride: true,

            scaleSteps: 9.5,

            // scaleStepWidth: Math.ceil(Math.max.apply(null,data.datasets[0].data) / 0.1),

            scaleStepWidth: 0.05,

            scaleStartValue: 1

        });

    }

    function barChart() {

        var ctx = document.getElementById('sales-volume-bar-chart').getContext("2d")

        var data = {

            labels: ["2014-10", "2014-11", "2014-12", "2015-1", "2015-2", "2015-3"],

            datasets: [{

                label: "",

                fillColor: "rgba(153,204,153,0.5)",

                strokeColor: "rgba(0,102,51,1)",

                pointColor: "rgba(220,220,220,1)",

                pointStrokeColor: "#338033",

                pointHighlightFill: "#338033",

                pointHighlightStroke: "rgba(220,220,220,1)",

                data: [1.27, 1.30, 1.30, 1.41, 1.04, 1.29]

            }]

        };

        var salesVolumeChart = new Chart(ctx).Bar(data, {

            // 点击的小提示

            tooltipTemplate: "<%if (label){%><%=label%> 销量:<%}%><%= value %>万辆"

        });

    }

    // 启动

    setTimeout(function() {

        // 避免IE7-8 调用getContext报错,使用setTimeout

        lineChart()

        barChart()

    }, 0)

    // 在手机测试,canvas中的动画看起来很卡,性能很差

    // PC上还不错

    if (/Mobile/i.test(navigator.userAgent)) {

        //针对手机,性能做一些降级,看起来就不会那么卡了

        Chart.defaults.global.animationSteps = Chart.defaults.global.animationSteps / 6

        Chart.defaults.global.animationEasing = "linear"

    }

    </script>

附录:

其中用到的软件:

Chart.js框架,版本1.0.2,一个简单、轻量级的绘图框架,基于HTML5 canvas。这个框架能很多种图,折线图、柱状图、玫瑰图等。

excanvas.js,这是一个专门解决canvas IE兼容问题的框架。因为IE9已经支持canvas,所有<=IE8引入这个库就可以了。

这两个库在Github上都有,如图:

兼容性:

因为引入了excanvas.js ,所以,兼容到IE7+,

其他的chrome、android、iOS等浏览器,都是可以兼容的。  

页面加入了一些web app的meta,所以对手机的兼容也是不错的:

手机访问截图(手机型号:Smartisan T1 白色,Android):

图表在手机上可以用手指拖动。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值