我正在使用Google Charts Gantt在我的页面中绘制几个图表,这是代码:
function drawGantt() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
data.addColumn('string', 'Resource');
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([
['2014Spring', 'Spring 2014', 'spring',
new Date(2014, 2, 22), new Date(2014, 5, 20), null, 100, null],
['2014Summer', 'Summer 2014', 'summer',
new Date(2014, 5, 21), new Date(2014, 8, 20), null, 100, null],
['2014Autumn', 'Autumn 2014', 'autumn',
new Date(2014, 8, 21), new Date(2014, 11, 20), null, 100, null],
['2014Winter', 'Winter 2014', 'winter',
new Date(2014, 11, 21), new Date(2015, 2, 21), null, 100, null],
['2015Spring', 'Spring 2015', 'spring',
new Date(2015, 2, 22), new Date(2015, 5, 20), null, 50, null],
['2015Summer', 'Summer 2015', 'summer',
new Date(2015, 5, 21), new Date(2015, 8, 20), null, 0, null],
['2015Autumn', 'Autumn 2015', 'autumn',
new Date(2015, 8, 21), new Date(2015, 11, 20), null, 0, null],
['2015Winter', 'Winter 2015', 'winter',
new Date(2015, 11, 21), new Date(2016, 2, 21), null, 0, null],
['Football', 'Football Season', 'sports',
new Date(2014, 8, 4), new Date(2015, 1, 1), null, 100, null],
['Baseball', 'Baseball Season', 'sports',
new Date(2015, 2, 31), new Date(2015, 9, 20), null, 14, null],
['Basketball', 'Basketball Season', 'sports',
new Date(2014, 9, 28), new Date(2015, 5, 20), null, 86, null],
['Hockey', 'Hockey Season', 'sports',
new Date(2014, 9, 8), new Date(2015, 5, 21), null, 89, null]
]);
var els = document.getElementsByClassName("gantt-chart");
for(i = 0; i < els.length; i++){
var el = els[i];
var options = {
width: 1100,
height: 400,
chartArea: {width: '100%', height: '100%'},
gantt: {
trackHeight: 30
}
};
var chart = new google.visualization.Gantt(el);
chart.draw(data, options);
}
}
正如你在最后我所看到的那样,我在HTML中使用循环元素并为每个元素分配一个图表,但结果是除了1之外的所有图表都是空的,每次都是随机渲染的。
我读过Java Language Specification与不同图表的类似问题有关,但我无法真正应用该解决方案,因为我可以轻松地在页面中同时呈现超过15个图表。< / p>
如何在不与draw
功能冲突的情况下同时渲染所有这些图表?
提前致谢。
答案 0 :(得分:0)
代码似乎在这里工作正常......
google.charts.load('current', {'packages':['gantt']});
google.charts.setOnLoadCallback(drawGantt);
function drawGantt() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
data.addColumn('string', 'Resource');
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([
['2014Spring', 'Spring 2014', 'spring',
new Date(2014, 2, 22), new Date(2014, 5, 20), null, 100, null],
['2014Summer', 'Summer 2014', 'summer',
new Date(2014, 5, 21), new Date(2014, 8, 20), null, 100, null],
['2014Autumn', 'Autumn 2014', 'autumn',
new Date(2014, 8, 21), new Date(2014, 11, 20), null, 100, null],
['2014Winter', 'Winter 2014', 'winter',
new Date(2014, 11, 21), new Date(2015, 2, 21), null, 100, null],
['2015Spring', 'Spring 2015', 'spring',
new Date(2015, 2, 22), new Date(2015, 5, 20), null, 50, null],
['2015Summer', 'Summer 2015', 'summer',
new Date(2015, 5, 21), new Date(2015, 8, 20), null, 0, null],
['2015Autumn', 'Autumn 2015', 'autumn',
new Date(2015, 8, 21), new Date(2015, 11, 20), null, 0, null],
['2015Winter', 'Winter 2015', 'winter',
new Date(2015, 11, 21), new Date(2016, 2, 21), null, 0, null],
['Football', 'Football Season', 'sports',
new Date(2014, 8, 4), new Date(2015, 1, 1), null, 100, null],
['Baseball', 'Baseball Season', 'sports',
new Date(2015, 2, 31), new Date(2015, 9, 20), null, 14, null],
['Basketball', 'Basketball Season', 'sports',
new Date(2014, 9, 28), new Date(2015, 5, 20), null, 86, null],
['Hockey', 'Hockey Season', 'sports',
new Date(2014, 9, 8), new Date(2015, 5, 21), null, 89, null]
]);
var els = document.getElementsByClassName("gantt-chart");
for(i = 0; i < els.length; i++){
var el = els[i];
var options = {
width: 1100,
height: 400,
chartArea: {width: '100%', height: '100%'},
gantt: {
trackHeight: 30
}
};
var chart = new google.visualization.Gantt(el);
chart.draw(data, options);
}
}
&#13;
div {
border: 1px solid #e0e0e0;
padding: 6px;
}
&#13;
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
<div class="gantt-chart"></div>
&#13;