Skip to content

Commit d80c36c

Browse files
committed
Rename to chartjs-plugin-annotation, fix issues and prep for v0.2 release
1 parent 17ed3ed commit d80c36c

File tree

10 files changed

+85
-70
lines changed

10 files changed

+85
-70
lines changed

Chart.Annotation.min.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Chart.Annotation.js
1+
# chartjs-plugin-annotation.js
22

33
An annotation plugin for Chart.js >= 2.1.3
44

@@ -123,7 +123,7 @@ You can find documentation for Chart.js at [www.chartjs.org/docs](http://www.cha
123123

124124
## Contributing
125125

126-
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.Annotation.js/blob/master/CONTRIBUTING.md) first.
126+
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/chartjs-plugin-annotation.js/blob/master/CONTRIBUTING.md) first.
127127

128128
## License
129129

Chart.Annotation.js renamed to chartjs-plugin-annotation.js

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
2-
* Chart.Annotation.js
2+
* chartjs-plugin-annotation.js
33
* http://chartjs.org/
4-
* Version: 0.1.1
4+
* Version: 0.2.0
55
*
66
* Copyright 2016 Evert Timberg
77
* Released under the MIT license
@@ -118,8 +118,24 @@ var drawTimeOptions = Chart.Annotation.drawTimeOptions = {
118118
beforeDatasetsDraw: "beforeDatasetsDraw",
119119
};
120120

