-
Notifications
You must be signed in to change notification settings - Fork 12
devguide
In modules/loadDataSource.js
we have functions that allow us to obtain data from different data sources. We can extend it add functionality to support newer data sources. The function must have 4 parts:
- Specification of the path of the data source.
- Reading from data source(file or REST API).
- Convert data to JSON.
- Send data back to the application.
- Invoke the callback.
#!javascript
function newSource(options, callback){
// 1 Specify the path of the data source.
var path = options.path;
// 2 Read the file using filepath or make HTTP request
fs.readFile(path, 'utf8', function(err, d){
if(err){
console.log("Error: "+ err);
return;
}
// 3 Convert data into JSON
data = JSON.parse(d);
// 4 Send data back to app.js
exports.data = data;
// 5 Invoke callback
callback();
});
}
Support for new visualizations can be done using these 3 steps:
-
Write functions for rendering visualization The
public/javascripts/visualizations.js
specifies functions for rendering various visualizations. Every visualization has aninit
function(Ex:renderImageGridInit()
etc) and anotherrender function )=(Ex:
renderImageGrid()``` etc.) that gets called up on every filtering request. -
Modify app.js to encapsulate visualization information in results object The information required by the client is provided by the server
app.js
. To embed information in theresults
object which is provided by the server and is used by server to know about the current state of filtering. Most visualizations use thevisualization
attribute in theresults
object. For example theimageGrid
if(visualization.type == "imageGrid") results["visualization"] = {values:(dimensions["visualization"].top(100))}
-
Register the rendering functions Now in
public/javascripts/main.js
we haverenderVisualisation()
andrenderVisualizationInit()
where you need to register your visualization'srender()
andrenderInit()
functions.
Have a look at at the [https://github.com/dc-js](dc.js documentation) for creating new dc.js objects. We create fake dimension and fake group to work with dc.js. The actual crossfilter dimensions and groups are on the server. Following is a sample definition for the dimensions and groups we use on the client side:
#!javascript
dimension = function() {
return {
//Called on each filter request on this dimension
filter: function(f) {
//makes an ajax request to the server
refresh(queryFilter, visualAttributes)
},
//Called while removing a filter from this dimension
filterAll: function() {
delete queryFilter[dim];
refresh(queryFilter, visualAttributes);
},
name: function(){
return dim;
}
}
}();
group = function() {
return {
//Returns statistics used by dc.js for plotting charts.
all: function() {
return filteredData[dim].values;
},
order: function() {
return groups[dim];
},
top: function() {
return filteredData[dim].values;
}
}
}();