Created
May 5, 2016 22:53
-
-
Save kitingChris/0d0e1c0897093c6f039aa906dd1b2fd3 to your computer and use it in GitHub Desktop.
Morris Donut with legend
This file contains 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
<!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("■ ").addClass('colorDot').css('color', legendColor); | |
$(legendItem).prepend(legendColorDot) | |
$('#legend').append(legendItem); | |
}); | |
</script> | |
</body> | |
</html> |
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
I use the latest version of Morris (0.6.9 (3rd July 2021)) and
segment.data
doesn't work. I fixed it to replace bysegment.options.data[index]
and I get index by the foreach loop :chart.segments.forEach(function(segment, index){})