1
我製作了一個可以用css和VW
(視口寬度)完全調整大小的系統/網站。在該系統/網站中,我有一個可與pixels
一起使用的GoogleChart。現在我想用Javascript/jQuery/CSS來縮放圖表(transform scale
)。在頁面加載就足夠了。窗口寬度/高度上的div比例
任何人都可以幫助我在正確的方向嗎?
這裏是與問題的jsfiddle:https://jsfiddle.net/j9a9wpdj/
// Load the Visualization API and the corechart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width': 400,
'height': 300};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
#test {
\t width: 80vw;
height: 40vw;
background-color: #dcdcdc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="test">
<div id="chart_div"></div>
</div>
[結論] 這對我來說是什麼在起作用:
function zoomit() {
$("#chart-wrapper").css('zoom', $(window).width()/$("#chart-wrapper").width());
}
$(document).ready(zoomit);
$(window).resize(zoomit);
感謝您的回答,但那不是我要找的。我想整個圖表縮放到窗口寬度 –
@EmielSchumacher - 檢查更新的答案 – Pugazh
你是最好的.. !!非常感謝你。這對我來說真的是一場艱難.. –