Skip to content

Commit

Permalink
Add text truncate transform.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Apr 25, 2013
1 parent a19dc3e commit 568b1fc
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ vega.js: \
src/data/stack.js \
src/data/stats.js \
src/data/treemap.js \
src/data/truncate.js \
src/data/unique.js \
src/data/wordcloud.js \
src/data/zip.js \
Expand Down
33 changes: 33 additions & 0 deletions src/_package.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,39 @@ vg.maxIndex = function(data, f) {
return idx;
};

vg.truncate = function(s, length, pos, word, ellipsis) {
var len = s.length;
if (len <= length) return s;
ellipsis = ellipsis || "...";
var l = Math.max(0, length - ellipsis.length);

switch (pos) {
case "left":
return ellipsis + (word ? vg_truncateOnWord(s,l,1) : s.slice(len-l));
case "middle":
case "center":
var l1 = Math.ceil(l/2), l2 = Math.floor(l/2);
return (word ? vg_truncateOnWord(s,l1) : s.slice(0,l1)) + ellipsis
+ (word ? vg_truncateOnWord(s,l2,1) : s.slice(len-l2));
default:
return (word ? vg_truncateOnWord(s,l) : s.slice(0,l)) + ellipsis;
}
}

function vg_truncateOnWord(s, len, rev) {
var cnt = 0, tok = s.split(vg_truncate_word_re);
if (rev) {
s = (tok = tok.reverse())
.filter(function(w) { cnt += w.length; return cnt <= len; })
.reverse();
} else {
s = tok.filter(function(w) { cnt += w.length; return cnt <= len; });
}
return s.length ? s.join("").trim() : tok[0].slice(0, len);
}

var vg_truncate_word_re = /([\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u2028\u2029\u3000\uFEFF])/;

// Logging

function vg_write(msg) {
Expand Down
3 changes: 2 additions & 1 deletion src/canvas/marks.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@ vg.canvas.marks = (function() {
}

function fontString(o) {
// TODO config
return (o.fontStyle ? o.fontStyle + " " : "")
+ (o.fontVariant ? o.fontVariant + " " : "")
+ (o.fontWeight ? o.fontWeight + " " : "")
+ (o.fontSize != undefined ? o.fontSize + "px " : "11px ")
+ (o.fontSize != null ? o.fontSize + "px " : "11px ")
+ (o.font || "sans-serif");
}

Expand Down
45 changes: 45 additions & 0 deletions src/data/truncate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
vg.data.truncate = function() {
var value = vg.accessor("data"),
as = "truncate",
position = "right",
ellipsis = "...",
wordBreak = true,
limit = 100;

var truncate = vg.data.mapper(function(d) {
var text = vg.truncate(value(d), limit, position, wordBreak, ellipsis);
return (d[as] = text, d);
});

truncate.value = function(field) {
value = vg.accessor(field);
return truncate;
};

truncate.as = function(field) {
as = field;
return truncate;
};

truncate.limit = function(len) {
limit = +len;
return truncate;
};

truncate.position = function(pos) {
position = pos;
return truncate;
};

truncate.ellipsis = function(str) {
ellipsis = str+"";
return truncate;
};

truncate.wordBreak = function(b) {
wordBreak = !!b;
return truncate;
};

return truncate;
};
80 changes: 79 additions & 1 deletion vega.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,39 @@ vg.maxIndex = function(data, f) {
return idx;
};

vg.truncate = function(s, length, pos, word, ellipsis) {
var len = s.length;
if (len <= length) return s;
ellipsis = ellipsis || "...";
var l = Math.max(0, length - ellipsis.length);

switch (pos) {
case "left":
return ellipsis + (word ? vg_truncateOnWord(s,l,1) : s.slice(len-l));
case "middle":
case "center":
var l1 = Math.ceil(l/2), l2 = Math.floor(l/2);
return (word ? vg_truncateOnWord(s,l1) : s.slice(0,l1)) + ellipsis
+ (word ? vg_truncateOnWord(s,l2,1) : s.slice(len-l2));
default:
return (word ? vg_truncateOnWord(s,l) : s.slice(0,l)) + ellipsis;
}
}

function vg_truncateOnWord(s, len, rev) {
var cnt = 0, tok = s.split(vg_truncate_word_re);
if (rev) {
s = (tok = tok.reverse())
.filter(function(w) { cnt += w.length; return cnt <= len; })
.reverse();
} else {
s = tok.filter(function(w) { cnt += w.length; return cnt <= len; });
}
return s.length ? s.join("").trim() : tok[0].slice(0, len);
}

var vg_truncate_word_re = /([\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u2028\u2029\u3000\uFEFF])/;

// Logging

function vg_write(msg) {
Expand Down Expand Up @@ -1172,10 +1205,11 @@ var vg_gradient_id = 0;vg.canvas = {};vg.canvas.path = (function() {
}

function fontString(o) {
// TODO config
return (o.fontStyle ? o.fontStyle + " " : "")
+ (o.fontVariant ? o.fontVariant + " " : "")
+ (o.fontWeight ? o.fontWeight + " " : "")
+ (o.fontSize != undefined ? o.fontSize + "px " : "11px ")
+ (o.fontSize != null ? o.fontSize + "px " : "11px ")
+ (o.font || "sans-serif");
}

Expand Down Expand Up @@ -3188,6 +3222,50 @@ function vg_load_http(url, callback) {
};

return treemap;
};vg.data.truncate = function() {
var value = vg.accessor("data"),
as = "truncate",
position = "right",
ellipsis = "...",
wordBreak = true,
limit = 100;

var truncate = vg.data.mapper(function(d) {
var text = vg.truncate(value(d), limit, position, wordBreak, ellipsis);
return (d[as] = text, d);
});

truncate.value = function(field) {
value = vg.accessor(field);
return truncate;
};

truncate.as = function(field) {
as = field;
return truncate;
};

truncate.limit = function(len) {
limit = +len;
return truncate;
};

truncate.position = function(pos) {
position = pos;
return truncate;
};

truncate.ellipsis = function(str) {
ellipsis = str+"";
return truncate;
};

truncate.wordBreak = function(b) {
wordBreak = !!b;
return truncate;
};

return truncate;
};vg.data.unique = function() {

var field = null,
Expand Down
6 changes: 3 additions & 3 deletions vega.min.js

Large diffs are not rendered by default.

0 comments on commit 568b1fc

Please sign in to comment.