【问题标题】:How to resize canvasjs chart when its container's size changes dynamically?当容器的大小动态变化时如何调整canvasjs图表的大小?
【发布时间】:2017-10-11 14:32:26
【问题描述】:

先看这个例子https://codepen.io/merajahmed/pen/PJaeWR

window.onload = function () {

var chart = new CanvasJS.Chart("chartContainer", {
	animationEnabled: true,
	exportEnabled: true,
	theme: "light1", // "light1", "light2", "dark1", "dark2"
	title:{
		text: "Simple Column Chart with Index Labels"
	},
	data: [{
		type: "column", //change type to bar, line, area, pie, etc
		//indexLabel: "{y}", //Shows y value on all Data Points
		indexLabelFontColor: "#5A5757",
		indexLabelPlacement: "outside",
		dataPoints: [
			{ x: 10, y: 71 },
			{ x: 20, y: 55 },
			{ x: 30, y: 50 },
			{ x: 40, y: 65 },
			{ x: 50, y: 92, indexLabel: "Highest" },
			{ x: 60, y: 68 },
			{ x: 70, y: 38 },
			{ x: 80, y: 71 },
			{ x: 90, y: 54 },
			{ x: 100, y: 60 },
			{ x: 110, y: 36 },
			{ x: 120, y: 49 },
			{ x: 130, y: 21, indexLabel: "Lowest" }
		]
	}]
});
chart.render();

}
.sidebar {
    background-color: black;
    width: 150px;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
}

.sidebar h1 {
    color: white;
}

.portfolio {
    background-color: red;
    position: absolute;
    top: 0;
    left: 150px;
    right: 0;
    bottom: 0;
    
    -moz-transition: left 0.5s ease;
    transition: left 0.5s ease;
}

input[type=checkbox] {
   display: none;
}

input:checked ~ .portfolio { 
    left: 0;
}

input:checked ~ label {
    left: 0;
}

label {
    z-index: 2;
    position: absolute;
    top: 0;
    left: 150px;
    background-color: blue;
    
    -moz-transition: left 0.5s ease;
    transition: left 0.5s ease;
}
<div class="main-wrap">
    <input id="slide-sidebar" type="checkbox" role="button" />
        <label for="slide-sidebar"><span>close</span></label>
    <div class="sidebar"><h1>Settings</h1></div>
    <div class="portfolio">
      
      <div id="chartContainer" style="height: 370px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
    </div>
</div>

我想在单击关闭按钮时将图表调整为全宽,但图表宽度没有改变。 由于在调整图表容器大小之间存在动画,我希望图表在动画期间占据其容器的全宽。意味着,图表应该在动画期间扩大/缩小其宽度。

【问题讨论】:

    标签: javascript html css charts canvasjs


    【解决方案1】:

    呈现图表的函数由onload事件触发。

    只需提取该函数定义,将其命名为render,然后为onresize 事件触发相同的函数。

    https://codepen.io/anon/pen/JrZvQy

    window.onload = render;
    window.onresize = render;
    

    【讨论】:

    • @MerajAhmed 添加了代码以在单击关闭按钮时触发渲染功能。这是一个使用setTimeout 的简单示例,但应该足以让您入门。
    • 是的,我明白了。但它重新渲染图表。它的大小在动画期间不会改变。如果您尝试调整浏览器窗口的大小,图表会自动调整大小,而无需在我的 codepen 中重新渲染。我想要同样的效果。
    • 我想你应该有足够的能力去弄清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2010-10-26
    • 2018-03-03
    相关资源
    最近更新 更多