Chart.js没有高度响应

时间:2017-08-03 13:15:46

标签: javascript charts chart.js

我有一些问题要让chart.js折线图对高度以及宽度做出响应。

查看示例应该如何工作: http://www.chartjs.org/samples/latest/charts/line/basic.html

这是我的代码:

  var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
  var lineChartData = {
    labels : ['January','February','March','April','May','June','July'],
    datasets : [
      {
        label: 'My First dataset',
        labelColor : '#fff',
        fontColor : '#fff' ,
        backgroundColor : 'rgba(220,220,220,0.2)',
        borderColor : 'rgba(220,220,220,1)',
        pointBackgroundColor : 'rgba(220,220,220,1)',
        pointBorderColor : '#fff',
        data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
      }
    ]
  }

  var options = {
    maintainAspectRatio: false,
    legend: {
      display: false,
    },
    scales: {
      xAxes: [{
        gridLines: {
         display: false,
         color: '#03A5C5',
         lineWidth: 8,
         },
        ticks: {
          fontColor: "white",
        },
      }],
      yAxes: [{
        gridLines: {
         display: false,
         color: '#03A5C5',
         lineWidth: 8,
         },
        ticks: {
          fontColor: "white",
          beginAtZero: true,
        }
      }]
    }
  };
  var ctx = document.getElementById('canvas-1');
  var chart = new Chart(ctx, {
    responsive: true,
    type: 'line',
    data: lineChartData,
    options: options
  });

3 个答案:

答案 0 :(得分:3)

从Chart.js的Docs开始,建议将canvas包装到容器div中并更改容器的宽度/高度。但基本上它是按给定的宽度或高度改变的。

lannymcnie找到了一个更灵活的自定义解决方案,可用于任何画布响应:



var stage = new createjs.Stage("canvas");

var c = new createjs.Shape();
c.graphics.f("#f00").dc(0,0,50); // Drawn a 100x100 circle from the center

var t = new createjs.Text("Resize the browser/frame to redraw", "24px Arial bold", "#000");
t.x = t.y = 20;
stage.addChild(c, t);

window.addEventListener("resize", handleResize);
function handleResize() {
    var w = window.innerWidth-2; // -2 accounts for the border
    var h = window.innerHeight-2;
    stage.canvas.width = w;
    stage.canvas.height = h;
    //
    var ratio = 100/100; // 100 is the width and height of the circle content.
    var windowRatio = w/h;
    var scale = w/100;
    if (windowRatio > ratio) {
        scale = h/100;
    }
    // Scale up to fit width or height
    c.scaleX= c.scaleY = scale; 
    
    // Center the shape
    c.x = w / 2;
    c.y = h / 2;
        
    stage.update();
}
       
handleResize(); // First draw

html, body {
    padding: 0; margin: 0;
    overflow:hidden;
}
canvas {
    border: 1px solid #f00;
}

<script src="https://code.createjs.com/easeljs-0.8.2.min.js"></script>
<canvas id="canvas" width="800" height="600"></canvas>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

       1. Create Class

        .chart-container {
        position: relative;
        margin: auto;
        height: 80vh;
        width: 80vw;
        }

        2. Create Div With class chart-container and place canvas tag inside it.

        <div class="chart-container">
        <canvas id="canvas"></canvas>
        </div>

        3. Chart options :- Use property maintainAspectRatio: false,

        options: {
        maintainAspectRatio: false,
        responsive: true, 
...... 

        }

答案 2 :(得分:0)

您可以让它响应并设置不同的纵横比。我的问题是图表往往会在移动设备上被挤压,几乎没有高度。

为了补救,我在 chart.js 3.2.1 中使用了 AspectRatio 选项:

options: {          
   aspectRatio: 1, 
}

这样图表是正方形并保持正方形,您可以使用这个通常介于 0.5 到 2 之间的数字来找到适合您在两个显示器上的比率。