Skip to content

Commit 0c94a5b

Browse files
committed
misc cleanup, added babelify to transpile before uglify(what a crazy world 2016 js is! ... entropy)
1 parent 8a6b847 commit 0c94a5b

File tree

4 files changed

+55
-40
lines changed

4 files changed

+55
-40
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}

demo/iris.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@
138138
// annotations
139139
var annotationG = svg.append('g'),
140140
annotation = window.annotation = d3.annotate()
141-
// .show(true)
142-
.show(false)
143-
// .show((d) => d.species !== "versicolor")
141+
.show(false) // .show((d) => d.species !== "versicolor")
144142
.text((d) => `${d.species}: ${d.sepalWidth.toFixed(1)} x ${d.sepalLength.toFixed(1)}`)
145143
.container(annotationG);
146144

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
{
22
"name": "d3-annotate",
3-
"version": "0.0.1",
3+
"author": {
4+
"name": "Chris Polis",
5+
"url": "https://twitter.com/ChrisPolis"
6+
},
7+
"version": "0.1.0",
48
"description": "Interactively and programmatically add, edit, move and save SVG chart annotations",
59
"keywords": [
610
"d3",
711
"d3-module",
812
"svg",
9-
"data-vis"
13+
"chart",
14+
"datavis",
15+
"visualization"
1016
],
11-
"license": "BSD-3-Clause",
17+
"license": "MIT",
1218
"main": "build/d3-annotate.js",
1319
"jsnext:main": "index",
1420
"homepage": "https://github.com/cmpolis/d3-annotate",
@@ -19,14 +25,16 @@
1925
"scripts": {
2026
"pretest": "rm -rf build && mkdir build && rollup -g d3-drag:d3,d3-selection:d3 -f umd -n d3 -o build/d3-annotate.js -- index.js",
2127
"test": "tape 'test/**/*-test.js'",
22-
"prepublish": "npm run test && uglifyjs build/d3-annotate.js -c -m -o build/d3-annotate.min.js",
28+
"prepublish": "npm run test && ./node_modules/.bin/babel build/d3-annotate.js -o build/d3-annotate-es5.js && uglifyjs build/d3-annotate-es5.js -c -m -o build/d3-annotate.min.js",
2329
"postpublish": "zip -j build/d3-annotate.zip -- LICENSE README.md build/d3-annotate.js build/d3-annotate.min.js"
2430
},
2531
"dependencies": {
2632
"d3-drag": "1.0",
2733
"d3-selection": "1.0"
2834
},
2935
"devDependencies": {
36+
"babel-cli": "^6.14.0",
37+
"babel-preset-es2015": "^6.14.0",
3038
"rollup": "0.27",
3139
"tape": "4",
3240
"uglify-js": "2"

src/annotate.js

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ export default function() {
55
var keyFn = (_, ndx) => ndx,
66
textFn = (d) => d,
77
container,
8-
dx = 0, dy = 0,
9-
x = (d) => d.pos.x + (d.pos.width / 2),
10-
y = (d) => d.pos.y + (d.pos.height / 2),
11-
textAnchor = 'start',
8+
x = (d) => d.box.x + (d.box.width / 2),
9+
y = (d) => d.box.y + (d.box.height / 2),
10+
textAnchor = 'middle',
1211
show = true,
1312
dragControl = drag()
1413
.on("start", function() { this.classList.add('dragging'); })
@@ -22,7 +21,6 @@ export default function() {
2221
//
2322
// serialize keys, bind click to add text, add text if `show` is T or fn
2423
function annotate(selection) {
25-
container.classed('d3-an-container', true);
2624
selection.nodes().forEach((el, ndx) => el.__key__ = keyFn(el.__data__, ndx));
2725
selection.on('click', function() { appendText(select(this)); });
2826
if(show) { appendText(selection, true); }
@@ -32,14 +30,9 @@ export default function() {
3230
// add new data bound <text> annotation
3331
function appendText(sel, filter) {
3432
var _sel = (show instanceof Function && filter) ? sel.filter(show) : sel,
35-
_keyFn = (d) => keyFn(d.data),
3633
_textFn = (d) => textFn(d.data),
37-
annotationData = _sel.nodes().map((node, ndx) => {
38-
return {
39-
data: node.__data__,
40-
key: node.__key__,
41-
pos: node.getBBox()
42-
};
34+
annotationData = _sel.nodes().map((node) => {
35+
return { data: node.__data__, key: node.__key__, box: node.getBBox() };
4336
});
4437

4538
var textSelection = container.selectAll('text.with-data')
@@ -48,7 +41,6 @@ export default function() {
4841
.text(_textFn)
4942
.attr('class', 'annotation with-data')
5043
.attr('x', x).attr('y', y)
51-
.attr('dx', dx).attr('dy', dy)
5244
.attr('text-anchor', textAnchor)
5345
.call(dragControl)
5446
.on('click', function() {
@@ -57,26 +49,6 @@ export default function() {
5749
});
5850
}
5951

60-
//
61-
// properties
62-
annotate.container = function() {
63-
if(arguments.length) {
64-
container = arguments[0];
65-
return annotate;
66-
} else { return container; }
67-
};
68-
annotate.text = function() {
69-
if(arguments.length) {
70-
textFn = arguments[0];
71-
return annotate;
72-
} else { return textFn; }
73-
};
74-
annotate.show = function() {
75-
if(arguments.length) {
76-
show = arguments[0];
77-
return annotate;
78-
} else { return show; }
79-
};
8052

8153
//
8254
// text editor
@@ -107,5 +79,39 @@ export default function() {
10779
//
10880
// TODO: add annotations from object
10981

82+
83+
//
84+
// properties
85+
annotate.container = function(_) {
86+
if(!arguments.length) return container;
87+
container = _;
88+
container.classed('d3-an-container', true);
89+
return annotate;
90+
};
91+
annotate.text = function(_) {
92+
if(!arguments.length) return text;
93+
textFn = _; return annotate;
94+
};
95+
annotate.key = function(_) {
96+
if(!arguments.length) return keyFn;
97+
keyFn = _; return annotate;
98+
};
99+
annotate.show = function(_) {
100+
if(!arguments.length) return show;
101+
show = _; return annotate;
102+
};
103+
annotate.textAnchor = function(_) {
104+
if(!arguments.length) return textAnchor;
105+
textAnchor = _; return annotate;
106+
};
107+
annotate.x = function(_) {
108+
if(!arguments.length) return x;
109+
x = _; return annotate;
110+
};
111+
annotate.y = function(_) {
112+
if(!arguments.length) return y;
113+
y = _; return annotate;
114+
};
115+
110116
return annotate;
111117
};

0 commit comments

Comments
 (0)