Commit 5d5898ea authored by S Anand's avatar S Anand
Browse files

ENH: add g1.scale(). Fixes #31

parent c2f0f7e3
Pipeline #45052 passed with stage
in 2 minutes and 2 seconds
export { version } from './src/package.js'
export { scale } from './src/scale.js'
export { version } from './src/package.js' export { version } from './src/package.js'
export { url } from './index-urlfilter.js' export { url } from './index-urlfilter.js'
export { datafilter } from './src/datafilter.js' export { datafilter } from './src/datafilter.js'
export { scale } from './src/scale.js'
import './index-highlight.js' import './index-highlight.js'
import './index-template.js' import './index-template.js'
import './index-formhandler.js' import './index-formhandler.js'
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
"bootstrap": "4.0.0-beta.3", "bootstrap": "4.0.0-beta.3",
"browserify": "14", "browserify": "14",
"component-emitter": "1", "component-emitter": "1",
"d3": "4",
"d3-scale-chromatic": "1",
"eslint": "^4", "eslint": "^4",
"express": "4", "express": "4",
"faucet": "^0.0.1", "faucet": "^0.0.1",
......
...@@ -41,5 +41,11 @@ export default [ ...@@ -41,5 +41,11 @@ export default [
input: "index-leaflet", input: "index-leaflet",
plugins: [uglify()], plugins: [uglify()],
output: { file: "dist/leaflet.min.js", format: "umd", name: "g1" } output: { file: "dist/leaflet.min.js", format: "umd", name: "g1" }
},
{
input: "index-scale",
plugins: [uglify()],
output: { file: "dist/scale.min.js", format: "umd", name: "g1" }
} }
] ]
export { scale }
function scale(data, config) {
return function (val) {
var colorCategory, scale, color
var metricFormula = typeof config.metric == 'function' ? config.metric
: function (d) { return d[config.metric] }
if (config.scheme) {
color = config.scheme
if (color.lastIndexOf('scheme', 0) !== 0)
color = 'interpolate' + color
}
if (config.scale)
scale = config.scale.replace(/\w+/g, function (w) {
return w[0].toUpperCase() + w.slice(1).toLowerCase()
})
else if (color)
scale = 'Sequential'
else
scale = 'Linear'
var domain = config.domain || d3.extent(data, metricFormula)
if (color)
colorCategory = d3['scale' + scale](d3[color])
.domain(domain)
else if (config.range)
colorCategory = d3['scale' + scale]()
.domain(domain)
.range(config.range)
return colorCategory(metricFormula(val))
}
}
<!DOCTYPE html>
<html>
<head>
<title>sanddance tests</title>
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
<script src="../node_modules/d3/build/d3.js"></script>
<script src="../node_modules/d3-scale-chromatic/dist/d3-scale-chromatic.js"></script>
<script src="../dist/g1.js"></script>
<script src="tape.js"></script>
<style>
</style>
</head>
<body>
<script>
tape.onFinish(function () { window.renderComplete = true })
</script>
<div id="test">
</div>
<script>
var data = [];
for (var i = 1; i <= 100; i++) {
var obj = {
age: Math.floor(Math.random() * 100) + 1
};
data.push(obj)
};
tape('Test scale', function (t) {
var g1_scale, result, scale
// metric as string
g1_scale = g1.scale(data, {
metric: 'age',
scale: 'linear',
domain: [0, 100],
range: ['red', 'blue']
});
result = g1_scale(data[0]);
scale = d3.scaleLinear()
.domain([0, 100])
.range(['red', 'blue'])
t.equal(result, scale(data[0].age))
// metric as a function
g1_scale = g1.scale(data, {
metric: function (d) { return d.age },
scale: 'LINEAR',
domain: [0, 100],
range: ['red', 'blue']
});
result = g1_scale(data[0]);
scale = d3.scaleLinear()
.domain([0, 100])
.range(['red', 'blue'])
t.equal(result, scale(data[0].age))
// no scale
g1_scale = g1.scale(data, {
metric: function (d) { return d.age },
domain: [0, 100],
range: ['red', 'blue']
});
result = g1_scale(data[0]);
scale = d3.scaleLinear()
.domain([0, 100])
.range(['red', 'blue'])
t.equal(result, scale(data[0].age))
// no domain
g1_scale = g1.scale(data, {
metric: function (d) { return d.age },
range: ['red', 'blue']
});
result = g1_scale(data[0]);
scale = d3.scaleLinear()
.domain(d3.extent(data, function (d) { return d.age }))
.range(['red', 'blue'])
t.equal(result, scale(data[0].age))
// scheme
g1_scale = g1.scale(data, {
metric: function (d) { return d.age },
scheme: 'RdYlGn',
});
result = g1_scale(data[0]);
scale = d3.scaleSequential(d3.interpolateRdYlGn)
.domain(d3.extent(data, function (d) { return d.age }))
t.equal(result, scale(data[0].age))
// scheme with scale
g1_scale = g1.scale(data, {
metric: function (d) { return d.age },
scale: 'Ordinal',
scheme: 'schemeCategory10',
});
result = g1_scale(data[0]);
scale = d3.scaleOrdinal(d3.schemeCategory10)
.domain(d3.extent(data, function (d) { return d.age }))
t.equal(result, scale(data[0].age))
t.end()
})
</script>
</body>
</html>
...@@ -684,6 +684,223 @@ crypto-browserify@^3.0.0: ...@@ -684,6 +684,223 @@ crypto-browserify@^3.0.0:
randombytes "^2.0.0" randombytes "^2.0.0"
randomfill "^1.0.3" randomfill "^1.0.3"
d3-array@1, d3-array@1.2.1, d3-array@^1.2.0:
version "1.2.1"
resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-1.2.1.tgz#d1ca33de2f6ac31efadb8e050a021d7e2396d5dc"
d3-axis@1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/d3-axis/-/d3-axis-1.0.8.tgz#31a705a0b535e65759de14173a31933137f18efa"
d3-brush@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/d3-brush/-/d3-brush-1.0.4.tgz#00c2f238019f24f6c0a194a26d41a1530ffe7bc4"
dependencies:
d3-dispatch "1"
d3-drag "1"
d3-interpolate "1"
d3-selection "1"
d3-transition "1"
d3-chord@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/d3-chord/-/d3-chord-1.0.4.tgz#7dec4f0ba886f713fe111c45f763414f6f74ca2c"
dependencies:
d3-array "1"
d3-path "1"
d3-collection@1, d3-collection@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/d3-collection/-/d3-collection-1.0.4.tgz#342dfd12837c90974f33f1cc0a785aea570dcdc2"
d3-color@1, d3-color@1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/d3-color/-/d3-color-1.0.3.tgz#bc7643fca8e53a8347e2fbdaffa236796b58509b"
d3-dispatch@1, d3-dispatch@1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/d3-dispatch/-/d3-dispatch-1.0.3.tgz#46e1491eaa9b58c358fce5be4e8bed626e7871f8"
d3-drag@1, d3-drag@1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/d3-drag/-/d3-drag-1.2.1.tgz#df8dd4c502fb490fc7462046a8ad98a5c479282d"
dependencies:
d3-dispatch "1"
d3-selection "1"
d3-dsv@1, d3-dsv@1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/d3-dsv/-/d3-dsv-1.0.8.tgz#907e240d57b386618dc56468bacfe76bf19764ae"
dependencies:
commander "2"
iconv-lite "0.4"
rw "1"
d3-ease@1, d3-ease@1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/d3-ease/-/d3-ease-1.0.3.tgz#68bfbc349338a380c44d8acc4fbc3304aa2d8c0e"
d3-force@1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/d3-force/-/d3-force-1.1.0.tgz#cebf3c694f1078fcc3d4daf8e567b2fbd70d4ea3"
dependencies:
d3-collection "1"
d3-dispatch "1"
d3-quadtree "1"
d3-timer "1"
d3-format@1, d3-format@1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/d3-format/-/d3-format-1.2.2.tgz#1a39c479c8a57fe5051b2e67a3bee27061a74e7a"
d3-geo@1.9.1:
version "1.9.1"
resolved "https://registry.yarnpkg.com/d3-geo/-/d3-geo-1.9.1.tgz#157e3b0f917379d0f73bebfff3be537f49fa7356"
dependencies:
d3-array "1"
d3-hierarchy@1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/d3-hierarchy/-/d3-hierarchy-1.1.5.tgz#a1c845c42f84a206bcf1c01c01098ea4ddaa7a26"
d3-interpolate@1, d3-interpolate@1.1.6:
version "1.1.6"
resolved "https://registry.yarnpkg.com/d3-interpolate/-/d3-interpolate-1.1.6.tgz#2cf395ae2381804df08aa1bf766b7f97b5f68fb6"
dependencies:
d3-color "1"
d3-path@1, d3-path@1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/d3-path/-/d3-path-1.0.5.tgz#241eb1849bd9e9e8021c0d0a799f8a0e8e441764"
d3-polygon@1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/d3-polygon/-/d3-polygon-1.0.3.tgz#16888e9026460933f2b179652ad378224d382c62"
d3-quadtree@1, d3-quadtree@1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/d3-quadtree/-/d3-quadtree-1.0.3.tgz#ac7987e3e23fe805a990f28e1b50d38fcb822438"
d3-queue@3.0.7:
version "3.0.7"
resolved "https://registry.yarnpkg.com/d3-queue/-/d3-queue-3.0.7.tgz#c93a2e54b417c0959129d7d73f6cf7d4292e7618"
d3-random@1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/d3-random/-/d3-random-1.1.0.tgz#6642e506c6fa3a648595d2b2469788a8d12529d3"
d3-request@1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/d3-request/-/d3-request-1.0.6.tgz#a1044a9ef4ec28c824171c9379fae6d79474b19f"
dependencies:
d3-collection "1"
d3-dispatch "1"
d3-dsv "1"
xmlhttprequest "1"
d3-scale-chromatic@1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/d3-scale-chromatic/-/d3-scale-chromatic-1.2.0.tgz#25820d059c0eccc33e85f77561f37382a817ab58"
dependencies:
d3-color "1"
d3-interpolate "1"
d3-scale@1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/d3-scale/-/d3-scale-1.0.7.tgz#fa90324b3ea8a776422bd0472afab0b252a0945d"
dependencies:
d3-array "^1.2.0"
d3-collection "1"
d3-color "1"
d3-format "1"
d3-interpolate "1"
d3-time "1"
d3-time-format "2"
d3-selection@1, d3-selection@1.3.0, d3-selection@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/d3-selection/-/d3-selection-1.3.0.tgz#d53772382d3dc4f7507bfb28bcd2d6aed2a0ad6d"
d3-shape@1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/d3-shape/-/d3-shape-1.2.0.tgz#45d01538f064bafd05ea3d6d2cb748fd8c41f777"
dependencies:
d3-path "1"
d3-time-format@2, d3-time-format@2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/d3-time-format/-/d3-time-format-2.1.1.tgz#85b7cdfbc9ffca187f14d3c456ffda268081bb31"
dependencies:
d3-time "1"
d3-time@1, d3-time@1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/d3-time/-/d3-time-1.0.8.tgz#dbd2d6007bf416fe67a76d17947b784bffea1e84"
d3-timer@1, d3-timer@1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/d3-timer/-/d3-timer-1.0.7.tgz#df9650ca587f6c96607ff4e60cc38229e8dd8531"
d3-transition@1, d3-transition@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/d3-transition/-/d3-transition-1.1.1.tgz#d8ef89c3b848735b060e54a39b32aaebaa421039"
dependencies:
d3-color "1"
d3-dispatch "1"
d3-ease "1"
d3-interpolate "1"
d3-selection "^1.1.0"
d3-timer "1"
d3-voronoi@1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/d3-voronoi/-/d3-voronoi-1.1.2.tgz#1687667e8f13a2d158c80c1480c5a29cb0d8973c"
d3-zoom@1.7.1:
version "1.7.1"
resolved "https://registry.yarnpkg.com/d3-zoom/-/d3-zoom-1.7.1.tgz#02f43b3c3e2db54f364582d7e4a236ccc5506b63"
dependencies:
d3-dispatch "1"
d3-drag "1"
d3-interpolate "1"
d3-selection "1"
d3-transition "1"
d3@4:
version "4.13.0"
resolved "https://registry.yarnpkg.com/d3/-/d3-4.13.0.tgz#ab236ff8cf0cfc27a81e69bf2fb7518bc9b4f33d"
dependencies:
d3-array "1.2.1"
d3-axis "1.0.8"
d3-brush "1.0.4"
d3-chord "1.0.4"
d3-collection "1.0.4"
d3-color "1.0.3"
d3-dispatch "1.0.3"
d3-drag "1.2.1"
d3-dsv "1.0.8"
d3-ease "1.0.3"
d3-force "1.1.0"
d3-format "1.2.2"
d3-geo "1.9.1"
d3-hierarchy "1.1.5"
d3-interpolate "1.1.6"
d3-path "1.0.5"
d3-polygon "1.0.3"
d3-quadtree "1.0.3"
d3-queue "3.0.7"
d3-random "1.1.0"
d3-request "1.0.6"
d3-scale "1.0.7"
d3-selection "1.3.0"
d3-shape "1.2.0"
d3-time "1.0.8"
d3-time-format "2.1.1"
d3-timer "1.0.7"
d3-transition "1.1.1"
d3-voronoi "1.1.2"
d3-zoom "1.7.1"
dashdash@^1.12.0: dashdash@^1.12.0:
version "1.14.1" version "1.14.1"
resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0" resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0"
...@@ -1419,7 +1636,7 @@ https-proxy-agent@^2.1.0: ...@@ -1419,7 +1636,7 @@ https-proxy-agent@^2.1.0:
agent-base "^4.1.0" agent-base "^4.1.0"
debug "^3.1.0" debug "^3.1.0"
iconv-lite@0.4.19, iconv-lite@^0.4.17: iconv-lite@0.4, iconv-lite@0.4.19, iconv-lite@^0.4.17:
version "0.4.19" version "0.4.19"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"
...@@ -2454,7 +2671,7 @@ run-async@^2.2.0: ...@@ -2454,7 +2671,7 @@ run-async@^2.2.0:
dependencies: dependencies:
is-promise "^2.1.0" is-promise "^2.1.0"
rw@^1.3.2: rw@1, rw@^1.3.2:
version "1.3.3" version "1.3.3"
resolved "https://registry.yarnpkg.com/rw/-/rw-1.3.3.tgz#3f862dfa91ab766b14885ef4d01124bfda074fb4" resolved "https://registry.yarnpkg.com/rw/-/rw-1.3.3.tgz#3f862dfa91ab766b14885ef4d01124bfda074fb4"
...@@ -2996,6 +3213,10 @@ xml-char-classes@^1.0.0: ...@@ -2996,6 +3213,10 @@ xml-char-classes@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/xml-char-classes/-/xml-char-classes-1.0.0.tgz#64657848a20ffc5df583a42ad8a277b4512bbc4d" resolved "https://registry.yarnpkg.com/xml-char-classes/-/xml-char-classes-1.0.0.tgz#64657848a20ffc5df583a42ad8a277b4512bbc4d"
xmlhttprequest@1:
version "1.8.0"
resolved "https://registry.yarnpkg.com/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz#67fe075c5c24fef39f9d65f5f7b7fe75171968fc"
xtend@^4.0.0, xtend@~4.0.1: xtend@^4.0.0, xtend@~4.0.1:
version "4.0.1" version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment