Advertisement
Guest User

es + d3 + nvd3

a guest
Apr 7th, 2014
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.     requirejs.config({
  3.         paths: {
  4.             d3: "scripts/d3.v3",
  5.             nvd3: "scripts/nv.d3",
  6.             elasticsearch: "scripts/elasticsearch"
  7.         },
  8.         shim: {
  9.             nvd3: {
  10.               exports: 'nv',
  11.               deps: ['d3.global']
  12.             }
  13.         }
  14.     });
  15.     // workaround for nvd3 using global d3
  16.     define("d3.global", ["d3"], function(_) {
  17.       d3 = _;
  18.     });
  19.     require(["scripts/main"], function () {});
  20. </script>
  21.  
  22. //main.js:
  23. define(['d3', 'nvd3', 'elasticsearch'], function (d3, nv, elasticsearch) {
  24.     var client = new elasticsearch.Client();
  25.     client.search({
  26.         ...
  27.     }).then(function (resp) {
  28.         var data = resp.aggregations.someaggregationthatyoumade.buckets;
  29.         nv.addGraph(function() {
  30.             var chart = nv.models.pieChart()
  31.                 .x(function(d) { return d.key; })
  32.                 .y(function(d) { return d.valueintheaggregation; })
  33.                 .showLabels(true);
  34.             d3.select("#thechart svg")
  35.                 .datum(data)
  36.                 .call(chart);
  37.             return chart;
  38.         });
  39.     });
  40. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement