Forked from alexrainman/Responsive PieChart with custom tooltip
Last active
August 29, 2015 14:10
-
-
Save he-and-her/dc6758b286b10d8ca37a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> | |
<script type="text/javascript" src="https://www.google.com/jsapi"></script> | |
<style> | |
#piechart { | |
top: 0; | |
left: 0; | |
width:100%; | |
height:100%; | |
} | |
.google-visualization-tooltip{ | |
display:table; | |
} | |
g{ | |
cursor:pointer; | |
} | |
</style> | |
<script type="text/javascript"> | |
google.load("visualization", "1", {packages:["corechart"]}); | |
google.setOnLoadCallback(initChart); | |
$(window).on("resize", function (event) { | |
initChart(); | |
}); | |
function initChart() { | |
var options = { | |
legend:'none', | |
width: '100%', | |
height: '100%', | |
pieSliceText: 'label', | |
tooltip: { isHtml: true }, | |
chartArea: { | |
left: "3%", | |
top: "3%", | |
height: "94%", | |
width: "94%" | |
} | |
}; | |
var data = google.visualization.arrayToDataTable([ | |
['Risk Factors', 'Percentage'], | |
['Smoking', 20], | |
['Alcohol', 20], | |
['Medicines', 20], | |
['Diet', 20], | |
['Lifestyle', 20] | |
]); | |
drawChart(data, options) | |
} | |
function drawChart(data, options) { | |
var tooltip = [ | |
"Cigarattes and other forms on smokeless tobacco promote osteoporosis", | |
"Excessive alcohol consumption accelerates bone loss", | |
"Chronic steroid use and other medications can promote osteoporosis", | |
"Diet deficient in osteoporosis puts you at risk of osteoporosis", | |
"Lifestyle without exercise increases bone loss and makes muscle weak" | |
]; | |
var chart = new google.visualization.PieChart(document.getElementById('piechart')); | |
var sliceid = 0; | |
function eventHandler(e){ | |
chart.setSelection([e]); | |
try { | |
selection = chart.getSelection(); | |
sliceid = selection[0].row; | |
} | |
catch(err) { | |
; | |
} | |
$(".google-visualization-tooltip-item-list li:eq(0)").css("font-weight", "bold"); | |
$(".google-visualization-tooltip-item-list li:eq(1)").html(tooltip[sliceid]).css("font-family", "Arial"); | |
} | |
google.visualization.events.addListener(chart, 'onmouseover', eventHandler); | |
chart.draw(data, options); | |
} | |
</script> | |
</head> | |
<body> | |
<div id="piechart"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment