Skip to content

How Do I...

iros edited this page Apr 26, 2013 · 22 revisions

Not sure how to do something in Dataset? Maybe others have come across your issue before! This page will aggregate various tips and tricks we come across over time.

Questions:


# How do I bin a dataset based on a time interval (such as a week or month)?

jsFiddle of this code: http://jsfiddle.net/iros/9PHbV/

Ever had a dataset that had very high precision in it's time column? If your data includes timestamps, for example, binning it into days might be tricky. Here's how you could do it.

Let's say this is your data:

var data = [
  { timestamp : 'Sep 03 2012 16:05:15 GMT+0200', category: 'A', value : 12 },
  { timestamp : 'Sep 05 2012 12:25:54 GMT+0200', category: 'A', value : 100 },
  { timestamp : 'Sep 06 2012 16:35:25 GMT+0200', category: 'A', value : 3 },
  { timestamp : 'Sep 06 2012 15:45:53 GMT+0200', category: 'B', value : 12 },
  { timestamp : 'Sep 07 2012 16:11:54 GMT+0200', category: 'B', value : 42 },
  { timestamp : 'Sep 07 2012 19:22:15 GMT+0200', category: 'A', value : 203 },
  { timestamp : 'Sep 07 2012 02:33:05 GMT+0200', category: 'A', value : 13 }
];

You create a new dataset with the proper column definition for the timestamp column:

var ds = new Miso.Dataset({
  data : data,
  columns : [
    { 
      name : 'timestamp', 
      format : "MMM DD YYYY HH:mm:ss z", 
      type : 'time' 
    }
  ]
});

Once your data is actually fetched, you can go ahead and group it in the following way:

ds.fetch().then(function() {
  var byDay = ds.groupBy('timestamp', ['value'], {
    preprocess : function(timestamp) {
      // first extract just the day - this is the meat of the operation!
      var justDay = timestamp.format("MMM DD YYYY");

      // then return it as a moment object because the timestamp column
      // is a column after all.
      return moment(justDay, "MMM DD YYYY");
    }
  });
});

Your data now looks like this:

[
  {
    "_id": 15,
    "_oids": [4],
    "timestamp": {
      "_d": "2012-09-02T22:00:00.000Z",
      "_isUTC": false
    },
    "value": 12
  },
  {
    "_id": 16,
    "_oids": [5],
    "timestamp": {
      "_d": "2012-09-04T22:00:00.000Z",
      "_isUTC": false
    },
    "value": 100
  },
  {
    "_id": 17,
    "_oids": [6,7],
    "timestamp": {
      "_d": "2012-09-05T22:00:00.000Z",
      "_isUTC": false
    },
    "value": 15
  },
  {
    "_id": 18,
    "_oids": [8,9,10],
    "timestamp": {
      "_d": "2012-09-06T22:00:00.000Z",
      "_isUTC": false
    },
    "value": 258
  }
]

# Can I use Dataset in a Chrome Extension?

The current issue with including a miso build is that one of it's dependencies (lodash) uses Function(...), to build methods optimized for the current environment. To avoid this issue, look into building a mobile version of lodash and including that as a dependency. The reason we are not doing this by default is that it no longer includes some IE fixes that we believe need to exist in the core builds.

See this ticket for more info: #158

# How do I use Miso Dataset with Require.js?

First, you will need one of the builds that include AMD compatibility. They are the ones that have the letter 'm' in the file name.

Second, you will need to set up a shim in your config.js to include all dependencies. Here's a sample config.js file:

// Set the require.js configuration for your application.
require.config({

  paths: {
    // JavaScript folders.
    libs: "../assets/js/libs",

    // Libraries.
    'lodash': "../assets/js/libs/lodash",
    'moment': "../assets/js/libs/moment",
    'underscore.deferred': "../assets/js/libs/underscore.deferred",
    'underscore.math': "../assets/js/libs/underscore.math",
    'backbone': "../assets/js/libs/backbone",
    'miso.ds': "../assets/js/libs/miso.ds.0.4.0.m"
  },

  shim: {

    'underscore.deferred' : {
      deps : ["lodash"],
      exports : "_"
    },

    'underscore.math' : {
      deps : ["lodash"],
      exports : "_"
    },

    'miso.ds' : {
      deps: ["lodash", "moment", "underscore.deferred", "underscore.math"],
      exports: "Miso"
    }
  }
});

You are now ready to simply require miso.ds as a requirement in your modules.

# How do I prevent IE8 from caching my data between requests?

This issue arose in ticket #195.

When requesting a data url, if for any reason your browser (in this case IE8,) are not fetching updated data from the server, you can invalidate the cache by appending a random parameter to your url like so:

var ds = new Miso.Dataset({
  url: function(){ 
    return 'data.csv?'+ Math.random(); 
  },
  delimiter: ','
});