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

标签 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>

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

最佳答案

渲染图表的函数由onload 事件触发。

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

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

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

关于javascript - 如何在容器大小动态变化时调整 canvasjs 图表的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46690954/

相关文章:

css - 向下滚动时展开 DIV 到页面底部

css - 为什么这个导航在 Firefox 上坏了?

html - 数据:image/png;base64 mean?是什么意思

javascript - 简单的 jQuery $.get 示例代码不起作用

javascript - 检查 Javascript 中的按钮是否被单击

javascript - 创建一个 DIV 并给它一个 data-cart-info 属性。在 DIV 中,创建一个带有 mdc-typography--headline4"的标题作为其 CSS 类

javascript - Jquery 元素用法

javascript - Angular 2 : import external js file into component

css - 检查元素计数是否为奇数,否则仅使用 CSS

css - 显示带有淡出效果的 W