我正在尝试在模态弹出窗口中显示Google图表。它在普通页面上工作得很好,但它没有出现在Modal上。
这是我正在使用的代码。我不确定会出现什么问题。
<!--Div that will hold the pie chart-->
<h3 style="text-align:center;">Test</h3>
<hr>
<div id="chart_div" style="height: 500px;">
</div>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Options', 'Votes'],
['1', 11],
['2', 2],
['3', 2],
['4', 2],
['5', 7],
['6', 21],
]);
// Set chart options
var options = {
'backgroundColor': 'transparent',
'is3D': 'True',
'legend':'bottom',
'width':'100%',
'height':'100%',
chartArea:{left:0,top:0,width:"100%",height:"80%"}
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<a class="open-newmodal" href="/poll/">show poll</a>
所以我的想法是,使用模态和jquery的.load()函数,我应该能够将生成在/ poll / page上的图表加载到我所做的模态中的/ home / page。
这是我正在使用的模式
.newmodal-container {
display: none;
overflow-y: scroll;
position: fixed;
top: 0; right: 0;
height: 100%;
width: 100%;
background-color: rgba(0, 0, 0, 0.3);
}
.newmodal {
min-height: 100%;
height: auto;
width: 60%;
margin: 0px auto;
background-color: white;
padding: 20px;
border:1px solid #D3D3D3;
box-shadow: 0px 0px 5px #D3D3D3;
}
.dp-none {
display: none !important;
}
.dp-block {
display: block !important;
}
// New Modal
var body = $('body'),
main = $('.main'),
open_modal = $('.open-newmodal'),
close_modal = $('.close-newmodal'),
modal_container = $('.newmodal-container');
open_modal.live('click', function(event){
event.preventDefault();
body.addClass('body-locked');
modal_container.addClass('dp-block');
var href = $(this).attr('href');
modal = $('.data-load');
modal.load(href);
modal.attr("title",href);
});
close_modal.live('click', function(event){
event.preventDefault();
body.removeClass('body-locked');
modal_container.removeClass('dp-block');
modal.empty();
});
$(document).keyup(function(e) {
if (e.keyCode == 27){
event.preventDefault();
body.removeClass('body-locked');
modal_container.removeClass('dp-block');
modal.empty();
}
});
答案 0 :(得分:0)
这里有一些代码可以解释我在评论中的意思。
基本上,当单击按钮显示图表时,就是在为饼图加载时。
希望这能让你走上正轨。
<button onclick="displayModal">Show Chart <\button>
<div id="modal_chart" class="modal">
<!--Div that will hold the pie chart-->
<h3 style="text-align:center;">Test</h3>
<hr>
<div id="chart_div" style="height: 500px;">
</div>
</div>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
function displayModal() {
$("#modal_chart").fadeIn(fast, function() {
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
});
}
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Options', 'Votes'],
['1', 11],
['2', 2],
['3', 2],
['4', 2],
['5', 7],
['6', 21],
]);
// Set chart options
var options = {
'backgroundColor': 'transparent',
'is3D': 'True',
'legend':'bottom',
'width':'100%',
'height':'100%',
chartArea:{left:0,top:0,width:"100%",height:"80%"}
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
答案 1 :(得分:0)
我认为加载图表然后应用模态代码时不应该有任何明显的问题。下面是使用您提到的一些CSS和JQueryUI替代http://jsbin.com/ajerol/3的示例(您也可以在此处主动编辑代码http://jsbin.com/ajerol/3/edit)。
<!DOCTYPE html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: 'My Daily Activities'
};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
function displayModal() {
$("#modal_chart").show();
}
function hideModal() {
$("#modal_chart").hide();
}
function makeModal() {
$("#modal_chart").addClass('newmodal-container');
modal_container = $('.newmodal-container');
$("#modal_chart").show();
}
function jqueryModal() {
$("#modal_chart").dialog({ modal: true });
}
</script>
</head>
<body>
<button onclick="displayModal()">Show Chart </button>
<button onclick="hideModal()">Hide Chart </button>
<button onclick="makeModal()">Attach modal CSS </button>
<button onclick="jqueryModal()">jQuery modal </button>
<div id="modal_chart" class="modal">
<!--Div that will hold the pie chart-->
<h3 style="text-align:center;">Test</h3>
<hr>
<div id="chart_div" style="height: 300px; width: 300px;">
</div>
</div>
</body>
关于您的代码的一个问题,您是在html加载之前调用以下代码吗?
open_modal = $('.open-newmodal'),
close_modal = $('.close-newmodal'),
modal_container = $('.newmodal-container');
答案 2 :(得分:0)
好的,看看你想如何将另一个页面加载为模态图,我有以下解决方案。
在http://jsbin.com/abucet/3中初始化父页面上的google lib,然后确保您的子页面不会重新初始化或重新包含父页面中加载的lib,请参阅child @ {{ 3}}
所以基本上是子页面@ http://jsbin.com/ajerol/5/edit 访问子项@ http://jsbin.com/ajerol/5
的父页面父页面
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div id='headerDiv' style='background-color:#FF0000'>
Test Load
</div>
<div id='modalDiv' style='background-color:#FFAA00'>
Close this and click the button to load
Graph will be in a modal JQuery Dialog. Some of the google visualization initialization will be done in this page, not the page we're loading.
</div>
<button id='getIt'>Get the Graph</button>
</body>
</html>
Javascript
$('#getIt').click( function() {
$.get( "http://jsbin.com/ajerol/4",
function(data) {
var newGraph= "<div id='graphModal'>"+data+"</div>";
//clean up html
newGraph.replace('<body>','');
newGraph.replace('</body>','');
// we have the libs on this page, no need for that other cruft
newGraph.replace('<head.*head>','');
// debug:
//alert(newGraph);
// append to the current modal
$('body').append(newGraph);
// the graph page in modal
$('#graphModal').dialog({modal:true,width:300});
$('#getIt').parent().append('<button onclick="$(\'#graphModal\'\).dialog(\);">showGraph</button>');
}
);
});
// initial note
$('#modalDiv').dialog({modal:true});
// load this on the parent page, not the one we're calling
google.load("visualization", "1", {packages:["corechart"]});
儿童页面
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: 'My Daily Activities'
};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<div id="modal_chart" class="modal">
<!--Div that will hold the pie chart-->
<div id="chart_div" style="height: 300px; width: 300px;">
</div>
</div>
儿童Javascript(正在加载)
drawChart();
答案 3 :(得分:0)
您可以使用远程网址中的谷歌图表和模态,如下所示:
在远程网址上,您需要以这种方式加载Google链接:
enter code here
$.getScript(
"https://www.google.com/jsapi",
function() {
google.load('visualization', '1', {'packages':['corechart'],"callback": drawChart});
});
为我工作。