我有一个气泡图,里面有一些饼图。现在,我正专注于将饼图与各自的div对齐。饼图应该与div的左上角对齐,但是它们与中心对齐而不对我的top: 0px right: 0px attributes
做出反应。这是我的jsFiddle:
HTML / CSS
<div id="chart_div"></div>
.whiteHat {
border: none;
width: 100px;
position: absolute;
background-color: red;
}
JS
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawSeriesChart);
function drawSeriesChart() {
var data = google.visualization.arrayToDataTable([
['ID', 'Life Expectancy', 'Fertility Rate', 'Region', 'Population'],
['CAN', 80.66, 1.67, 'North America', 33739900],
['DEU', 79.84, 1.36, 'Europe', 81902307],
['DNK', 78.6, 1.84, 'Europe', 5523095],
['EGY', 72.73, 2.78, 'Middle East', 79716203],
['GBR', 80.05, 2, 'Europe', 61801570],
['IRN', 72.49, 1.7, 'Middle East', 73137148],
['IRQ', 68.09, 4.77, 'Middle East', 31090763],
['ISR', 81.55, 2.96, 'Middle East', 7485600],
['RUS', 68.6, 1.54, 'Europe', 141850000],
['USA', 78.09, 2.05, 'North America', 307007000]
]);
var options = {
title: 'Correlation between life expectancy, fertility rate ' +
'and population of some world countries (2010)',
hAxis: {
title: 'Life Expectancy'
},
vAxis: {
title: 'Fertility Rate'
},
bubble: {
textStyle: {
fontSize: 11
}
}
};
var container = document.getElementById('chart_div');
var chart = new google.visualization.BubbleChart(document.getElementById('chart_div'));
google.visualization.events.addListener(chart, 'ready', function() {
var layout = chart.getChartLayoutInterface();
for (var i = 0; i < data.getNumberOfRows(); i++) {
// add image above every fifth element
if (i % 5 == 0) {
var xPos = layout.getXLocation(data.getValue(i, 1));
var yPos = layout.getYLocation(data.getValue(i, 2));
var data2 = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var pie = document.getElementById('piechart');
var whiteHat = container.appendChild(document.createElement('div'));
whiteHat.className = 'whiteHat';
whiteHat.style.top = yPos + 'px';
whiteHat.style.left = xPos + 'px';
new google.visualization.PieChart(whiteHat).draw(data2, {
legend: {
position: 'none'
},
backgroundColor: 'transparent',
chartArea: {
top: 0,
left: 0,
width: "100%",
height: "100%"
},
});
}
}
});
chart.draw(data, options);
}
我只是想将饼图对齐div的左上角,我使用红色背景来显示div。我该如何解决这个问题?
答案 0 :(得分:0)
我刚刚添加了这个CSS,使它看起来不错:
.whiteHat {
left: 0 !important;
bottom: 0 !important;
width: 50% !important;
height: auto !important;
right: auto !important;
top: auto !important;
}
.whiteHat + .whiteHat {
left: auto !important;
bottom: 0 !important;
width: 50% !important;
height: auto !important;
right: 0 !important;
top: auto !important;
}
我使用!important
的主要原因是使用内联样式覆盖插件生成的样式。
预览强>
小提琴:https://jsfiddle.net/4acLpk6L/
<强>糟糕即可。左上角是吗?只需交换值。
.whiteHat {
left: 0 !important;
bottom: auto !important;
width: 50% !important;
height: auto !important;
right: auto !important;
top: 0 !important;
}
.whiteHat + .whiteHat {
left: auto !important;
bottom: auto !important;
width: 50% !important;
height: auto !important;
right: 0 !important;
top: 0 !important;
}
预览强>