chart.jsを使用して複数の図表を同時に表示しようとしています。私のセットアップのために 、私は図と(今のところ2)複数のiframeを作成split.htmlファイルを表示し、それらにchart.htmlをロードする1つのchart.htmlファイルを持っています。 chart.htmlを直接開くと、サイズ変更は機能しますが、iframeに読み込んだ場合は変更されません。 サイジング自体は既に奇妙なので、私はchart.jsでエラーを想像することしかできませんでした。次の "上位"要素(divは100%幅と高さが固定されたdiv)上に配置され、キャンバスに直接幅や高さを設定しても変更されません。以下のコードを参照してください。Chart.js:グラフがiframeでサイズ変更されない
chart.html:
<html>
<head>
<script src="./node_modules/chart.js/dist/Chart.bundle.js"></script>
<script src="./node_modules/jquery/dist/jquery.min.js"></script>
<script src="./js/diagram.js"></script>
</head>
<body>
<div id="container" style="width:100%; height:100%;">
<canvas id="diagram"></canvas>
</div>
</body>
split.html:
<html>
<head>
<link rel="stylesheet" href="./css/splitter.css" />
<script src="./node_modules/jquery/dist/jquery.min.js"></script>
<script src="./js/splitter.js"></script>
</head>
<body>
<div id="content">
</div>
</body>
diagram.js:
$(document).ready(function() {
var ctx = document.getElementById("diagram");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: /** excluded (unimportance) **/
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
// Resize chart
$(window).resize(function() {
console.log("resize works!");
if(myChart) myChart.update();
});
});
splitter.js:
$(document).ready(function() {
const splits = 2;
switch (splits) {
case 2:
$("#content").append(
$('<iframe />')
.attr("id", "frame1")
.attr("src", "./chart.html")
.addClass("width50 height100")
);
$("#content").append(
$('<iframe />')
.attr("id", "frame2")
.attr("src", "./chart.html")
.addClass("width50 height100 left50")
);
break;
}
});
splitter.css:あなたのsplitter.jsをmodifiingとdiv要素に
iframe {
position: fixed;
border: none;
margin: 0;
padding: 0;
}
.width100 {
width: 100%;
}
.height100 {
height: 100%;
}
.width50 {
width: 50%;
}
.height50 {
height: 50%;
}
.left50 {
left: 50%;
}
.top50 {
top: 50%;
}
iframeを使用する特別な理由は? –
いいえ、何が良い方法でしょうか? – ThexBasic