gpt4 book ai didi

javascript - 谷歌图表 : how to set initial selection with listener for interactive

转载 作者:行者123 更新时间:2023-11-30 06:20:24 24 4
gpt4 key购买 nike

我有一些代码正试图在我的环境中进行操作,但无法获取最后一段。我想让图表以选择的初始值开始绘制,但我也想为用户选择的值留下监听器。我尝试使用初始化变量来标记首次加载和后续点击,但这似乎不起作用。

非常感谢任何帮助。

Date.prototype.getWeekNumber = function(){
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
};

function(response) {

var i;

google.charts.load("current", {packages:["calendar"]});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'date', id: 'Date' });
dataTable.addColumn({ type: 'number', id: 'Changes' });

for(var i=0; i < response.d.Rows.length; i++){
var row = [new Date(response.d.Rows[i][0]),response.d.Rows[i][1]];
dataTable.addRow(row);
}

var chart = new google.visualization.Calendar(document.getElementById('calendar_basic'));

var options = {
title: 'Total Alerts Triggered By Day',
calendar: { cellSize: 25 },
colorAxis: {colors:['#86ce76','#d61007']},
};

<!--Event listener starting code-->
function selectHandler() {
<!-- Do Some Code -->
}

google.visualization.events.addListener(chart, 'select', selectHandler);
<!--End event listener-->

<!--My Test-->
if !(response){

var weekNumber = (new Date()).getWeekNumber();
var dayOfWeek = 0; <!--(new Date()).getDay();-->
chart.setSelection([{'row':dayOfWeek,'column':weekNumber}]);
}

chart.draw(dataTable, options);

因此监听器可以工作,但我想要的初始值(在图表中选择当前日期)不起作用。

最佳答案

Rude Dawg,我正在尝试对你在 github 上提供的相同代码做同样的事情并且具有讽刺意味。我相信我现在已经开始工作了。这是我用来执行此操作的代码。希望您可以将这段代码片段与您的代码相匹配


google.visualization.events.addOneTimeListener(chart, 'ready', function () {
selectHandler([{date: (new Date).getTime()}]);
});


<!--Event listener starting code-->
function selectHandler(initialDate) {
var selectedItem;
var selection = initialDate || chart.getSelection();
if (initialDate) {
selectedItem = {"row":0};
}
else if (selection.length > 0) {
console.log(new Date(selection[0].date));
selectedItem = selection[0];
}

if (selectedItem) {

关键是只传递你想用哪一行来初始化其他图形。

关于javascript - 谷歌图表 : how to set initial selection with listener for interactive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53494503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com