你可以使用图表 'select' 用于了解用户点击了哪个日期的事件。 使用日期格式化程序格式化所选日期并打开网址。
'select'
var formatDate = new google.visualization.DateFormat({ pattern: 'yyyy-MM-dd' }); google.visualization.events.addListener(chart, 'select', function () { var selection = chart.getSelection(); if (selection.length > 0) { window.open('http://thispage.php/?date=' + formatDate.formatValue(new Date(selection[0].date))); } });
请参阅以下工作代码段, 例如,url被写入控制台...
google.charts.load('current', { packages:['calendar'] }).then(function () { var dataTable = new google.visualization.DataTable(); dataTable.addColumn({ type: 'date', id: 'Date' }); dataTable.addColumn({ type: 'number', id: 'Won/Loss' }); dataTable.addRows([ [new Date(), 1] ]); var chart = new google.visualization.Calendar(document.getElementById('calendar_basic')); var options = { title: 'EVENTS CALENDAR PLOT', height: 350, noDataPattern: { backgroundColor: '#75f990', color: '#00ff34' } }; var formatDate = new google.visualization.DateFormat({ pattern: 'yyyy-MM-dd' }); google.visualization.events.addListener(chart, 'select', function () { var selection = chart.getSelection(); if (selection.length > 0) { //window.open('http://thispage.php/?date=' + formatDate.formatValue(new Date(selection[0].date))); console.log('http://thispage.php/?date=' + formatDate.formatValue(new Date(selection[0].date))); } }); chart.draw(dataTable, options); });
<script src="https://www.gstatic.com/charts/loader.js"></script> <div id="calendar_basic"></div>