데이터 시각화를 위해 Google Charts API를 사용하고 있습니다. 차트에서 선택한 날짜의 날짜를 검색하고 싶습니다.
내가 검색하는 것은 매우 이상합니다. 문서에서 권장하는대로 차트에서 selectHandler를 사용하고 있습니다.
function selectHandler() {
console.log(chart.getSelection()[0]);
}
google.visualization.events.addListener(chart, 'select', selectHandler);
다음 날짜를 클릭하면 콘솔에서 다음 출력을 읽습니다.
22/04/2020 : 1587513600000
19/06/2020 : 1592524800000
이것은 어떤 종류의 날짜 형식입니까? 누구든지 나를 도울 수 있습니까?
이것은 Unix Epoch 이후 밀리 초 수로 표시된 날짜입니다. 날짜 객체
의 getTime()
메서드가 반환 한 값과 동일 합니다.
선택에서받은 값을 날짜 생성자에 전달하여 날짜를 가져올 수 있습니다.
var selection = chart.getSelection();
if (selection.length > 0) {
var selectedDate = new Date(selection[0].date);
}
다음 작업 스 니펫 참조 ...
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(2012, 3, 13), 37032 ],
[ new Date(2012, 3, 14), 38024 ],
[ new Date(2012, 3, 15), 38024 ],
[ new Date(2012, 3, 16), 38108 ],
[ new Date(2012, 3, 17), 38229 ],
[ new Date(2013, 9, 4), 38177 ],
[ new Date(2013, 9, 5), 38705 ],
[ new Date(2013, 9, 12), 38210 ],
[ new Date(2013, 9, 13), 38029 ],
[ new Date(2013, 9, 19), 38823 ],
[ new Date(2013, 9, 23), 38345 ],
[ new Date(2013, 9, 24), 38436 ],
[ new Date(2013, 9, 30), 38447 ]
]);
var chart = new google.visualization.Calendar(document.getElementById('calendar_basic'));
var options = {
title: "Red Sox Attendance",
height: 350,
};
function selectHandler() {
var selection = chart.getSelection();
if (selection.length > 0) {
var selectedDate = new Date(selection[0].date);
console.log(selectedDate);
}
}
google.visualization.events.addListener(chart, 'select', selectHandler);
chart.draw(dataTable, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="calendar_basic"></div>
참고 : 콘텐츠에 액세스하기 전에 선택 항목의 길이를 확인해야합니다.
선택 이벤트는 날짜가 선택되지 않은 경우에도 호출됩니다.
이 경우 선택 배열은 비어 있습니다 ...
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다