如何在容器的大小动态变化时调整canvasjs图表的大小?

时间:2017-10-11 14:32:27

标签: javascript html css charts canvasjs

首先看一下这个例子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>

我想在单击关闭按钮时将图表调整为全宽,但图表宽度不会改变。 由于在调整图表容器大小之间有动画,我希望图表在动画期间占据其容器的整个宽度。手段,图表应在动画期间增大/缩小其宽度。

1 个答案:

答案 0 :(得分:1)

呈现图表的功能由onload事件触发。

只需提取该功能定义,为其命名,例如render,然后为onresize事件触发相同的功能。

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

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