/    Sign up×
Community /Pin to ProfileBookmark

Help with Bar Graph

Hello!

I need some help in understanding how to add percentages to my bar graph.

Can someone please tell me how this can be done? I get the number rates, but I would like the percentages as well.

Here is my code:

[code=html]<html>
<head>
<script type=”text/javascript” src=”https://www.google.com/jsapi”></script>
</head>
<script type=”text/javascript”>
google.load(“visualization”, “1”, {packages:[“corechart”]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
[“Race”, “Number”, { role: “style” } ],
[“African American”, 2411,”#7D110C”],
[“Hispanic/Latino”, 1076, “#82786F”],
[“Asian/American”, 735, “#7D110C”],
[“Two or More Races”, 692, “#82786F”],
[“International Students”, 684, “#7D110C”],
[“American Indian/Alaskan Native”, 28, “#82786F”],
[“Native Hawaiian/Pacific Islander”, 9, “color: #7D110C”]
]);

var view = new google.visualization.DataView(data);
view.setColumns([0, 1,
{ calc: “stringify”,
sourceColumn: 1,
type: “string”,
role: “annotation” },
2]);

var options = {
title: “Ethnically Diverse Undergraduates”,
width: 1200,
height: 400,
bar: {groupWidth: “65%”},
legend: { position: “none” },
};
var chart = new google.visualization.ColumnChart(document.getElementById(“columnchart_values”));
chart.draw(view, options);
}
</script>

<script>
google.load(“visualization”, “1”, {packages:[“corechart”]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
[“Race”, “Number”, { role: “style” } ],
[“African American”, 2411,”#7D110C”],
[“Hispanic/Latino”, 1076, “#82786F”],
[“Asian/American”, 735, “#7D110C”],
[“Two or More Races”, 692, “#82786F”],
[“International Students”, 684, “#7D110C”],
[“American Indian/Alaskan Native”, 28, “#82786F”],
[“Native Hawaiian/Pacific Islander”, 9, “color: #7D110C”]
]);

var options = {
title: ‘Ethnically Diverse Undergraduates’,
legend: ‘none’
};

var chart = new google.visualization.PieChart(document.getElementById(‘piechart2’));
chart.draw(data, options);

var total = 0;
for (var i = 0; i < data.getNumberOfRows(); i++) {
total += data.getValue(i, 1);
}

var legend = document.getElementById(“legend”);
var legItem = [];
var colors = [‘#7D110C’, ‘#82786F’, ‘#7D110C’, ‘#82786F’, ‘#7D110C’, ‘#82786F’, ‘#7D110C’];
for (var i = 0; i < data.getNumberOfRows(); i++) {
var label = data.getValue(i, 0);
var value = data.getValue(i, 1);
var percent = Number(100 * value / total).toFixed(1);

// This will create legend list for the display
legItem[i] = document.createElement(‘li’);
legItem[i].id = ‘legend_’ + data.getValue(i, 0);
legItem[i].innerHTML = ‘<div class=”legendMarker”> <span style=”color:’ + colors[i] + ‘;”>’ + label + ‘: ‘ + value + ‘(‘ + percent + ‘%)</span></div>’;

legend.appendChild(legItem[i]);
}
}

</script>

<body>
<div id=”columnchart_values” style=”width: 1500px; height: 300px;”></div>

<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<style>
#piechart2 {
width: 700px;
height: 400px;
position: relative;
float: left;
}

#legend {
margin-top: 100px;
margin-right: 50px;
}
</style>
<div id=”piechart2″></div>
<div id=”legend”></div>
</body>
</html>
[/code]

Thanks!

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @phannum spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...