chart.js在移动视图中调整高度

时间:2016-08-10 13:14:11

标签: javascript html css twitter-bootstrap chart.js

当我将网站切换到移动视图时,我需要获取chart.js来调整图表的高度,我正在使用bootstrap作为我的框架工作正常。

网站移动视图的问题在于图表太小而无法与之互动。

我已经尝试在var options = {}中添加属性但是没有运气我甚至尝试过在css中使用高度归属但没有运气!

请帮忙!

这是网站:

http://preview.qaxawkzy7e6d2t9vhswkar3v1n8w7b9p705un9k2ojkbj4i.box.codeanywhere.com/

代码:

<html>
  <head>
    <meta name="viewport" content="width=device-width, minimal-scale=1.0, maximum-scale=1.0,">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.bundle.js"></script>
  </head>
  <body style="padding: 20px;">
    <div class="row">
      <div class="col-md-6">
        <div class="well">
          <canvas id="myChart"></canvas>
        </div>   
      </div>
      <div class="col-md-6">
        <div class="well">
          <h1>Hi there!</h1>
        </div>   
      </div>
    </div>
    <script>
      var ctx = document.getElementById("myChart");

      var myChart = new Chart(ctx, {
          type: 'bar',
          options: {
              responsive: true,
          },
          data: {
              labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
              datasets: [{
                  label: '# of Votes',
                  data: [12, 19, 3, 5, 2, 3],
                  backgroundColor: [
                      'rgba(255, 99, 132, 0.2)',
                      'rgba(54, 162, 235, 0.2)',
                      'rgba(255, 206, 86, 0.2)',
                      'rgba(75, 192, 192, 0.2)',
                      'rgba(153, 102, 255, 0.2)',
                      'rgba(255, 159, 64, 0.2)'
                  ],
                  borderColor: [
                      'rgba(255,99,132,1)',
                      'rgba(54, 162, 235, 1)',
                      'rgba(255, 206, 86, 1)',
                      'rgba(75, 192, 192, 1)',
                      'rgba(153, 102, 255, 1)',
                      'rgba(255, 159, 64, 1)'
                  ],
                  borderWidth: 1
              }]
          },
          options: {
              scales: {
                  yAxes: [{
                      ticks: {
                          beginAtZero:true
                      }
                  }]
              }
          }
      });
    </script>
  </body>
</html>

3 个答案:

答案 0 :(得分:3)

我认为答案是为图表设置maintainAspectRatio: false选项,然后在屏幕宽度达到一定大小时响应地设置图表的minimum height

答案 1 :(得分:2)

如果您使用的是引导程序,则可以使用表响应类来维护响应视图,而无需使用keepAspectRatio选项

所以您的画布代码:

<div class="table-responsive">
    <canvas id="canvas" style="height:400px" class="table"></canvas>
</div>

和您的选项代码:

 options: {
            responsive: false,
            maintainAspectRatio: false,
            //...your other values

答案 2 :(得分:1)

另一种可以使用min-height的方法,它应该是画布的内联样式,在这种情况下,您可以保持响应为true

<div class="table-responsive">
  <canvas id="canvas"  style="min-height:250px"  class="table"></canvas>
</div>

并且您选择响应为真

  options: {
                responsive: true,// other properties ....
            }