Last active
January 2, 2016 12:49
-
-
Save timelyportfolio/8305586 to your computer and use it in GitHub Desktop.
rCharts addFilter with multiselect
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
#example how to use the new addFilters with angular and lodash | |
#require(devtools) | |
#install("rCharts","ramnathv",ref="dev") | |
require(rCharts) | |
n1 <- nPlot( | |
Freq ~ Sex, | |
group = "Eye", | |
data = as.data.frame(HairEyeColor), | |
type = "multiBarChart" | |
) | |
n1$addFilters("Hair","Eye") | |
n1$templates$script = "http://timelyportfolio.github.io/rCharts_nvd3_templates/script_multiselect.html" | |
n1 |
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://cdnjs.cloudflare.com/ajax/libs/nvd3/1.1.14-beta/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://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script> | |
<style> | |
.rChart { | |
display: block | |
margin: auto auto; | |
width: 100%; | |
height: 400px; | |
} | |
/* | |
body { | |
margin-top: 60px; | |
} | |
*/ | |
</style> | |
</head> | |
<body ng-app> | |
<div class='container' ng-controller="DemoCtrl"> | |
<div class='row'> | |
<div class='col-md-3'> | |
<form class='well'> | |
<select | |
ng-model="selected" | |
ng-options="c as c.value group by c.variable for c in filters" | |
class="form-control" multiple size="10"> | |
</select> | |
</form> | |
</div> | |
<div class='col-md-8'> | |
<div class="bs-docs-example"> | |
<div id='chart26a8482f1ed8' class='rChart '> | |
<svg></svg> | |
</div> | |
</div> | |
</div> | |
</div> | |
<div class= 'row'> | |
<pre><code class='r'>#example how to use the new addFilters with angular and lodash | |
#require(devtools) | |
#install("rCharts","ramnathv",ref="dev") | |
require(rCharts) | |
n1 <- nPlot( | |
Freq ~ Sex, | |
group = "Eye", | |
data = as.data.frame(HairEyeColor), | |
type = "multiBarChart" | |
) | |
n1$addFilters("Hair","Eye") | |
n1$templates$script = "http://timelyportfolio.github.io/rCharts_nvd3_templates/script_multiselect.html" | |
n1</code></pre> | |
</div> | |
</div> | |
<script type="text/javascript"> | |
function DemoCtrl($scope){ | |
$scope.opts = { | |
"dom": "chart26a8482f1ed8", | |
"width": 700, | |
"height": 400, | |
"x": "Sex", | |
"y": "Freq", | |
"group": "Eye", | |
"type": "multiBarChart", | |
"id": "chart26a8482f1ed8" | |
} | |
$scope.data = [ | |
{ | |
"Hair": "Black", | |
"Eye": "Brown", | |
"Sex": "Male", | |
"Freq": 32 | |
}, | |
{ | |
"Hair": "Brown", | |
"Eye": "Brown", | |
"Sex": "Male", | |
"Freq": 53 | |
}, | |
{ | |
"Hair": "Red", | |
"Eye": "Brown", | |
"Sex": "Male", | |
"Freq": 10 | |
}, | |
{ | |
"Hair": "Blond", | |
"Eye": "Brown", | |
"Sex": "Male", | |
"Freq": 3 | |
}, | |
{ | |
"Hair": "Black", | |
"Eye": "Blue", | |
"Sex": "Male", | |
"Freq": 11 | |
}, | |
{ | |
"Hair": "Brown", | |
"Eye": "Blue", | |
"Sex": "Male", | |
"Freq": 50 | |
}, | |
{ | |
"Hair": "Red", | |
"Eye": "Blue", | |
"Sex": "Male", | |
"Freq": 10 | |
}, | |
{ | |
"Hair": "Blond", | |
"Eye": "Blue", | |
"Sex": "Male", | |
"Freq": 30 | |
}, | |
{ | |
"Hair": "Black", | |
"Eye": "Hazel", | |
"Sex": "Male", | |
"Freq": 10 | |
}, | |
{ | |
"Hair": "Brown", | |
"Eye": "Hazel", | |
"Sex": "Male", | |
"Freq": 25 | |
}, | |
{ | |
"Hair": "Red", | |
"Eye": "Hazel", | |
"Sex": "Male", | |
"Freq": 7 | |
}, | |
{ | |
"Hair": "Blond", | |
"Eye": "Hazel", | |
"Sex": "Male", | |
"Freq": 5 | |
}, | |
{ | |
"Hair": "Black", | |
"Eye": "Green", | |
"Sex": "Male", | |
"Freq": 3 | |
}, | |
{ | |
"Hair": "Brown", | |
"Eye": "Green", | |
"Sex": "Male", | |
"Freq": 15 | |
}, | |
{ | |
"Hair": "Red", | |
"Eye": "Green", | |
"Sex": "Male", | |
"Freq": 7 | |
}, | |
{ | |
"Hair": "Blond", | |
"Eye": "Green", | |
"Sex": "Male", | |
"Freq": 8 | |
}, | |
{ | |
"Hair": "Black", | |
"Eye": "Brown", | |
"Sex": "Female", | |
"Freq": 36 | |
}, | |
{ | |
"Hair": "Brown", | |
"Eye": "Brown", | |
"Sex": "Female", | |
"Freq": 66 | |
}, | |
{ | |
"Hair": "Red", | |
"Eye": "Brown", | |
"Sex": "Female", | |
"Freq": 16 | |
}, | |
{ | |
"Hair": "Blond", | |
"Eye": "Brown", | |
"Sex": "Female", | |
"Freq": 4 | |
}, | |
{ | |
"Hair": "Black", | |
"Eye": "Blue", | |
"Sex": "Female", | |
"Freq": 9 | |
}, | |
{ | |
"Hair": "Brown", | |
"Eye": "Blue", | |
"Sex": "Female", | |
"Freq": 34 | |
}, | |
{ | |
"Hair": "Red", | |
"Eye": "Blue", | |
"Sex": "Female", | |
"Freq": 7 | |
}, | |
{ | |
"Hair": "Blond", | |
"Eye": "Blue", | |
"Sex": "Female", | |
"Freq": 64 | |
}, | |
{ | |
"Hair": "Black", | |
"Eye": "Hazel", | |
"Sex": "Female", | |
"Freq": 5 | |
}, | |
{ | |
"Hair": "Brown", | |
"Eye": "Hazel", | |
"Sex": "Female", | |
"Freq": 29 | |
}, | |
{ | |
"Hair": "Red", | |
"Eye": "Hazel", | |
"Sex": "Female", | |
"Freq": 7 | |
}, | |
{ | |
"Hair": "Blond", | |
"Eye": "Hazel", | |
"Sex": "Female", | |
"Freq": 5 | |
}, | |
{ | |
"Hair": "Black", | |
"Eye": "Green", | |
"Sex": "Female", | |
"Freq": 2 | |
}, | |
{ | |
"Hair": "Brown", | |
"Eye": "Green", | |
"Sex": "Female", | |
"Freq": 14 | |
}, | |
{ | |
"Hair": "Red", | |
"Eye": "Green", | |
"Sex": "Female", | |
"Freq": 7 | |
}, | |
{ | |
"Hair": "Blond", | |
"Eye": "Green", | |
"Sex": "Female", | |
"Freq": 8 | |
} | |
] | |
$scope.controls = [] | |
$scope.filters = [ | |
{ | |
"variable": "Hair", | |
"value": "Black" | |
}, | |
{ | |
"variable": "Hair", | |
"value": "Brown" | |
}, | |
{ | |
"variable": "Hair", | |
"value": "Red" | |
}, | |
{ | |
"variable": "Hair", | |
"value": "Blond" | |
}, | |
{ | |
"variable": "Eye", | |
"value": "Brown" | |
}, | |
{ | |
"variable": "Eye", | |
"value": "Blue" | |
}, | |
{ | |
"variable": "Eye", | |
"value": "Hazel" | |
}, | |
{ | |
"variable": "Eye", | |
"value": "Green" | |
} | |
] | |
$scope.drawChart = function(){ | |
drawChart($scope.opts, $scope.data) | |
} | |
$scope.$watch('selected', function(){ | |
if (!(typeof($scope.selected) === "undefined")) { | |
$scope.opts.selected = $scope.selected.map(function(d){ | |
selectKey={}; | |
selectKey[d.variable]=d.value; | |
return selectKey; | |
}) | |
} | |
}) | |
$scope.$watch('opts',function(){ | |
$scope.drawChart() | |
}, true) | |
} | |
function drawChart(opts, data){ | |
if (_.keys(opts.selected).length > 0){ | |
// for multiselect union | |
data = _.at(data,_.chain(opts.selected) | |
.map(function(d){ | |
return _.compact( | |
data.map(function(val,ind){ | |
if(val[_.keys(d)[0]]===d[_.keys(d)[0]]) { | |
return ind | |
} else {return false} | |
}) | |
) | |
}) | |
.flatten() | |
.uniq() | |
.value() | |
) | |
/* for multiselect intersection | |
data = _.chain(opts.selected) | |
.map(function(d){ | |
return _.compact( | |
data.map(function(val,ind){ | |
if(val[_.keys(d)[0]]===d[_.keys(d)[0]]) { | |
return ind | |
} else {return false} | |
}) | |
) | |
}) | |
.flatten() | |
.value(); | |
intersected = _.filter(_.countBy(testdata,function(d){return d}),opts.selected.length); | |
*/ | |
} | |
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) | |
d3.select("#" + opts.id + ' svg') | |
// .empty() | |
.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