$(document).ready(function() {
$(".btn").click(function () {
$("#bar").html("");
var data = JSON.parse($("#txtJson").val());
var total = 0;
$.each(data.items, function (i, item) {
total = total + parseFloat(item.value);
});
$("#bar").removeClass("hidden");
var barWidth = parseInt($("#bar").width());
$.each(data.items, function (i, item) {
var percent = parseFloat(parseFloat(item.value) / total * 100);
var sectionWidth = barWidth * percent / 100;
if (data.items.length==1) {
$("#bar").append("<div id='cell"+i+"' class='sectionStyle sectionFull' style='width:" + sectionWidth + "px;background-color:" + item.color + "'></div>");
} else {
if (i == 0) {
$("#bar").append("<div id='cell" + i + "' class='sectionStyle sectionLeft' style='width:" + sectionWidth + "px;background-color:" + item.color + "'></div>");
} else if (i == data.items.length - 1) {
$("#bar").append("<div id='cell" + i + "' class='sectionStyle sectionRight' style='width:" + sectionWidth + "px;background-color:" + item.color + "'></div>");
} else {
$("#bar").append("<div id='cell" + i + "' class='sectionStyle' style='width:" + sectionWidth + "px;background-color:" + item.color + "'></div>");
}
}
$("#cell" + i).click(function() {
alert("Your have selected " + item.label + ", value: " + item.value + ", percentage:" + percent);
});
});
});
});