Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created September 29, 2013 16:58
Show Gist options
  • Save ramnathv/6754287 to your computer and use it in GitHub Desktop.
Save ramnathv/6754287 to your computer and use it in GitHub Desktop.
NVD3 Tooltips
dat<- data.frame(
Gene_Name= LETTERS[1:15],
Value1 = sample(-8:20,15,replace=TRUE),
Value2 =sample(-6:10,15,replace=TRUE)
)
library(rCharts)
r1<- nPlot(Value1~Value2,data=dat, type="scatterChart")
r1$chart(tooltipContent = "#! function(key, x, y, e){
return '<b>Gene Name</b>: ' + e.point.Gene_Name
} !#")
r1
<!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://nvd3.org/src/nv.d3.css'>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js' type='text/javascript'></script>
<script src='http://d3js.org/d3.v2.min.js' type='text/javascript'></script>
<script src='http://nvd3.org/nv.d3.js' type='text/javascript'></script>
<script src='http://nvd3.org/lib/fisheye.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='chartb1f9353b6194' class='rChart nvd3'>
</div>
<br/>
<pre><code class='r'>dat&lt;- data.frame(
Gene_Name= LETTERS[1:15],
Value1 = sample(-8:20,15,replace=TRUE),
Value2 =sample(-6:10,15,replace=TRUE)
)
library(rCharts)
r1&lt;- nPlot(Value1~Value2,data=dat, type=&quot;scatterChart&quot;)
r1$chart(tooltipContent = &quot;#! function(key, x, y, e){
return '&lt;b&gt;Gene Name&lt;/b&gt;: ' + e.point.Gene_Name
} !#&quot;)
r1
</code></pre>
</div>
</div>
</div>
</div>
<script type='text/javascript'>
$(document).ready(function(){
drawchartb1f9353b6194()
});
function drawchartb1f9353b6194(){
var opts = {
"dom": "chartb1f9353b6194",
"width": 700,
"height": 400,
"x": "Value2",
"y": "Value1",
"type": "scatterChart",
"id": "chartb1f9353b6194"
},
data = [
{
"Gene_Name": "A",
"Value1": 17,
"Value2": -1
},
{
"Gene_Name": "B",
"Value1": 14,
"Value2": 10
},
{
"Gene_Name": "C",
"Value1": 5,
"Value2": 7
},
{
"Gene_Name": "D",
"Value1": -1,
"Value2": 6
},
{
"Gene_Name": "E",
"Value1": 7,
"Value2": -2
},
{
"Gene_Name": "F",
"Value1": -2,
"Value2": 9
},
{
"Gene_Name": "G",
"Value1": 16,
"Value2": -1
},
{
"Gene_Name": "H",
"Value1": 6,
"Value2": 8
},
{
"Gene_Name": "I",
"Value1": 1,
"Value2": 7
},
{
"Gene_Name": "J",
"Value1": 18,
"Value2": 8
},
{
"Gene_Name": "K",
"Value1": -7,
"Value2": 4
},
{
"Gene_Name": "L",
"Value1": 5,
"Value2": -3
},
{
"Gene_Name": "M",
"Value1": 6,
"Value2": 9
},
{
"Gene_Name": "N",
"Value1": 13,
"Value2": 0
},
{
"Gene_Name": "O",
"Value1": -8,
"Value2": 8
}
]
var data = d3.nest()
.key(function(d){
return opts.group === undefined ? 'main' : d[opts.group]
})
.entries(data)
nv.addGraph(function() {
var chart = nv.models[opts.type]()
.x(function(d) { return d[opts.x] })
.y(function(d) { return d[opts.y] })
.width(opts.width)
.height(opts.height)
chart
.tooltipContent( function(key, x, y, e){
return '<b>Gene Name</b>: ' + e.point.Gene_Name
} )
d3.select("#" + opts.id)
.append('svg')
.datum(data)
.transition().duration(500)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
};
</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