Skip to content

Instantly share code, notes, and snippets.

@merlinstardust
Created August 9, 2017 21:53
Show Gist options
  • Save merlinstardust/9925bbac94b0c91f1430c2de1569bb26 to your computer and use it in GitHub Desktop.
Save merlinstardust/9925bbac94b0c91f1430c2de1569bb26 to your computer and use it in GitHub Desktop.
Uses of Moment in Chart.js
docs/00-Getting-Started.md:Chart.js provides two different builds that are available for your use. The `Chart.js` and `Chart.min.js` files include Chart.js and the accompanying color parsing library. If this version is used and you require the use of the time axis, [Moment.js](http://momentjs.com/) will need to be included before Chart.js.
docs/02-Scales.md:parser | String or Function | - | If defined as a string, it is interpreted as a custom format to be used by moment to parse the date. If this is a function, it must return a moment.js object given the appropriate data value.
docs/02-Scales.md:tooltipFormat | String | '' | The moment js format string to use for the tooltip.
docs/02-Scales.md:When providing data for the time scale, Chart.js supports all of the formats that Moment.js accepts. See [Moment.js docs](http://momentjs.com/docs/#/parsing/) for details.
docs/02-Scales.md:The following display formats are used to configure how different time units are formed into strings for the axis tick marks. See [moment.js](http://momentjs.com/docs/#/displaying/format/) for the allowable format strings.
gulpfile.js: './node_modules/moment/min/moment.min.js'
gulpfile.js: .ignore('moment')
gulpfile.js: 'moment',
package.json: "moment": "^2.10.6"
README.md:Chart.js provides two different builds that are available for your use. The `Chart.js` and `Chart.min.js` files include Chart.js and the accompanying color parsing library. If this version is used and you require the use of the time axis, [Moment.js](http://momentjs.com/) will need to be included before Chart.js.
README.md:Before submitting an issue or a pull request to the project, please take a moment to look over the [contributing guidelines](https://github.com/chartjs/Chart.js/blob/master/CONTRIBUTING.md) first.
samples/scales/time/combo-time-scale.html: <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
samples/scales/time/combo-time-scale.html: return moment().add(days, 'd').format(timeFormat);
samples/scales/time/line-time-point-data.html: <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
samples/scales/time/line-time-point-data.html: return moment().add(days, 'd').toDate();
samples/scales/time/line-time-point-data.html: return moment().add(days, 'd').format();
samples/scales/time/line-time-point-data.html: var lastTime = myLine.scales['x-axis-0'].labelMoments[0].length ? myLine.scales['x-axis-0'].labelMoments[0][myLine.scales['x-axis-0'].labelMoments[0].length - 1] : moment();
samples/scales/time/line-time-scale.html: <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
samples/scales/time/line-time-scale.html: return moment().add(days, 'd').toDate();
samples/scales/time/line-time-scale.html: return moment().add(days, 'd').format(timeFormat);
samples/scales/time/line-time-scale.html: return moment().add(days, 'd').unix();
src/scales/scale.time.js:var moment = require('moment');
src/scales/scale.time.js:moment = typeof(moment) === 'function' ? moment : window.moment;
src/scales/scale.time.js: parser: false, // false == a pattern string from http://momentjs.com/docs/#/parsing/string-format/ or a custom callback that converts its argument to a moment
src/scales/scale.time.js: format: false, // DEPRECATED false == date objects, moment object, callback or a pattern string from http://momentjs.com/docs/#/parsing/string-format/
src/scales/scale.time.js: isoWeekday: false, // override week start day - see http://momentjs.com/docs/#/get-set/iso-weekday/
src/scales/scale.time.js: // defaults to unit's corresponding unitFormat below or override using pattern string from http://momentjs.com/docs/#/displaying/format/
src/scales/scale.time.js: if (!moment) {
src/scales/scale.time.js: throw new Error('Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com');
src/scales/scale.time.js: me.firstTick = moment.min.call(me, scaleLabelMoments);
src/scales/scale.time.js: me.lastTick = moment.max.call(me, scaleLabelMoments);
src/scales/scale.time.js: var momentsForDataset = [];
src/scales/scale.time.js: momentsForDataset.push(labelMoment);
src/scales/scale.time.js: me.firstTick = me.firstTick !== null ? moment.min(me.firstTick, labelMoment) : labelMoment;
src/scales/scale.time.js: me.lastTick = me.lastTick !== null ? moment.max(me.lastTick, labelMoment) : labelMoment;
src/scales/scale.time.js: momentsForDataset = scaleLabelMoments;
src/scales/scale.time.js: me.labelMoments.push(momentsForDataset);
src/scales/scale.time.js: me.firstTick = (me.firstTick || moment()).clone();
src/scales/scale.time.js: me.lastTick = (me.lastTick || moment()).clone();
src/scales/scale.time.js: // For every unit in between the first and last moment, create a moment and add it to the ticks tick
src/scales/scale.time.js: // not already a moment object
src/scales/scale.time.js: return me.firstTick.clone().add(moment.duration(offset, me.tickUnit).asSeconds(), 'seconds');
src/scales/scale.time.js: return moment(label, me.options.time.parser);
src/scales/scale.time.js: return moment(label);
src/scales/scale.time.js: // Custom parsing (return an instance of moment)
src/scales/scale.time.js: return moment(label, me.options.time.format);
test/scale.time.tests.js: it('Should load moment.js as a dependency', function() {
test/scale.time.tests.js: expect(window.moment).not.toBe(undefined);
test/scale.time.tests.js: return moment('01/01/2015 12:00', 'DD/MM/YYYY HH:mm').add(days, 'd').toDate();
test/scale.time.tests.js: return moment('01/01/2015 12:00', 'DD/MM/YYYY HH:mm').add(days, 'd').toDate();
test/scale.time.tests.js: return moment.unix(label);
test/scale.time.tests.js: value: moment(chart.data.labels[0]),
test/scale.time.tests.js: value: moment(chart.data.labels[6]),
test/scale.time.tests.js: value: moment(chart.data.labels[0]),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment