This is a simple gist to attempt to answer rCharts Issue #481 which might also help documentation-starved nvd3
users who might like to reverse the x
axis.
Last active
August 29, 2015 14:04
-
-
Save timelyportfolio/3e1bd582a0d48a0cbcd7 to your computer and use it in GitHub Desktop.
nvd3 + rCharts : reverse scale domain for x axis
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
require(rCharts) | |
dt <- data.frame(x = 1:10, y = rep(c(5,10), 5), z = rep(c('a', 'b'), 5)) | |
n = nPlot(y ~ x, group = "z", data = dt, type = "stackedAreaChart") | |
n$params$width = 500 | |
n$params$height = 400 | |
# could use R to calculate the domain for the x axis scale | |
n$chart( xDomain = sort(range(dt$x),decreasing=T) ) | |
# or could specify manually like this | |
# n$chart( xDomain = c(10,0) ) | |
n |
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://nvd3.org/assets/css/nv.d3.css'> | |
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'></script> | |
<script src='http://d3js.org/d3.v3.min.js' type='text/javascript'></script> | |
<script src='http://timelyportfolio.github.io/rCharts_nvd3_tests/libraries/widgets/nvd3/js/nv.d3.min-new.js' type='text/javascript'></script> | |
<script src='http://nvd3.org/assets/lib/fisheye.js' type='text/javascript'></script> | |
<style> | |
.rChart { | |
display: block; | |
margin-left: auto; | |
margin-right: auto; | |
width: 500px; | |
height: 400px; | |
} | |
</style> | |
</head> | |
<body > | |
<div id = 'chart131433df6458' class = 'rChart nvd3'></div> | |
<script type='text/javascript'> | |
$(document).ready(function(){ | |
drawchart131433df6458() | |
}); | |
function drawchart131433df6458(){ | |
var opts = { | |
"dom": "chart131433df6458", | |
"width": 500, | |
"height": 400, | |
"x": "x", | |
"y": "y", | |
"group": "z", | |
"type": "stackedAreaChart", | |
"id": "chart131433df6458" | |
}, | |
data = [ | |
{ | |
"x": 1, | |
"y": 5, | |
"z": "a" | |
}, | |
{ | |
"x": 2, | |
"y": 10, | |
"z": "b" | |
}, | |
{ | |
"x": 3, | |
"y": 5, | |
"z": "a" | |
}, | |
{ | |
"x": 4, | |
"y": 10, | |
"z": "b" | |
}, | |
{ | |
"x": 5, | |
"y": 5, | |
"z": "a" | |
}, | |
{ | |
"x": 6, | |
"y": 10, | |
"z": "b" | |
}, | |
{ | |
"x": 7, | |
"y": 5, | |
"z": "a" | |
}, | |
{ | |
"x": 8, | |
"y": 10, | |
"z": "b" | |
}, | |
{ | |
"x": 9, | |
"y": 5, | |
"z": "a" | |
}, | |
{ | |
"x": 10, | |
"y": 10, | |
"z": "b" | |
} | |
] | |
if(!(opts.type==="pieChart" || opts.type==="sparklinePlus" || opts.type==="bulletChart")) { | |
var data = d3.nest() | |
.key(function(d){ | |
//return opts.group === undefined ? 'main' : d[opts.group] | |
//instead of main would think a better default is opts.x | |
return opts.group === undefined ? opts.y : d[opts.group]; | |
}) | |
.entries(data); | |
} | |
if (opts.disabled != undefined){ | |
data.map(function(d, i){ | |
d.disabled = opts.disabled[i] | |
}) | |
} | |
nv.addGraph(function() { | |
var chart = nv.models[opts.type]() | |
.width(opts.width) | |
.height(opts.height) | |
if (opts.type != "bulletChart"){ | |
chart | |
.x(function(d) { return d[opts.x] }) | |
.y(function(d) { return d[opts.y] }) | |
} | |
chart | |
.xDomain([ 10, 1 ]) | |
d3.select("#" + opts.id) | |
.append('svg') | |
.datum(data) | |
.transition().duration(500) | |
.call(chart); | |
nv.utils.windowResize(chart.update); | |
return chart; | |
}); | |
}; | |
</script> | |
<script></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment