[英]chart.js chart distorted with fixed width
I have a chart.js canvas inside a container which I gave a fixed min-width, so people could scroll the chart on mobile devices. 我在一个容器内有一个chart.js画布,该画布具有最小的固定宽度,因此人们可以在移动设备上滚动图表。
But as soon as the container has its fixed width, the chart is getting more distorted, the smaller the screen gets. 但是,一旦容器具有固定的宽度,图表就会变得越失真,屏幕越小。
In the chart I have already tried setting the options responsive: true
and maintainAspectRatio: false
. 在图表中,我已经尝试过设置选项
responsive: true
和maintainAspectRatio: false
。 This solved the problem a bit, but the chart is still distorted. 这一点解决了问题,但图表仍然失真。
Chart.js Options: Chart.js选项:
var options = {
responsive: true,
maintainAspectRatio: false,
scales: {
yAxes: [{
stacked: false,
gridLines: {
display: true,
color: "rgba(151,151,151,0.2)"
}
}],
xAxes: [{
gridLines: {
display: true,
color: "rgba(151,151,151,0.2)"
}
}]
}
};
HTML: HTML:
<div class="tds-chart-inner tds-hor-scroll">
<canvas id="tds-chart-mem"></canvas>
</div>
CSS: CSS:
.tds-chart-inner {
min-width: 850px !important;
}
.tds-hor-scroll {
left: 0px;
width: 100% !important;
overflow-x: scroll;
}
canvas {
left: 0px;
height: 300px !important;
}
Working chart 工作图
Distorted chart 图表失真
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
</head>
<style>
.tds-chart-inner {
width:100%;
}
</style>
<body class="">
<div class="tds-chart-inner tds-hor-scroll">
<canvas id="myChart"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js">
</script>
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3,80],
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,
fontColor: 'red'
}
}]
}
}
});
</script>
for me the problem was that graph was setting for itself height that was bigger that I myself had set to canvas. 对我来说,问题是图形本身的高度设置得比我自己设置的要大。
my solution was: 我的解决方案是:
responsive: true, maintainAspectRatio: false,
in the example in the question problem was in css: 在问题的示例中,问题在CSS中:
canvas { left: 0px; height: 300px !important; }
i would change it to: 我将其更改为:
.tds-hor-scroll { overflow-x: auto; } .tds-chart-inner { min-width: 850px; height: 300px; }
<div class="tds-hor-scroll"> <div class="tds-chart-inner"> <canvas id="tds-chart-mem"></canvas> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.