使用最新版本的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年人口图例元素添加工具提示?&#34;?
更新
在弄清楚答案之后,我已经制作了一个编码器,以展示如何使图例中的每个元素显示不同的描述。我希望这有助于将来的某个人。
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);
}
&#13;
.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;
}
&#13;
<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>
&#13;
答案 0 :(得分:2)
没有关于图例条目的标准工具提示,但您可以添加自己的...
请参阅以下工作代码段
在这里,我使用图表事件onmouseover
和onmouseout
,
知道传说何时被“徘徊”
当事件触发时,如果事件的row
属性为null
,则
然后传说正在徘徊
我还使用getChartLayoutInterface
将工具提示放在图例附近
google.charts.load('current', {
callback: drawBasic,
packages: ['corechart']
});
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'));
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) {
$('#series-name').html(data.getColumnLabel(gglEvent.column));
$(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><span>Series Info</span></div>
<div id="series-name"></div>
</div>