Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created October 1, 2013 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/6773415 to your computer and use it in GitHub Desktop.
Save timelyportfolio/6773415 to your computer and use it in GitHub Desktop.
require(rCharts)
df <- data.frame(var = 1:10, x=(1:10)/100)
dP <- dPlot( x ~ x, groups = "var", data=df, type = "bubble")
dP$xAxis(
type = "addMeasureAxis",
outputFormat = ".2%"
)
dP$yAxis(
type = "addMeasureAxis", #y axis measure by default but do anyways
outputFormat = ".2f" #do fixed 2 decimal just as demo
)
dP
<!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">
<script src='http://d3js.org/d3.v3.min.js' type='text/javascript'></script>
<script src='http://dimplejs.org/dist/dimple.v1.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='chart2340185a3abf' class='rChart nvd3Plot dimple'></div>
<br/>
<pre><code class='r'>require(rCharts)
df &lt;- data.frame(var = 1:10, x=(1:10)/100)
dP &lt;- dPlot( x ~ x, groups = &quot;var&quot;, data=df, type = &quot;bubble&quot;)
dP$xAxis(
type = &quot;addMeasureAxis&quot;,
outputFormat = &quot;.2%&quot;
)
dP$yAxis(
type = &quot;addMeasureAxis&quot;, #y axis measure by default but do anyways
outputFormat = &quot;.2f&quot; #do fixed 2 decimal just as demo
)
dP
</code></pre>
</div>
</div>
</div>
</div>
<script type="text/javascript">
(function(){
var opts = {
"dom": "chart2340185a3abf",
"width": 700,
"height": 400,
"x": "x",
"y": "x",
"groups": "var",
"type": "bubble",
"id": "chart2340185a3abf"
},
data = [{"var":1,"x":0.01},{"var":2,"x":0.02},{"var":3,"x":0.03},{"var":4,"x":0.04},{"var":5,"x":0.05},{"var":6,"x":0.06},{"var":7,"x":0.07},{"var":8,"x":0.08},{"var":9,"x":0.09},{"var":10,"x":0.1}],
xAxis = {
"type": "addMeasureAxis",
"showPercent": false,
"outputFormat": ".2%"
},
yAxis = {
"type": "addMeasureAxis",
"showPercent": false,
"outputFormat": ".2f"
},
zAxis = [],
colorAxis = [],
defaultColors = [],
legend = [];
var svg = dimple.newSvg("#" + opts.id, opts.width, opts.height);
//data = dimple.filterData(data, "Owner", ["Aperture", "Black Mesa"])
var myChart = new dimple.chart(svg, data);
if (opts.bounds) {
myChart.setBounds(opts.bounds.x, opts.bounds.y, opts.bounds.width, opts.bounds.height);//myChart.setBounds(80, 30, 480, 330);
}
//dimple allows use of custom CSS with noFormats
if(opts.noFormats) { myChart.noFormats = opts.noFormats; };
//for markimekko and addAxis also have third parameter measure
//so need to evaluate if measure provided
//x axis
var x;
if(xAxis.measure) {
x = myChart[xAxis.type]("x",opts.x,xAxis.measure);
} else {
x = myChart[xAxis.type]("x", opts.x);
};
if(!(xAxis.type === "addPctAxis")) x.showPercent = xAxis.showPercent;
if (xAxis.orderRule) x.addOrderRule(xAxis.orderRule);
if (xAxis.grouporderRule) x.addGroupOrderRule(xAxis.grouporderRule);
if (xAxis.overrideMin) x.overrideMin = xAxis.overrideMin;
if (xAxis.overrideMax) x.overrideMax = xAxis.overrideMax;
if (xAxis.overrideMax) x.overrideMax = xAxis.overrideMax;
if (xAxis.inputFormat) x.dateParseFormat = xAxis.inputFormat;
if (xAxis.outputFormat) x.tickFormat = xAxis.outputFormat;
//y axis
var y;
if(yAxis.measure) {
y = myChart[yAxis.type]("y",opts.y,yAxis.measure);
} else {
y = myChart[yAxis.type]("y", opts.y);
};
if(!(yAxis.type === "addPctAxis")) y.showPercent = yAxis.showPercent;
if (yAxis.orderRule) y.addOrderRule(yAxis.orderRule);
if (yAxis.grouporderRule) y.addGroupOrderRule(yAxis.grouporderRule);
if (yAxis.overrideMin) y.overrideMin = yAxis.overrideMin;
if (yAxis.overrideMax) y.overrideMax = yAxis.overrideMax;
if (yAxis.inputFormat) y.dateParseFormat = yAxis.inputFormat;
if (yAxis.outputFormat) y.tickFormat = yAxis.outputFormat;
//z for bubbles
var z;
if (!(typeof(zAxis) === 'undefined') && zAxis.type){
if(zAxis.measure) {
z = myChart[zAxis.type]("z",opts.z,zAxis.measure);
} else {
z = myChart[zAxis.type]("z", opts.z);
};
if(!(zAxis.type === "addPctAxis")) z.showPercent = zAxis.showPercent;
if (zAxis.orderRule) z.addOrderRule(zAxis.orderRule);
if (zAxis.overrideMin) z.overrideMin = zAxis.overrideMin;
if (zAxis.overrideMax) z.overrideMax = zAxis.overrideMax;
}
if(d3.keys(colorAxis).length > 0) {
myChart[colorAxis.type](colorAxis.colorSeries,colorAxis.palette) ;
}
//allow manipulation of default colors to use with dimple
if(defaultColors.length) {
defaultColors = defaultColors[0];
if (typeof(defaultColors) == "function") {
//assume this is a d3 scale
//for now loop through first 20 but need a better way to handle
defaultColorsArray = [];
for (var n=0;n<20;n++) {
defaultColorsArray.push(defaultColors(n));
};
defaultColors = defaultColorsArray;
}
defaultColors.forEach(function(d,i) {
defaultColors[i] = new dimple.color(d);
})
myChart.defaultColors = defaultColors;
}
//here need think I need to evaluate group and if missing do null
//as the first argument
//if provided need to use groups from opts
if(opts.hasOwnProperty("groups")) {
var s = myChart.addSeries( opts.groups, dimple.plot[opts.type] );
//series offers an aggregate method that we will also need to check if available
//options available are avg, count, max, min, sum
if (!(typeof(opts.aggregate) === 'undefined')) {
s.aggregate = eval(opts.aggregate);
}
if (!(typeof(opts.lineWeight) === 'undefined')) {
s.lineWeight = eval(opts.lineWeight);
}
if (!(typeof(opts.barGap) === 'undefined')) {
s.barGap = eval(opts.barGap);
}
} else var s = myChart.addSeries( null, dimple.plot[opts.type] );
//unsure if this is best but if legend is provided (not empty) then evaluate
if(d3.keys(legend).length > 0) {
var l =myChart.addLegend();
d3.keys(legend).forEach(function(d){
l[d] = legend[d];
});
}
//quick way to get this going but need to make this cleaner
if(opts.storyboard) {
myChart.setStoryboard(opts.storyboard);
};
//catch all for other options
//these can be provided by dMyChart$chart( ... )
myChart.draw();
})();
</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