微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将缩放重置为chart.js中的初始值?

我是chartjs的新手.我应用了此库的扩展来启用缩放.起初它看起来很棒,但是当我缩放时,我无法返回到我的条形图开始时的原始点.

有谁知道为什么会发生这种情况或我该如何解决?非常感谢你.

var ctx = document.getElementById("canvas").getContext('2d');
var myChart = new Chart(ctx,{
   type: 'bar',data: {
      labels: ["08 Aug 2018 02:42 pm","09 Aug 2018 11:24 am","04 Sep 2018 01:23 pm","04 Sep 2018 01:26 pm","04 Sep 2018 01:31 pm","04 Sep 2018 01:33 pm","04 Sep 2018 01:35 pm","04 Sep 2018 01:36 pm","04 Sep 2018 01:38 pm","04 Sep 2018 01:40 pm","04 Sep 2018 01:45 pm","04 Sep 2018 01:46 pm","04 Sep 2018 01:49 pm","04 Sep 2018 01:53 pm","04 Sep 2018 02:07 pm","04 Sep 2018 02:19 pm","04 Sep 2018 02:20 pm","04 Sep 2018 02:30 pm","04 Sep 2018 02:31 pm","04 Sep 2018 02:33 pm","05 Sep 2018 09:30 am","05 Sep 2018 09:31 am","08 Aug 2018 02:42 pm","05 Sep 2018 09:31 am"],datasets: [{"label":"Estatura (cm)","backgroundColor":"rgba(0,100,150,0.5)","pointBorderColor":"rgba(26,179,148,1)","data":["68.00","69.00","70.00","71.00","72.00","73.00","74.00","75.00","76.00","77.00","78.00","79.00","80.00","81.00","82.00","83.00","84.00","85.00","86.00","87.00","88.00","89.00","90.00","91.00","68.00","91.00"]}]

   },options: {
      legend: {
        position: 'top',},scales: {
        xAxes: [{
          ticks: {
            autoSkip: true,maxRotation: 0,minRotation: 0
          }
        }]
      },pan: {
        enabled: false,mode: 'xy' // is panning about the y axis neccessary for bar charts?
      },zoom: {
        sensitivity:0.5,drag: false,enabled: true,mode: 'x'
      }
    }
});

这是我的代码工作:

https://jsfiddle.net/ogq5uex6/2/

解决方法

@yavg请查看下面的截图.许多标签都是重复的.不要像这样添加重复的标签.尝试添加24小时的时间作为我的答案

我想你是这么想的.

var ctx = document.getElementById("canvas").getContext('2d');
    var myChart = new Chart(ctx,{
       type: 'bar',data: {
          labels:   
          ["08 Aug 2018 14:42","09 Aug 2018 11:24","04 Sep 2018 13:23","04 Sep 2018 13:26","04 Sep 2018 13:31","04 Sep 2018 13:33","04 Sep 2018 13:35","04 Sep 2018 13:36","04 Sep 2018 13:38","04 Sep 2018 13:39","04 Sep 2018 13:40","04 Sep 2018 13:45","04 Sep 2018 13:46","04 Sep 2018 13:49","04 Sep 2018 13:50","04 Sep 2018 13:51","04 Sep 2018 13:53","04 Sep 2018 14:07","04 Sep 2018 14:19","04 Sep 2018 14:20","04 Sep 2018 14:21","04 Sep 2018 14:30","04 Sep 2018 14:31","04 Sep 2018 14:33","05 Sep 2018 09:30","05 Sep 2018 09:31","08 Aug 2018 14:43","09 Aug 2018 11:25","04 Sep 2018 13:22","04 Sep 2018 13:27","04 Sep 2018 13:32","04 Sep 2018 13:34","04 Sep 2018 13:37","04 Sep 2018 13:41","04 Sep 2018 01:40","04 Sep 2018 01:41","04 Sep 2018 01:42","04 Sep 2018 01:45","04 Sep 2018 01:46","04 Sep 2018 01:49","04 Sep 2018 01:50","04 Sep 2018 01:51","04 Sep 2018 01:53","04 Sep 2018 02:07","04 Sep 2018 02:19","04 Sep 2018 02:20","04 Sep 2018 02:22","04 Sep 2018 02:36","04 Sep 2018 02:32","04 Sep 2018 02:35","05 Sep 2018 10:31","05 Sep 2018 10:32"],"91.00"]}]
       },options: {
          legend: {
            position: 'top',scales: {
            xAxes: [{
              ticks: {
                autoSkip: true,minRotation: 0
              }
            }]
          },pan: {
            enabled: false,mode: 'xy' // is panning about the y axis neccessary for bar charts?
          },zoom: {
            sensitivity:0.5,mode: 'x'
          }
        }
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>


<script src="https://npmcdn.com/Chart.Zoom.js@0.3.0/Chart.Zoom.min.js"></script>



<canvas id="canvas" height="180"></canvas>

My Codepen Example here

原文地址:https://www.jb51.cc/js/158109.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