我正在尝试使用display:none和display:block在我的网站上。风格如下:
<style>
.div_class {
display: none;
}
</style>
正文内容为:
<div class="div_class">
<div id="graph_id"></div>
</div>
脚本是:
<script>
// some event occurs and sets the display property of div_class to block as follows
$(".div_class").css('display','block');
var BarGraph = echarts.init(document.getElementById('graph_id'), theme);
BarGraph.setOption({
// properties that are set for the echart 'graph_id'
});
</script>
display: block;
使div可见,但设置的echart属性不会呈现。 div显示为空白。可能是什么原因?我无法弄明白。请帮助。谢谢:))
答案 0 :(得分:0)
显示div
后重新呈现BarGraph答案 1 :(得分:0)
BarGraph .setOption({
与
的作用相同 BarGraph.setOption({
? (摆脱空间)
另外,尝试将所有代码包装在
中$(document).ready(function() {
***code here***
});
编辑:
^从那里获取代码,将jquery添加到head,在div.container中包装图表块,显示:无,创建点击事件
$(document).on('click', function() {
$(".container").show();
myChart.setOption(option);
});
工作片段:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
<!-- including ECharts file -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="http://echarts.baidu.com/dist/echarts.common.min.js"></script>
</head>
<body>
<style>
.container {
display: none;
}
</style>
<!-- prepare a DOM container with width and height -->
click anywhere!
<div class="container">
<div id="main" style="width: 600px;height:400px;"></div>
</div>
<script type="text/javascript">
// based on prepared DOM, initialize echarts instance
var myChart = echarts.init(document.getElementById('main'));
// specify chart configuration item and data
var option = {
title: {
text: 'ECharts entry example'
},
tooltip: {},
legend: {
data:['Sales']
},
xAxis: {
data: ["shirt","cardign","chiffon shirt","pants","heels","socks"]
},
yAxis: {},
series: [{
name: 'Sales',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// use configuration item and data specified to show chart
$(document).on('click', function() {
$(".container").show();
myChart.setOption(option);
});
</script>
</body>
</html>
&#13;
答案 2 :(得分:-1)
我尝试使用chart.js
如果display
属性设置为none
,则无法绘制图表。事情是visibility:hidden
保留空间,而display:none
不保留空间
<style>
.div_class{
visibility:hidden;
}
</style>
尝试使用上面的代码替换css。我希望这会对你有所帮助:)。
您可以参考以下帖子获取更多信息:Difference between visibility:hidden and display:none?
答案 3 :(得分:-1)
<script>
// some event occurs and sets the display property of div_class to block as follows
$(".div_class").css('display','block').promise().done(function(){
//draw graph here
var BarGraph = echarts.init(document.getElementById('graph_id'), theme);
BarGraph .setOption({
// properties that are set for the echart 'graph_id'
});
});
</script>
尝试以上代码。我添加了.promise()它的作用只是在css&#34; display:block;&#34;之后。然后应用图形绘制过程