Skip to content

Commit acc1775

Browse files
committed
Repository setup and initial implementation
0 parents  commit acc1775

File tree

6 files changed

+266
-0
lines changed

6 files changed

+266
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/dist
2+
/node_modules
3+
.DS_Store

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
**The MIT License (MIT)**
2+
3+
Copyright (c) 2016 Simon Brunel
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Chart.Deferred.js
2+
3+
A [Chart.js](http://www.chartjs.org/) plugin to defer the initial chart update until the user scrolls and the canvas appears inside the viewport. The main intent of Chart.Deferred.js is to trigger the initial chart animations when the user is likely to see them.
4+
5+
Requires Chart.js **2.1.5** or later.
6+
7+
## Configuration
8+
9+
To configure this plugin, you can simply add the following entries to your chart options:
10+
11+
| Name | Type | Default | Description |
12+
| ---- | ---- | ------- | ----------- |
13+
| `deferred` | `Object/Boolean` | `true` | The deferred options (see `deferred.*` options). Also accepts a boolean, in which case if `true`, the chart will be deferred using the default options, else if `false`, the chart will not be deferred.
14+
| `deferred.enabled` | `Boolean` | `true` | `true` to enable this plugin, else `false` to disable it for the associated chart.
15+
| `deferred.xOffset` | `Number/String` | `0` | Number of pixels (or percent of the canvas width) from which the chart is considered inside the viewport.
16+
| `deferred.yOffset` | `Number/String` | `0` | Number of pixels (or percent of the canvas height) from which the chart is considered inside the viewport.
17+
| `deferred.delay` | `Number` | `0` | Number of milliseconds to delay the loading after the chart is considered inside the viewport.
18+
19+
For example:
20+
21+
```
22+
{
23+
deferred: { // enabled by default
24+
xOffset: 150, // defer until 150px of the canvas width are inside the viewport
25+
yOffset: '50%', // defer until 50% of the canvas height are inside the viewport
26+
delay: 500 // delay of 500 ms after the canvas is considered inside the viewport
27+
}
28+
}
29+
```
30+
31+
Note that default options will defer the chart loading until the first line of pixels of the canvas appears in the viewport.
32+
33+
## Development
34+
35+
The following commands are available from the repository root (requires [Node.js](https://nodejs.org/))
36+
37+
```shell
38+
> npm install // initialize node dependencies
39+
> gulp build // build dist files
40+
> gulp build --watch // build and watch for changes
41+
```
42+
43+
## License
44+
45+
Chart.Deferred.js is available under the [MIT license](LICENSE.md).

gulpfile.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var argv = require('yargs').argv
2+
var gulp = require('gulp');
3+
var insert = require('gulp-insert');
4+
var rename = require('gulp-rename');
5+
var replace = require('gulp-replace');
6+
var streamify = require('gulp-streamify');
7+
var uglify = require('gulp-uglify');
8+
var gutil = require('gulp-util');
9+
var path = require('path');
10+
var package = require('./package.json');
11+
12+
var srcDir = './src/';
13+
var outDir = './dist/';
14+
15+
var header = "/*!\n\
16+
* Chart.Deferred.js\n\
17+
* http://chartjs.org/\n\
18+
* Version: {{ version }}\n\
19+
*\n\
20+
* Copyright 2016 Simon Brunel\n\
21+
* Released under the MIT license\n\
22+
* https://github.com/chartjs/Chart.Deferred.js/blob/master/LICENSE.md\n\
23+
*/\n";
24+
25+
gulp.task('build', buildTask);
26+
gulp.task('default', buildTask);
27+
28+
function watch(glob, task) {
29+
gutil.log('Waiting for changes...');
30+
return gulp.watch(glob, function(e) {
31+
gutil.log('Changes detected for', path.relative('.', e.path), '(' + e.type + ')');
32+
task();
33+
gutil.log('Waiting for changes...');
34+
});
35+
}
36+
37+
function buildTask() {
38+
var task = function() {
39+
return gulp.src(srcDir + 'chart.deferred.js')
40+
.pipe(rename('Chart.Deferred.js'))
41+
.pipe(insert.prepend(header))
42+
.pipe(streamify(replace('{{ version }}', package.version)))
43+
.pipe(gulp.dest(outDir))
44+
.pipe(rename('Chart.Deferred.min.js'))
45+
.pipe(streamify(uglify({ preserveComments: 'license' })))
46+
.pipe(gulp.dest(outDir));
47+
};
48+
49+
if (argv.watch) {
50+
return task(), watch(srcDir + '**/*.js', task);
51+
} else {
52+
return task();
53+
}
54+
}

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "chart.deferred.js",
3+
"homepage": "http://www.chartjs.org",
4+
"description": "Chart.js plugin to defer initial animation.",
5+
"version": "0.1.0",
6+
"license": "MIT",
7+
"main": "src/plugin.js",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/chartjs/Chart.Deferred.js.git"
11+
},
12+
"devDependencies": {
13+
"gulp": "^3.9.1",
14+
"gulp-insert": "^0.5.0",
15+
"gulp-replace": "^0.5.4",
16+
"gulp-rename": "^1.2.2",
17+
"gulp-streamify": "^1.0.2",
18+
"gulp-uglify": "^1.5.3",
19+
"gulp-util": "^3.0.7",
20+
"path": "^0.12.7",
21+
"yargs": "^4.7.1"
22+
}
23+
}

src/chart.deferred.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*global window: false */
2+
"use strict";
3+
4+
(function() {
5+
6+
var Chart = window.Chart;
7+
var helpers = Chart.helpers;
8+
9+
/**
10+
* Plugin based on discussion from Chart.js issue #2745.
11+
* @see https://github.com/chartjs/Chart.js/issues/2745
12+
*/
13+
Chart.Deferred = Chart.Deferred || {};
14+
Chart.Deferred.defaults = {
15+
enabled: true,
16+
xOffset: 0,
17+
yOffset: 0,
18+
delay: 0
19+
};
20+
21+
// DOM implementation
22+
// @todo move it in Chart.js: src/core/core.platform.js
23+
Chart.platform = helpers.extend(Chart.platform || {}, {
24+
defer: function(fn, delay, scope) {
25+
window.setTimeout(function() {
26+
fn.call(scope)
27+
}, delay);
28+
}
29+
});
30+
31+
var deferred_instances = [];
32+
33+
function computeOffset(value, base) {
34+
var number = parseInt(value, 10);
35+
if (isNaN(number)) {
36+
return 0;
37+
} else if (typeof value === 'string' && value.indexOf('%') !== -1) {
38+
return number / 100 * base;
39+
} else {
40+
return number;
41+
}
42+
}
43+
44+
function chartInViewport(instance) {
45+
var canvas = instance.chart.canvas;
46+
var model = instance._deferred_model;
47+
var rect = canvas.getBoundingClientRect();
48+
var dy = computeOffset(model.yOffset || 0, rect.height);
49+
var dx = computeOffset(model.xOffset || 0, rect.width);
50+
51+
return rect.right - dx >= 0
52+
&& rect.bottom - dy >= 0
53+
&& rect.left + dx <= window.innerWidth
54+
&& rect.top + dy <= window.innerHeight;
55+
}
56+
57+
helpers.addEvent(window, 'scroll', function() {
58+
var instances = deferred_instances.slice(0);
59+
var ilen = instances.length;
60+
var instance, i;
61+
62+
for (i=0; i<ilen; ++i) {
63+
instance = instances[i];
64+
if (chartInViewport(instance)) {
65+
instance.update();
66+
}
67+
}
68+
});
69+
70+
function buildDeferredModel(instance) {
71+
var defaults = Chart.Deferred.defaults;
72+
var options = instance.options.deferred;
73+
var getValue = helpers.getValueOrDefault;
74+
75+
if (options === undefined) {
76+
options = {};
77+
} else if (typeof options === 'boolean') {
78+
// accepting { options: { deferred: true } }
79+
options = { enabled: options };
80+
}
81+
82+
return {
83+
enabled: getValue(options.enabled, defaults.enabled),
84+
xOffset: getValue(options.xOffset, defaults.xOffset),
85+
yOffset: getValue(options.yOffset, defaults.yOffset),
86+
delay: getValue(options.delay, defaults.delay),
87+
delayed: false,
88+
loaded: false
89+
};
90+
}
91+
92+
Chart.plugins.register({
93+
beforeInit: function(instance) {
94+
instance._deferred_model = buildDeferredModel(instance);
95+
deferred_instances.push(instance);
96+
},
97+
98+
beforeDatasetsUpdate: function(instance) {
99+
var model = instance._deferred_model;
100+
var index;
101+
102+
if (!model.loaded) {
103+
if (model.enabled && !chartInViewport(instance)) {
104+
// cancel the datasets update
105+
return false;
106+
}
107+
108+
// avoid extra scroll update by removing the chart from the lazy instances.
109+
index = deferred_instances.indexOf(instance);
110+
deferred_instances.splice(index, 1);
111+
model.loaded = true;
112+
113+
if (model.delay > 0) {
114+
model.delayed = true;
115+
Chart.platform.defer(function() {
116+
model.delayed = false;
117+
instance.update();
118+
}, model.delay);
119+
120+
return false;
121+
}
122+
}
123+
124+
if (model.delayed) {
125+
// in case of delayed update, ensure to block external requests, such
126+
// as interacting with the legend label, or direct calls to update()
127+
return false;
128+
}
129+
}
130+
});
131+
132+
})();

0 commit comments

Comments
 (0)