121+
function drawAnnotations(drawTime, chartInstance, easingDecimal) {
122+
var annotationOpts = chartInstance.options.annotation;
123+
if (annotationOpts.drawTime != drawTime) {
124+
return;
125+
}
126+
// If we have annotations, draw them
127+
var annotationObjects = chartInstance._annotationObjects;
128+
if (isArray(annotationObjects)) {
129+
var ctx = chartInstance.chart.ctx;
130+
131+
annotationObjects.forEach(function(obj) {
132+
obj.transition(easingDecimal).draw(ctx);
133+
});
134+
}
135+
}
136+
121137
// Chartjs Zoom Plugin
122-
var AnnotationPlugin = Chart.PluginBase.extend({
138+
var annotationPlugin = {
123139
beforeInit: function(chartInstance) {
124140
var options = chartInstance.options;
125141
options.annotation = helpers.configMerge(Chart.Annotation.defaults, options.annotation);
@@ -156,45 +172,30 @@ var AnnotationPlugin = Chart.PluginBase.extend({
156172
},
157173

158174
afterDraw: function(chartInstance, easingDecimal) {
159-
this.drawAnnotations(
175+
drawAnnotations(
160176
Chart.Annotation.drawTimeOptions.afterDraw,
161177
chartInstance,
162178
easingDecimal
163179
);
164180
},
165181
afterDatasetsDraw: function(chartInstance, easingDecimal) {
166-
this.drawAnnotations(
182+
drawAnnotations(
167183
Chart.Annotation.drawTimeOptions.afterDatasetsDraw,
168184
chartInstance,
169185
easingDecimal
170186
);
171187
},
172188
beforeDatasetsDraw: function(chartInstance, easingDecimal) {
173-
this.drawAnnotations(
189+
drawAnnotations(
174190
Chart.Annotation.drawTimeOptions.beforeDatasetsDraw,
175191
chartInstance,
176192
easingDecimal
177193
);
178-
},
179-
drawAnnotations: function(drawTime, chartInstance, easingDecimal) {
180-
var annotationOpts = chartInstance.options.annotation;
181-
if (annotationOpts.drawTime != drawTime) {
182-
return;
183-
}
184-
// If we have annotations, draw them
185-
var annotationObjects = chartInstance._annotationObjects;
186-
if (isArray(annotationObjects)) {
187-
var ctx = chartInstance.chart.ctx;
188-
189-
annotationObjects.forEach(function(obj) {
190-
obj.transition(easingDecimal).draw(ctx);
191-
});
192-
}
193194
}
194-
});
195+
};
195196

196-
module.exports = AnnotationPlugin;
197-
Chart.pluginService.register(new AnnotationPlugin());
197+
module.exports = annotationPlugin;
198+
Chart.pluginService.register(annotationPlugin);
198199

199200
},{"./box.js":2,"./line.js":4,"chart.js":1}],4:[function(require,module,exports){
200201
// Line Annotation implementation
@@ -226,22 +227,31 @@ module.exports = function(Chart) {
226227
}
227228
});
228229

230+
function isValid(num) {
231+
return !isNaN(num) && isFinite(num);
232+
}
233+
229234
function lineUpdate(obj, options, chartInstance) {
230235
var model = obj._model = obj._model || {};
231236

232237
var scale = chartInstance.scales[options.scaleID];
233238
var pixel = scale ? scale.getPixelForValue(options.value) : NaN;
239+
var endPixel = scale && isValid(options.endValue) ? scale.getPixelForValue(options.endValue) : NaN;
240+
if (isNaN(endPixel))
241+
endPixel = pixel;
234242
var chartArea = chartInstance.chartArea;
235243

236244
if (!isNaN(pixel)) {
237245
if (options.mode == horizontalKeyword) {
238246
model.x1 = chartArea.left;
239247
model.x2 = chartArea.right;
240-
model.y1 = model.y2 = pixel;
248+
model.y1 = pixel;
249+
model.y2 = endPixel;
241250
} else {
242251
model.y1 = chartArea.top;
243252
model.y2 = chartArea.bottom;
244-
model.x1 = model.x2 = pixel;
253+
model.x1 = pixel;
254+
model.x2 = endPixel;
245255
}
246256
}
247257

chartjs-plugin-annotation.min.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var srcDir = './src/';
1919
var outDir = './';
2020

2121
var header = "/*!\n\
22-
* Chart.Annotation.js\n\
22+
* chartjs-plugin-annotation.js\n\
2323
* http://chartjs.org/\n\
2424
* Version: {{ version }}\n\
2525
*\n\
@@ -37,14 +37,14 @@ function buildTask() {
3737
.ignore('chart.js')
3838
.ignore('hammerjs')
3939
.bundle()
40-
.pipe(source('Chart.Annotation.js'))
40+
.pipe(source('chartjs-plugin-annotation.js'))
4141
.pipe(insert.prepend(header))
4242
.pipe(streamify(replace('{{ version }}', package.version)))
4343
.pipe(gulp.dest(outDir))
4444
.pipe(streamify(uglify({
4545
preserveComments: 'some'
4646
})))
47-
.pipe(streamify(concat('Chart.Annotation.min.js')))
47+
.pipe(streamify(concat('chartjs-plugin-annotation.min.js')))
4848
.pipe(gulp.dest(outDir));
4949

5050
return nonBundled;

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "Chart.Annotation.js",
2+
"name": "chartjs-plugin-annotation",
33
"description": "Annotations for Chart.js",
4-
"version": "0.1.1",
4+
"version": "0.2.0",
55
"license": "MIT",
66
"main": "src/chart.annotation.js",
77
"repository": {
88
"type": "git",
9-
"url": "https://github.com/chartjs/Chart.Annotation.js.git"
9+
"url": "https://github.com/chartjs/chartjs-plugin-annotation.git"
1010
},
1111
"devDependencies": {
1212
"browserify": "^13.0.0",

samples/box.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<head>
55
<title>Scatter Chart</title>
66
<script src="../node_modules/chart.js/dist/Chart.bundle.js"></script>
7-
<script src="../Chart.Annotation.js"></script>
7+
<script src="../chartjs-plugin-annotation.js"></script>
88
<style>
99
canvas {
1010
-moz-user-select: none;
@@ -127,7 +127,7 @@
127127
}]
128128
},
129129
annotation: {
130-
drawTime: "beforeDatasetsDraw",
130+
drawTime: "afterDraw",
131131
annotations: [{
132132
type: 'box',
133133
xScaleID: 'x-axis-1',

samples/horizontal-line.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<head>
55
<title>Scatter Chart</title>
66
<script src="../node_modules/chart.js/dist/Chart.bundle.js"></script>
7-
<script src="../Chart.Annotation.js"></script>
7+
<script src="../chartjs-plugin-annotation.js"></script>
88
<style>
99
canvas {
1010
-moz-user-select: none;
@@ -127,7 +127,7 @@
127127
}]
128128
},
129129
annotation: {
130-
drawTime: "beforeDatasetsDraw",
130+
drawTime: "afterDraw",
131131
annotations: [{
132132
type: 'line',
133133
mode: 'horizontal',

src/chart.annotation.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,24 @@ var drawTimeOptions = Chart.Annotation.drawTimeOptions = {
3434
beforeDatasetsDraw: "beforeDatasetsDraw",
3535
};
3636

37+
function drawAnnotations(drawTime, chartInstance, easingDecimal) {
38+
var annotationOpts = chartInstance.options.annotation;
39+
if (annotationOpts.drawTime != drawTime) {
40+
return;
41+
}
42+
// If we have annotations, draw them
43+
var annotationObjects = chartInstance._annotationObjects;
44+
if (isArray(annotationObjects)) {
45+
var ctx = chartInstance.chart.ctx;
46+
47+
annotationObjects.forEach(function(obj) {
48+
obj.transition(easingDecimal).draw(ctx);
49+
});
50+
}
51+
}
52+
3753
// Chartjs Zoom Plugin
38-
var AnnotationPlugin = Chart.PluginBase.extend({
54+
var annotationPlugin = {
3955
beforeInit: function(chartInstance) {
4056
var options = chartInstance.options;
4157
options.annotation = helpers.configMerge(Chart.Annotation.defaults, options.annotation);
@@ -72,42 +88,27 @@ var AnnotationPlugin = Chart.PluginBase.extend({
7288
},
7389

7490
afterDraw: function(chartInstance, easingDecimal) {
75-
this.drawAnnotations(
91+
drawAnnotations(
7692
Chart.Annotation.drawTimeOptions.afterDraw,
7793
chartInstance,
7894
easingDecimal
7995
);
8096
},
8197
afterDatasetsDraw: function(chartInstance, easingDecimal) {
82-
this.drawAnnotations(
98+
drawAnnotations(
8399
Chart.Annotation.drawTimeOptions.afterDatasetsDraw,
84100
chartInstance,
85101
easingDecimal
86102
);
87103
},
88104
beforeDatasetsDraw: function(chartInstance, easingDecimal) {
89-
this.drawAnnotations(
105+
drawAnnotations(
90106
Chart.Annotation.drawTimeOptions.beforeDatasetsDraw,
91107
chartInstance,
92108
easingDecimal
93109
);
94-
},
95-
drawAnnotations: function(drawTime, chartInstance, easingDecimal) {
96-
var annotationOpts = chartInstance.options.annotation;
97-
if (annotationOpts.drawTime != drawTime) {
98-
return;
99-
}
100-
// If we have annotations, draw them
101-
var annotationObjects = chartInstance._annotationObjects;
102-
if (isArray(annotationObjects)) {
103-
var ctx = chartInstance.chart.ctx;
104-
105-
annotationObjects.forEach(function(obj) {
106-
obj.transition(easingDecimal).draw(ctx);
107-
});
108-
}
109110
}
110-
});
111+
};
111112

112-
module.exports = AnnotationPlugin;
113-
Chart.pluginService.register(new AnnotationPlugin());
113+
module.exports = annotationPlugin;
114+
Chart.pluginService.register(annotationPlugin);

src/line.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ module.exports = function(Chart) {
2727
}
2828
});
2929

30+
function isValid(num) {
31+
return !isNaN(num) && isFinite(num);
32+
}
33+
3034
function lineUpdate(obj, options, chartInstance) {
3135
var model = obj._model = obj._model || {};
3236

0 commit comments

Comments
 (0)