This demo shows how to customize the legend in a morrisjs plot, created using rCharts. If you hover over the line, you will observe that the order of the variables traj1
and traj2
in the legend switches based on which value is greater. This demo was created in response to a question posed on github.
Last active
August 29, 2015 13:57
-
-
Save ramnathv/9474140 to your computer and use it in GitHub Desktop.
Morris Plot with Dynamically Sorted 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
month = 0:10 | |
dat <- data.frame( | |
month = month, | |
traj1 = rnorm(11), | |
traj2 = rnorm(11) | |
) | |
m1 <- mPlot(x = 'month', y = c('traj1', 'traj2'), data = dat, type = 'Line') | |
m1$set(parseTime = FALSE) | |
m1$set(hoverCallback = "#! function(index, options, content){ | |
var row = options.data[index] | |
month = 'Month: ' + row.month | |
line1 = 'Traj1: ' + row.traj1 | |
line2 = 'Traj2: ' + row.traj2 | |
if (row.traj1 < row.traj2){ | |
tmp = line1; line1 = line2; line2 = tmp; | |
} | |
return [month, line1, line2].join('<br/>') | |
} !#") | |
m1 |
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> | |
<meta charset = 'utf-8'> | |
<html> | |
<head> | |
<link rel='stylesheet' href="http://netdna.bootstrapcdn.com/bootswatch/2.3.1/cosmo/bootstrap.min.css"> | |
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css" > | |
<link rel='stylesheet' href="http://getbootstrap.com/2.3.2/assets/js/google-code-prettify/prettify.css"> | |
<link rel='stylesheet' href="http://aozora.github.io/bootplus/assets/css/docs.css"> | |
<link rel='stylesheet' href='http://cdn.oesmith.co.uk/morris-0.4.2.min.css'> | |
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js' type='text/javascript'></script> | |
<script src='http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js' type='text/javascript'></script> | |
<script src='http://cdn.oesmith.co.uk/morris-0.4.2.min.js' type='text/javascript'></script> | |
<style> | |
.rChart { | |
display: block | |
margin: auto auto; | |
width: 100%; | |
height: 400px; | |
} | |
/* | |
body { | |
margin-top: 60px; | |
} | |
*/ | |
</style> | |
</head> | |
<body> | |
<div class='container'> | |
<div class='row'> | |
<div class='span8'> | |
<div class="bs-docs-example"> | |
<div id='chart15961f0c408' class='rChart morris'> | |
</div> | |
<br/> | |
<pre><code class='r'>month = 0:10 | |
dat <- data.frame( | |
month = month, | |
traj1 = rnorm(11), | |
traj2 = rnorm(11) | |
) | |
m1 <- mPlot(x = 'month', y = c('traj1', 'traj2'), data = dat, type = 'Line') | |
m1$set(parseTime = FALSE) | |
m1$set(hoverCallback = "#! function(index, options, content){ | |
var row = options.data[index] | |
month = 'Month: ' + row.month | |
line1 = 'Traj1: ' + row.traj1 | |
line2 = 'Traj2: ' + row.traj2 | |
if (row.traj1 < row.traj2){ | |
tmp = line1; line1 = line2; line2 = tmp; | |
} | |
return [month, line1, line2].join('<br/>') | |
} !#") | |
m1 | |
</code></pre> | |
</div> | |
</div> | |
</div> | |
</div> | |
<script type='text/javascript'> | |
var chartParams = { | |
"element": "chart15961f0c408", | |
"width": 700, | |
"height": 400, | |
"xkey": "month", | |
"ykeys": [ | |
"traj1", | |
"traj2" | |
], | |
"data": [ | |
{ | |
"month": 0, | |
"traj1": -1.043545070837, | |
"traj2": -0.3738552629869 | |
}, | |
{ | |
"month": 1, | |
"traj1": 1.178902021019, | |
"traj2": -0.02622395872875 | |
}, | |
{ | |
"month": 2, | |
"traj1": 1.418269738111, | |
"traj2": 1.789230918333 | |
}, | |
{ | |
"month": 3, | |
"traj1": -1.718234157295, | |
"traj2": -2.046378574553 | |
}, | |
{ | |
"month": 4, | |
"traj1": 1.471426259781, | |
"traj2": 2.246902862323 | |
}, | |
{ | |
"month": 5, | |
"traj1": 1.196944295267, | |
"traj2": 1.845120537351 | |
}, | |
{ | |
"month": 6, | |
"traj1": -1.504747109399, | |
"traj2": -0.7289058610983 | |
}, | |
{ | |
"month": 7, | |
"traj1": -1.432637917079, | |
"traj2": -0.7096412934887 | |
}, | |
{ | |
"month": 8, | |
"traj1": 1.392273894799, | |
"traj2": -1.300984562468 | |
}, | |
{ | |
"month": 9, | |
"traj1": -0.4115259684652, | |
"traj2": -2.095647356595 | |
}, | |
{ | |
"month": 10, | |
"traj1": -0.04985627691913, | |
"traj2": 0.5969627708814 | |
} | |
], | |
"parseTime": false, | |
"hoverCallback": function(index, options, content){ | |
var row = options.data[index] | |
month = 'Month: ' + row.month | |
line1 = 'Traj1: ' + row.traj1 | |
line2 = 'Traj2: ' + row.traj2 | |
if (row.traj1 < row.traj2){ | |
tmp = line1; line1 = line2; line2 = tmp; | |
} | |
return [month, line1, line2].join('<br/>') | |
} , | |
"id": "chart15961f0c408", | |
"labels": [ "traj1", "traj2" ] | |
}, | |
chartType = "Line" | |
new Morris[chartType](chartParams) | |
</script> | |
</body> | |
<!-- Google Prettify --> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/prettify/188.0.0/prettify.js"></script> | |
<script | |
src='https://google-code-prettify.googlecode.com/svn-history/r232/trunk/src/lang-r.js'> | |
</script> | |
<script> | |
var pres = document.getElementsByTagName("pre"); | |
for (var i=0; i < pres.length; ++i) { | |
pres[i].className = "prettyprint linenums"; | |
} | |
prettyPrint(); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment