Skip to content

Commit

Permalink
remove arrow functions and polyfill Array.find for IE
Browse files Browse the repository at this point in the history
for #1507
  • Loading branch information
gordonwoodhull committed Dec 7, 2018
1 parent 1177172 commit e3a62f6
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 20 deletions.
69 changes: 59 additions & 10 deletions web/examples/boxplot-enhanced.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,55 @@ <h6><u>Enhanced</u></h6>
<script type="text/javascript" src="../js/dc.js"></script>
<script type="text/javascript">


// polyfill Array.find for IE
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
value: function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}

var o = Object(this);

// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;

// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}

// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];

// 5. Let k be 0.
var k = 0;

// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}

// 7. Return undefined.
return undefined;
},
configurable: true,
writable: true
});
}

let clubColors = [{'value': 'Tiny Tots', 'color': '#9be199'}, {'value': 'High Flyers', 'color': '#e17f7e'}];
let genderColors = [{'value': 'Female', 'color': '#efc3e2'}, {'value': 'Male', 'color': '#76bbe1'}];

Expand Down Expand Up @@ -115,8 +164,8 @@ <h6><u>Enhanced</u></h6>
.group(pie01Group)
;

pie01.colorAccessor(data => data.key)
.colors(data => {
pie01.colorAccessor(function(data) { return data.key; })
.colors(function(data) {
return colorSchema(clubColors, data);
})

Expand All @@ -133,8 +182,8 @@ <h6><u>Enhanced</u></h6>
.group(pie02Group)
;

pie02.colorAccessor(data => data.key)
.colors(data => {
pie02.colorAccessor(function(data) { return data.key; })
.colors(function(data) {
return colorSchema(genderColors, data);
})

Expand Down Expand Up @@ -240,8 +289,8 @@ <h6><u>Enhanced</u></h6>
.elasticX(true)
;

bp03.colorAccessor(data => data.key)
.colors(data => {
bp03.colorAccessor(function(data) { return data.key; })
.colors(function(data) {
return colorSchema(genderColors, data);
});

Expand Down Expand Up @@ -269,18 +318,18 @@ <h6><u>Enhanced</u></h6>
bp04
.on('preRender', function() {get_counts(bp04, bp04Counts)})
.on('preRedraw', function() {get_counts(bp04, bp04Counts)})
.xAxis().tickFormat(v => v + ' [' + bp04Counts[v] + ']');
.xAxis().tickFormat(function(v) { return v + ' [' + bp04Counts[v] + ']'; });

bp04.colorAccessor(data => data.key)
.colors(data => {
bp04.colorAccessor(function(data) { return data.key; })
.colors(function(data) {
return colorSchema(genderColors, data);
});

// -----------------------------------------------
dc.renderAll();

function colorSchema(pref, aa) {
a = pref.find(i => i.value === aa);
a = pref.find(function(i) { return i.value === aa; });
return a.color;
}

Expand Down
63 changes: 56 additions & 7 deletions web/examples/boxplot-render-data.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,55 @@ <h6>dataOpacity = 0.4<br/>dataWidthPortion = 0.1</h6>
<script type="text/javascript" src="../js/dc.js"></script>
<script type="text/javascript">


// polyfill Array.find for IE
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
value: function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}

var o = Object(this);

// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;

// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}

// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];

// 5. Let k be 0.
var k = 0;

// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}

// 7. Return undefined.
return undefined;
},
configurable: true,
writable: true
});
}

let genderColors = [{'value': 'Female', 'color': '#efc3e2'}, {'value': 'Male', 'color': '#76bbe1'}];

let data = [
Expand Down Expand Up @@ -122,8 +171,8 @@ <h6>dataOpacity = 0.4<br/>dataWidthPortion = 0.1</h6>
.elasticX(true)
;

bp01.colorAccessor(data => data.key)
.colors(data => {
bp01.colorAccessor(function(data) { return data.key; })
.colors(function(data) {
return colorSchema(genderColors, data);
});

Expand All @@ -146,8 +195,8 @@ <h6>dataOpacity = 0.4<br/>dataWidthPortion = 0.1</h6>
.elasticX(true)
;

bp02.colorAccessor(data => data.key)
.colors(data => {
bp02.colorAccessor(function(data) { return data.key; })
.colors(function(data) {
return colorSchema(genderColors, data);
});

Expand All @@ -170,16 +219,16 @@ <h6>dataOpacity = 0.4<br/>dataWidthPortion = 0.1</h6>
.elasticX(true)
;

bp03.colorAccessor(data => data.key)
.colors(data => {
bp03.colorAccessor(function(data) { return data.key; })
.colors(function(data) {
return colorSchema(genderColors, data);
});

// -----------------------------------------------
dc.renderAll();

function colorSchema(pref, aa) {
a = pref.find(i => i.value === aa);
a = pref.find(function(i) { return i.value === aa; });
return a.color;
}

Expand Down
54 changes: 51 additions & 3 deletions web/examples/focus-dynamic-interval.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,54 @@
<script type="text/javascript" src="../js/dc.js"></script>
<script type="text/javascript">

// polyfill Array.find for IE
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
value: function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}

var o = Object(this);

// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;

// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}

// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];

// 5. Let k be 0.
var k = 0;

// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}

// 7. Return undefined.
return undefined;
},
configurable: true,
writable: true
});
}

(function() { // for emacs indenting (sorry)
function nonzero_min(chart) {
dc.override(chart, "yAxisMin", function() {
Expand Down Expand Up @@ -89,7 +137,7 @@
}
function choose_group(extent) {
var d = extent[1].getTime() - extent[0].getTime();
var found = groups_by_min_interval.find(mg => mg.threshold < d);
var found = groups_by_min_interval.find(function(mg) { return mg.threshold < d; });
console.log('interval ' + d + ' is more than ' + found.threshold + ' ms; choosing ' + found.name +
' for ' + found.interval.range(extent[0], extent[1]).length + ' points');
if(!found.group)
Expand All @@ -103,7 +151,7 @@
.dimension(dimension)
.group(choose_group(fullDomain))
.yAxisPadding(0.1)
.valueAccessor(kv => kv.value.total / kv.value.count)
.valueAccessor(function(kv) { return kv.value.total / kv.value.count; })
.rangeChart(rangeChart)
.x(d3.scaleTime().domain(fullDomain))
.xUnits(d3.timeDay)
Expand All @@ -123,7 +171,7 @@
.dimension(dimension)
.group(groups_by_min_interval[0].group)
.yAxisPadding(1)
.valueAccessor(kv => kv.value.total / kv.value.count)
.valueAccessor(function(kv) { return kv.value.total / kv.value.count; })
.x(d3.scaleTime().domain(fullDomain))
.xUnits(d3.timeDay);

Expand Down

0 comments on commit e3a62f6

Please sign in to comment.