Add test for layout.treemap.
Woot! Caught another bug in propagation of NaN node values.
This commit is contained in:
Родитель
f25c469d3d
Коммит
67ab76bcc1
|
@ -1103,7 +1103,7 @@ d3.layout.hierarchy = function() {
|
|||
if (sort) c.sort(sort);
|
||||
if (value) node.value = v;
|
||||
} else if (value) {
|
||||
node.value = value.call(hierarchy, data, depth);
|
||||
node.value = +value.call(hierarchy, data, depth) || 0;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
@ -1118,7 +1118,7 @@ d3.layout.hierarchy = function() {
|
|||
j = depth + 1;
|
||||
while (++i < n) v += revalue(children[i], j);
|
||||
} else if (value) {
|
||||
v = value.call(hierarchy, d3_layout_hierarchyInline ? node : node.data, depth);
|
||||
v = +value.call(hierarchy, d3_layout_hierarchyInline ? node : node.data, depth) || 0;
|
||||
}
|
||||
if (value) node.value = v;
|
||||
return v;
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -25,7 +25,7 @@ d3.layout.hierarchy = function() {
|
|||
if (sort) c.sort(sort);
|
||||
if (value) node.value = v;
|
||||
} else if (value) {
|
||||
node.value = value.call(hierarchy, data, depth);
|
||||
node.value = +value.call(hierarchy, data, depth) || 0;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ d3.layout.hierarchy = function() {
|
|||
j = depth + 1;
|
||||
while (++i < n) v += revalue(children[i], j);
|
||||
} else if (value) {
|
||||
v = value.call(hierarchy, d3_layout_hierarchyInline ? node : node.data, depth);
|
||||
v = +value.call(hierarchy, d3_layout_hierarchyInline ? node : node.data, depth) || 0;
|
||||
}
|
||||
if (value) node.value = v;
|
||||
return v;
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
require("./../../lib/env-js/envjs/node");
|
||||
require("./../../d3");
|
||||
require("./../../d3.layout");
|
||||
|
||||
var treemap = d3.layout.treemap();
|
||||
|
||||
console.log("zero, negative and NaN values:");
|
||||
console.log(" {value: 0} ", treemap.nodes({value: 0})[0].area);
|
||||
console.log(" {value: -1} ", treemap.nodes({value: -1})[0].area);
|
||||
console.log(" {value: NaN} ", treemap.nodes({value: NaN})[0].area);
|
||||
console.log("");
|
||||
|
||||
treemap.size([0, 0]);
|
||||
console.log("size([0, 0]):");
|
||||
console.log(" {value: 0} ", treemap.nodes({value: 0})[0].area);
|
||||
console.log(" {value: -1} ", treemap.nodes({value: -1})[0].area);
|
||||
console.log(" {value: NaN} ", treemap.nodes({value: NaN})[0].area);
|
||||
console.log(" {value: 1} ", treemap.nodes({value: 1})[0].area);
|
||||
console.log("");
|
||||
|
||||
treemap.size([-1, -1]);
|
||||
console.log("size([-1, -1]):");
|
||||
console.log(" {value: 0} ", treemap.nodes({value: 0})[0].area);
|
||||
console.log(" {value: -1} ", treemap.nodes({value: -1})[0].area);
|
||||
console.log(" {value: NaN} ", treemap.nodes({value: NaN})[0].area);
|
||||
console.log(" {value: 1} ", treemap.nodes({value: 1})[0].area);
|
||||
console.log("");
|
||||
|
||||
treemap.size([-1, 1]);
|
||||
console.log("size([-1, 1]):");
|
||||
console.log(" {value: 0} ", treemap.nodes({value: 0})[0].area);
|
||||
console.log(" {value: -1} ", treemap.nodes({value: -1})[0].area);
|
||||
console.log(" {value: NaN} ", treemap.nodes({value: NaN})[0].area);
|
||||
console.log(" {value: 1} ", treemap.nodes({value: 1})[0].area);
|
||||
console.log("");
|
||||
|
||||
treemap.size([NaN, NaN]);
|
||||
console.log("size([NaN, NaN]):");
|
||||
console.log(" {value: 0} ", treemap.nodes({value: 0})[0].area);
|
||||
console.log(" {value: -1} ", treemap.nodes({value: -1})[0].area);
|
||||
console.log(" {value: NaN} ", treemap.nodes({value: NaN})[0].area);
|
||||
console.log(" {value: 1} ", treemap.nodes({value: 1})[0].area);
|
||||
console.log("");
|
||||
|
||||
treemap.size([10, 10]);
|
||||
var tree = {children: [{value: 1}]};
|
||||
|
||||
console.log("padding:");
|
||||
console.log(" default ", log(treemap.nodes(tree)[1]));
|
||||
console.log(" 1 ", log(treemap.padding(1).nodes(tree)[1]));
|
||||
console.log(" 10 ", log(treemap.padding(10).nodes(tree)[1]));
|
||||
console.log(" [1, 1, 1, 1] ", log(treemap.padding([1, 1, 1, 1]).nodes(tree)[1]));
|
||||
console.log(" [10, 1, 10, 1] ", log(treemap.padding([10, 1, 10, 1]).nodes(tree)[1]));
|
||||
console.log(" [1, 10, 1, 10] ", log(treemap.padding([1, 10, 1, 10]).nodes(tree)[1]));
|
||||
console.log(" function(x) 1 ", log(treemap.padding(function(x) { return 1; }).nodes(tree)[1]));
|
||||
console.log(" function(x) 10 ", log(treemap.padding(function(x) { return 10; }).nodes(tree)[1]));
|
||||
console.log(" function(x) [1, 1, 1, 1] ", log(treemap.padding(function(x) { return [1, 1, 1, 1]; }).nodes(tree)[1]));
|
||||
console.log(" function(x) [1, 10, 1, 1]", log(treemap.padding(function(x) { return [1, 10, 1, 1]; }).nodes(tree)[1]));
|
||||
console.log(" function(x) null ", log(treemap.padding(function(x) { return null; }).nodes(tree)[1]));
|
||||
console.log(" null ", log(treemap.padding(null).nodes(tree)[1]));
|
||||
console.log("");
|
||||
|
||||
treemap.size([1000, 1000]);
|
||||
var tree = {children: [{value: 2}, {value: 260}, {value: 180}, {value: 2}, {value: 1}, {value: 0}]};
|
||||
console.log("zero-sized node:");
|
||||
treemap.nodes(tree).forEach(function(node) { console.log(new Array(node.depth + 2).join(" ") + log(node)); });
|
||||
console.log("");
|
||||
|
||||
function log(node) {
|
||||
return node.x + "," + node.y + "," + node.dx + "," + node.dy;
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
zero, negative and NaN values:
|
||||
{value: 0} 0
|
||||
{value: -1} 0
|
||||
{value: NaN} 0
|
||||
|
||||
size([0, 0]):
|
||||
{value: 0} 0
|
||||
{value: -1} 0
|
||||
{value: NaN} 0
|
||||
{value: 1} 0
|
||||
|
||||
size([-1, -1]):
|
||||
{value: 0} 0
|
||||
{value: -1} 0
|
||||
{value: NaN} 0
|
||||
{value: 1} 1
|
||||
|
||||
size([-1, 1]):
|
||||
{value: 0} 0
|
||||
{value: -1} 0
|
||||
{value: NaN} 0
|
||||
{value: 1} 0
|
||||
|
||||
size([NaN, NaN]):
|
||||
{value: 0} 0
|
||||
{value: -1} 0
|
||||
{value: NaN} 0
|
||||
{value: 1} 0
|
||||
|
||||
padding:
|
||||
default 0,0,10,10
|
||||
1 1,1,8,8
|
||||
10 5,5,0,0
|
||||
[1, 1, 1, 1] 1,1,8,8
|
||||
[10, 1, 10, 1] 1,5,8,0
|
||||
[1, 10, 1, 10] 5,1,0,8
|
||||
function(x) 1 1,1,8,8
|
||||
function(x) 10 5,5,0,0
|
||||
function(x) [1, 1, 1, 1] 1,1,8,8
|
||||
function(x) [1, 10, 1, 1] 0.5,1,0,8
|
||||
function(x) null 0,0,10,10
|
||||
null 0,0,10,10
|
||||
|
||||
zero-sized node:
|
||||
0,0,1000,1000
|
||||
16,0,11,416
|
||||
0,416,1000,584
|
||||
27,0,973,416
|
||||
5,0,11,416
|
||||
0,0,5,416
|
||||
0,0,0,416
|
||||
|
|
@ -0,0 +1,150 @@
|
|||
require("../env");
|
||||
require("../../d3");
|
||||
require("../../d3.layout");
|
||||
|
||||
var vows = require("vows"),
|
||||
assert = require("assert");
|
||||
|
||||
var suite = vows.describe("d3.layout.treemap");
|
||||
|
||||
suite.addBatch({
|
||||
"treemap": {
|
||||
topic: function() {
|
||||
return d3.layout.treemap;
|
||||
},
|
||||
"outputs a squarified treemap": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]).sort(null);
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 0, y: 833, dx: 1000, dy: 167},
|
||||
{x: 600, y: 0, dx: 400, dy: 833},
|
||||
{x: 0, y: 0, dx: 600, dy: 833},
|
||||
{x: 0, y: 555, dx: 600, dy: 278},
|
||||
{x: 0, y: 0, dx: 600, dy: 555}
|
||||
]);
|
||||
},
|
||||
"sorts by value by default": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]);
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 0, y: 0, dx: 333, dy: 500},
|
||||
{x: 333, y: 0, dx: 667, dy: 500},
|
||||
{x: 0, y: 500, dx: 1000, dy: 500},
|
||||
{x: 0, y: 500, dx: 333, dy: 500},
|
||||
{x: 333, y: 500, dx: 667, dy: 500}
|
||||
]);
|
||||
},
|
||||
"ignores zero values": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]).sort(null);
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 0}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 0, y: 833, dx: 1000, dy: 167},
|
||||
{x: 1000, y: 0, dx: 0, dy: 833},
|
||||
{x: 600, y: 0, dx: 400, dy: 833},
|
||||
{x: 0, y: 0, dx: 600, dy: 833},
|
||||
{x: 0, y: 555, dx: 600, dy: 278},
|
||||
{x: 0, y: 0, dx: 600, dy: 555}
|
||||
]);
|
||||
},
|
||||
"ignores NaN values": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]).sort(null);
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: NaN}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 0, y: 833, dx: 1000, dy: 167},
|
||||
{x: 1000, y: 0, dx: 0, dy: 833},
|
||||
{x: 600, y: 0, dx: 400, dy: 833},
|
||||
{x: 0, y: 0, dx: 600, dy: 833},
|
||||
{x: 0, y: 555, dx: 600, dy: 278},
|
||||
{x: 0, y: 0, dx: 600, dy: 555}
|
||||
]);
|
||||
},
|
||||
"does not overflow empty size": function(treemap) {
|
||||
var t = treemap().size([0, 0]).sort(null);
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 0, dy: 0},
|
||||
{x: 0, y: 0, dx: 0, dy: 0},
|
||||
{x: 0, y: 0, dx: 0, dy: 0},
|
||||
{x: 0, y: 0, dx: 0, dy: 0},
|
||||
{x: 0, y: 0, dx: 0, dy: 0},
|
||||
{x: 0, y: 0, dx: 0, dy: 0}
|
||||
]);
|
||||
},
|
||||
"can specify padding as a number": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]).sort(null).padding(1);
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 1, y: 833, dx: 998, dy: 166},
|
||||
{x: 600, y: 1, dx: 399, dy: 832},
|
||||
{x: 1, y: 1, dx: 599, dy: 832},
|
||||
{x: 2, y: 555, dx: 597, dy: 277},
|
||||
{x: 2, y: 2, dx: 597, dy: 553}
|
||||
]);
|
||||
},
|
||||
"can specify padding as an array": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]).sort(null).padding([1,2,3,4]);
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 4, y: 831, dx: 994, dy: 166},
|
||||
{x: 600, y: 1, dx: 398, dy: 830},
|
||||
{x: 4, y: 1, dx: 596, dy: 830},
|
||||
{x: 8, y: 553, dx: 590, dy: 275},
|
||||
{x: 8, y: 2, dx: 590, dy: 551}
|
||||
]);
|
||||
},
|
||||
"can specify padding as null": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]).sort(null).padding(null);
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 0, y: 833, dx: 1000, dy: 167},
|
||||
{x: 600, y: 0, dx: 400, dy: 833},
|
||||
{x: 0, y: 0, dx: 600, dy: 833},
|
||||
{x: 0, y: 555, dx: 600, dy: 278},
|
||||
{x: 0, y: 0, dx: 600, dy: 555}
|
||||
]);
|
||||
},
|
||||
"can specify padding as a function that returns a number": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]).sort(null).padding(function(d) { return d.depth; });
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 0, y: 833, dx: 1000, dy: 167},
|
||||
{x: 600, y: 0, dx: 400, dy: 833},
|
||||
{x: 0, y: 0, dx: 600, dy: 833},
|
||||
{x: 1, y: 555, dx: 598, dy: 277},
|
||||
{x: 1, y: 1, dx: 598, dy: 554}
|
||||
]);
|
||||
},
|
||||
"can specify padding as a function that returns an array": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]).sort(null).padding(function(d) { return [d.depth,2,3,4]; });
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 4, y: 831, dx: 994, dy: 166},
|
||||
{x: 600, y: 0, dx: 398, dy: 831},
|
||||
{x: 4, y: 0, dx: 596, dy: 831},
|
||||
{x: 8, y: 552, dx: 590, dy: 276},
|
||||
{x: 8, y: 1, dx: 590, dy: 551}
|
||||
]);
|
||||
},
|
||||
"can specify padding as a function that returns null": function(treemap) {
|
||||
var t = treemap().size([1000, 1000]).sort(null).padding(function(d) { return d.depth & 1 ? null : 1; });
|
||||
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
|
||||
{x: 0, y: 0, dx: 1000, dy: 1000},
|
||||
{x: 1, y: 833, dx: 998, dy: 166},
|
||||
{x: 600, y: 1, dx: 399, dy: 832},
|
||||
{x: 1, y: 1, dx: 599, dy: 832},
|
||||
{x: 1, y: 556, dx: 599, dy: 277},
|
||||
{x: 1, y: 1, dx: 599, dy: 555}
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function layout(node) {
|
||||
return {
|
||||
x: node.x,
|
||||
y: node.y,
|
||||
dx: node.dx,
|
||||
dy: node.dy
|
||||
};
|
||||
}
|
||||
|
||||
suite.export(module);
|
Загрузка…
Ссылка в новой задаче