【问题标题】:Chart.js not height responsiveChart.js 不响应高度
【发布时间】:2017-08-03 13:15:46
【问题描述】:

我无法让 chart.js 折线图同时响应高度和宽度。

查看示例它应该如何工作:

这是我的代码:

  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
  });

【问题讨论】:

  • 他们在docs 中说图表在响应方面存在问题。希望您能找到解决方案,因为我找不到
  • 在文档中,他们建议将画布标签包装到容器 div 中,并将其设置为自己的宽度和高度。基本上,这就是您提到的响应示例中的完成方式。对解决你的案子有帮助吗?
  • @blewherself 成功了!谢谢

标签: javascript charts chart.js


【解决方案1】:
  1. 创建类
.chart-container {
    position: relative;
    margin: auto;
    height: 80vh;
    width: 80vw;
}
  1. 使用类图表容器创建 Div 并在其中放置画布标签。
<div class="chart-container">
  <canvas id="canvas"></canvas>
</div>
  1. 图表选项,使用属性maintainAspectRatio设置为false:
options: {
    maintainAspectRatio: false,
    responsive: true,  
    ...
}

【讨论】:

  • 在选项下添加两个项目(maintainAspectRatio 和 responsive)为我解决了这个问题。
  • 登录只是为了喜欢答案。我一直试图弄清楚几个小时。干得好!
【解决方案2】:

Docs 开始,Chart.js 建议将画布包装到容器 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>

【讨论】:

    【解决方案3】:

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

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

    options: {          
       aspectRatio: 1, 
    }
    

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

    【讨论】:

      猜你喜欢
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多