Skip to content

Instantly share code, notes, and snippets.

@kitingChris
Created May 5, 2016 22:53
Show Gist options
  • Save kitingChris/0d0e1c0897093c6f039aa906dd1b2fd3 to your computer and use it in GitHub Desktop.
Save kitingChris/0d0e1c0897093c6f039aa906dd1b2fd3 to your computer and use it in GitHub Desktop.
Morris Donut with legend
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2016 by anonymous (http://jsbin.com/lucemiware/3/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta name="description" content="Morris.Donut with legend">
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.4.1.min.js"></script>
<meta charset=utf-8 />
<title>Morris.js Donut Chart Example</title>
<style id="jsbin-css">
#donut-example {
float: right;
}
ul#legend {
float: right;
list-style-type: none;
}
span.colorDot {
font-size: 20pt;
}
</style>
</head>
<body>
<ul id="legend"></ul>
<div id="donut-example"></div>
<script id="jsbin-javascript">
var chart = Morris.Donut({
element: 'donut-example',
data: [
{label: "Download Sales", value: 12},
{label: "In-Store Sales", value: 30},
{label: "Mail-Order Sales", value: 20}
]
});
chart.segments.forEach(function(segment){
console.log(segment.color,segment.data);
var legendText = segment.data.label+" ("+segment.data.value+")";
var legendColor = segment.color;
var legendItem = $('<li></li>').text(legendText).css('color', 'black');
var legendColorDot = $('<span></span>').html("&#9632; ").addClass('colorDot').css('color', legendColor);
$(legendItem).prepend(legendColorDot)
$('#legend').append(legendItem);
});
</script>
</body>
</html>
@LoanCB
Copy link

LoanCB commented Jul 12, 2022

I use the latest version of Morris (0.6.9 (3rd July 2021)) and segment.data doesn't work. I fixed it to replace by segment.options.data[index] and I get index by the foreach loop : chart.segments.forEach(function(segment, index){})

@kitingChris
Copy link
Author

This gist is 6 years old. Totally outdated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment