我正在努力让我的谷歌图表响应,这对我帮助很大:
<div class="embed-responsive embed-responsive-4by3">
<div id="chart_div" class="embed-responsive-item"></div>
</div>
因为我也在使用bootstrap。但是在我的PieChart中,我在中心的拼图中添加了一个叠加层。如何使拼贴覆盖层响应,就像它在中心的代码预览一样,但现在它已经过时了,调整浏览器的大小并不能使它变得更好。
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: 'My Daily Activities',
pieHole: 0.45,
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
&#13;
#pieHoleOverlay {
color:white;
text-align: center;
padding-top: 25px!important;
}
.pieHole {
display: block;
background: black;
height: 75px !important;
width: 75px !important;
position: absolute !important;
z-index: 10;
border-radius: 100%;
top: 140px !important;
left: 145px !important;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div class="embed-responsive embed-responsive-4by3">
<div id="piechart" class="embed-responsive-item"></div>
<div id="pieHoleOverlay" class="pieHole">test 99</div>
</div>
&#13;
答案 0 :(得分:0)
您可以在图表的'ready'
事件触发时定位叠加层...
使用图表方法 - &gt; getChartLayoutInterface().getBoundingBox(id)
这将为您提供传递的ID的界限
找到图表本身的界限......
chart.getChartLayoutInterface().getBoundingBox('chart')
找到第一个饼图片的边界等...
chart.getChartLayoutInterface().getBoundingBox('slice#0')
使用每个切片的边界来计算图表的总高度和宽度(仅限切片)
然后乘以pieHole
图表选项(0.45
)
另外,为了使图表响应,需要在窗口调整大小时重新绘制
请参阅以下工作代码段...
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: 'My Daily Activities',
pieHole: 0.45,
};
var container = document.getElementById('piechart');
var chart = new google.visualization.PieChart(container);
var overlay = document.getElementById('pieHoleOverlay');
google.visualization.events.addListener(chart, 'ready', function () {
var chartLayout = chart.getChartLayoutInterface();
var chartArea = chartLayout.getBoundingBox('chart');
var pieLabels = container.getElementsByTagName('text');
var pieHoleWidth;
var sliceBounds = {
bottom: null,
top: null,
left: null,
right: null,
height: null,
width: null
};
for (i = 0; i < data.getNumberOfRows(); i++) {
var slice = chartLayout.getBoundingBox('slice#' + i);
var sliceBottom = slice.top + slice.height;
var sliceRight = slice.left + slice.width;
sliceBounds.bottom = Math.max(sliceBottom, (sliceBounds.bottom || sliceBottom));
sliceBounds.right = Math.max(sliceRight, (sliceBounds.right || sliceRight));
sliceBounds.top = Math.min(slice.top, (sliceBounds.top || slice.top));
sliceBounds.left = Math.min(slice.left, (sliceBounds.left || slice.left));
}
sliceBounds.height = sliceBounds.bottom - sliceBounds.top;
sliceBounds.width = sliceBounds.right - sliceBounds.left;
if (data.getNumberOfRows() > 0) {
overlay.className = 'pieHole';
pieHoleWidth = (sliceBounds.width * options.pieHole);
overlay.style.left = (sliceBounds.left + (sliceBounds.width / 2) - (pieHoleWidth / 2)) + 'px';
overlay.style.width = pieHoleWidth + 'px';
overlay.style.height = overlay.style.width;
overlay.style.top = (((chartArea.height - chartArea.top) / 2) - (pieHoleWidth / 2)) + 'px';
overlay.style.lineHeight = overlay.style.height;
if (pieLabels.length > 0) {
overlay.style.fontSize = pieLabels[0].getAttribute('font-size') + 'px';
}
} else {
overlay.className = 'hidden';
}
});
chart.draw(data, options);
window.addEventListener('resize', function () {
chart.draw(data, options);
}, false);
});
&#13;
.pieHole {
background: black;
border-radius: 100%;
color: white;
position: absolute;
text-align: center;
z-index: 10;
}
&#13;
<script src="https://www.gstatic.com/charts/loader.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="embed-responsive embed-responsive-4by3">
<div id="piechart" class="embed-responsive-item"></div>
<div id="pieHoleOverlay" class="hidden">test 99</div>
</div>
&#13;