您可以使用 ChartWrapper、ControlWrapper 和 Dashboard 来执行此操作。
Here is the documentation related to this from Google.
基本上,您不是使用new google.visualization.ScatterChart(document.getElementById('yourId')) 来启动图表,而是使用称为 ChartWrapper 的东西(在我看来,这是一种更简单、更易读的方法)。
然后您创建一个ControlWrapper(控件(日期范围过滤器)元素的包装器)。
最后,您通过您的Dashboard 将您的ControlWrapper 绑定到您的ChartWrapper。
您的数据可能是:
var data = google.visualization.arrayToDataTable([
['Age', 'Weight', 'Date'],
[ 8, 12, new Date(2015, 10, 1)],
[ 4, 5.5, new Date(2015, 10, 2)],
[ 11, 14, new Date(2015, 10, 3)],
[ 4, 5, new Date(2015, 10, 4)],
[ 3, 3.5, new Date(2015, 10, 5)],
[ 6.5, 7, new Date(2015, 10, 6)]
]);
ChartWrapper 可能如下所示:
var scatterWrap = new google.visualization.ChartWrapper({
chartType:'ScatterChart',
containerId:'chart_div',
dataTable:data,
view:{
columns:[0, 1] // This makes sure your ScatterChart won't try to use the third column for visualization, that would result in an error.
}
});
还有一个像这样的 ControlWrapper
var dateRangeWrap = new google.visualization.ControlWrapper({
controlType:'DateRangeFilter',
containerId:'dateRange',
options:{
filterColumnIndex:2
}
});
最后初始化并绑定到您的仪表板:
var googleDashboard = new google.visualization.Dashboard(document.getElementById('dashboard'));
googleDashboard.bind(dateRangeWrap, scatterWrap);
googleDashboard.draw(data);
这将全部以 this Fiddle. 结尾(请注意,您需要在 google.load 中加载 controls 而不是 corechart)。