Here, we create a donut-chart directive using:
For instructions on getting started with Elasticsearch, please visit the overview page.
Download and unzip the NFL dataset. To load the data into Elasticsearch, open the terminal, change into the NFL dataset directory, and run:
curl -XPOST localhost:9200/nfl?pretty
curl -XPUT localhost:9200/nfl/2013/_mapping?pretty -d @mappings/nfl_mapping.json
curl -XPOST localhost:9200/nfl/2013/_bulk?pretty --data-binary @datasets/nfl_2013.json
To install Yeoman, you will need to have node and npm installed. If you haven't installed node or npm, this link is a great place to get started.
npm install -g yo
If you are using npm 1.2.10 or above, this will also automatically install grunt and bower for you. If you're on an older version of npm, you will need to install them manually:
npm install -g grunt-cli bower
If you have installed Grunt globally in the past, you will need to remove it first:
npm uninstall -g grunt
Finally, install the angular generator:
npm install -g generator-angular
After that, create a new directory for your application, then run:
yo angular
Here, we are first generating the file structure for a basic web application and then writing a number of boilerplate files for a new AngularJS application on top of it. This includes boilerplate directives and controllers as well as scaffolded Karma unit tests.
Let's install the dependencies for our app using bower:
bower install d3
bower install elasticsearch
You should now have d3.js and elasticsearch.js installed. You can check within the bower_components directory to verify or look for the script tag in the index.html file.
Now, we are ready to create a connection to our Elasticsearch server, which will be on our localhost on port 9200.
**Note: For the sake of simplicity, we have included both d3.js and elasticsearch.js in our global namespace.
Lets create our Angular service using our angular generator:
yo angular:service ejs
Now, simply modify the file according to the ejs.js file below.
Next, let's add a controller to our app:
yo angular:controller pie
Now, simply modify the file according to the pie.js file below.
We could add a controller to our directive explicitly, but I like to keep my controllers flexible and not bound to one specific directive.
The last major hurdle is to create our Angular directive with our D3 code. Let's create our directive:
yo angular:directive piechart
Now, modify the file according to the piechart.js file below.
The magic here is in $watch. The scope watches for changes to the data, and then re-renders the chart.
Let's wrap it up with some final changes. Open your app.js file and modify it according to the app.js file below. Finally, add a pie.hmtl file exactly like the one below.
Now, serve your app by running:
grunt server
You should now see a donut chart in your browser window, and you should be able to change the values of the chart by modifying the values in the text boxes. Give it a try!
Congrats! You've just created an Angular app with a simple D3 directive and Elasticsearch as your backend data store.