Google Charts API의 최신 버전 사용.Google 차트의 범례에 툴팁을 추가하는 방법
간단한 막대 차트가 있으며 범례의 각 항목이 무엇인지 설명하는 범례의 요소 위로 마우스를 가져 가면 툴팁이 표시됩니다. 막대의 툴팁이 동일하게 유지되고 레이블과 값을 표시하는 것이 좋습니다. 당신은 "데이터 2010 년 미국 인구 조사에서 가져온"라고 위로 마우스를 가져 가면 위의 차트에서
<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 년 인구 전설 요소에 툴팁을 추가?
UPDATE
답을 알아내는 후 난 당신이 전설 쇼 다른 설명의 각 요소를 만들 수있는 방법을 보여주기 위해 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) {
\t \t \t if (data.getColumnLabel(gglEvent.column) === '2010 Population') {
\t \t \t \t $('#series-name').html(data.getColumnLabel(gglEvent.column) + ' - Data from 2010 Census');
\t \t \t } else if (data.getColumnLabel(gglEvent.column) === '2011 Population') {
\t \t \t \t $('#series-name').html(data.getColumnLabel(gglEvent.column) + ' - Data from 2011 Census');
\t \t \t }
$(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>