我们是否可以选择使用谷歌图表API在甘特图中根据时间对记录进行排序。我的数据是根据某些标准在内部排序的。我在谷歌网站搜索我没有运气。请任何人帮助我。
答案 0 :(得分:2)
可以在绘制图表之前对数据进行排序
// sort by start and end dates
data.sort([{column: 2}, {column: 3}]);
这将影响行在图表上显示的顺序(y轴)
图表会自动按日期/时间在x轴上排序
参见以下示例......
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
data.addColumn('date', 'Start Date');
data.addColumn('date', 'End Date');
data.addColumn('number', 'Duration');
data.addColumn('number', 'Percent Complete');
data.addColumn('string', 'Dependencies');
data.addRows([
['Research', 'Find sources',
new Date(2015, 0, 1), new Date(2015, 0, 5), null, 100, null],
['Write', 'Write paper',
null, new Date(2015, 0, 9), daysToMilliseconds(3), 25, 'Research,Outline'],
['Cite', 'Create bibliography',
null, new Date(2015, 0, 7), daysToMilliseconds(1), 20, 'Research'],
['Complete', 'Hand in paper',
null, new Date(2015, 0, 10), daysToMilliseconds(1), 0, 'Cite,Write'],
['Outline', 'Outline paper',
null, new Date(2015, 0, 6), daysToMilliseconds(1), 100, 'Research']
]);
// sort by start and end dates
data.sort([{column: 2}, {column: 3}]);
new google.visualization.Gantt(document.getElementById('chart_div')).draw(
data,
{
height: 275
}
);
},
packages:['gantt']
});
function daysToMilliseconds(days) {
return days * 24 * 60 * 60 * 1000;
}

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
&#13;