使用最新版本的Google Charts API。
我有一个简单的条形图,希望在悬停在图例元素上时显示工具提示,以解释每个图例项的含义。 我仍希望在条形上保留相同的工具提示,并显示其标签和值。
我有一个简单的条形图,希望在悬停在图例元素上时显示工具提示,以解释每个图例项的含义。 我仍希望在条形上保留相同的工具提示,并显示其标签和值。
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawBasic);
function drawBasic() {
var data = google.visualization.arrayToDataTable([
['City', '2010 Population',],
['New York City, NY', 8175000],
['Los Angeles, CA', 3792000],
['Chicago, IL', 2695000],
['Houston, TX', 2099000],
['Philadelphia, PA', 1526000]
]);
var options = {
title: 'Population of Largest U.S. Cities',
chartArea: {width: '50%'},
hAxis: {
title: 'Total Population',
minValue: 0
},
vAxis: {
title: 'City'
}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
在上面的图表中,当您将鼠标悬停在2010年人口图例元素上时,您该如何添加一个工具提示,显示“数据来自2010年美国人口普查”?
更新
找到答案后,我已经制作了一个CodePen来展示如何使图例中的每个元素显示不同的描述。希望这能帮助未来的某个人。
https://codepen.io/jonnyske7ch/pen/bWzmxW
google.charts.load('current', {
callback: drawBasic,
packages: ['corechart']
});
function drawBasic() {
var data = google.visualization.arrayToDataTable([
['City', '2010 Population', '2011 Population'],
['New York City, NY', 8175000, 3792000],
['Los Angeles, CA', 3792000, 1526000],
['Chicago, IL', 2695000, 8175000],
['Houston, TX', 2099000, 2695000],
['Philadelphia, PA', 1526000, 2099000]
]);
var options = {
title: 'Population of Largest U.S. Cities',
chartArea: {width: '50%'},
hAxis: {
title: 'Total Population',
minValue: 0
},
vAxis: {
title: 'City'
}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
var legendTooltip = document.getElementById('legend-tooltip');
// set legend tooltip position
google.visualization.events.addListener(chart, 'ready', function (gglEvent) {
var chartLayout = chart.getChartLayoutInterface();
var legendBounds = chartLayout.getBoundingBox('legend');
legendTooltip.style.top = (legendBounds.top + (legendBounds.height * 2)) + 'px';
legendTooltip.style.left = legendBounds.left + 'px';
});
// show legend tooltip
google.visualization.events.addListener(chart, 'onmouseover', function (gglEvent) {
if (gglEvent.row === null) {
if (data.getColumnLabel(gglEvent.column) === '2010 Population') {
$('#series-name').html(data.getColumnLabel(gglEvent.column) + ' - Data from 2010 Census');
} else if (data.getColumnLabel(gglEvent.column) === '2011 Population') {
$('#series-name').html(data.getColumnLabel(gglEvent.column) + ' - Data from 2011 Census');
}
$(legendTooltip).removeClass('hidden');
}
});
// hide legend tooltip
google.visualization.events.addListener(chart, 'onmouseout', function (gglEvent) {
if (gglEvent.row === null) {
$(legendTooltip).addClass('hidden');
}
});
chart.draw(data, options);
}
.hidden {
display: none;
visibility: hidden;
}
.ggl-tooltip {
background-color: #ffffff;
border: 1px solid #E0E0E0;
display: inline-block;
font-size: 10pt;
padding: 8px 8px 8px 8px;
position: absolute;
}
.ggl-tooltip div {
margin-top: 4px;
}
.ggl-tooltip span {
font-weight: bold;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div id="chart_div"></div>
<div id="legend-tooltip" class="ggl-tooltip hidden">
<div id="series-name"></div>
</div>