Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color-by-string for Treemap/Sunburst #1415

Merged
merged 3 commits into from
May 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/perspective-viewer-d3fc/src/js/charts/sunburst.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {sunburstSeries} from "../series/sunburst/sunburstSeries";
import {tooltip} from "../tooltip/tooltip";
import {gridLayoutMultiChart} from "../layout/gridLayoutMultiChart";
import {colorRangeLegend} from "../legend/colorRangeLegend";
import {colorLegend} from "../legend/legend";

function sunburst(container, settings) {
if (settings.crossValues.length === 0) {
Expand All @@ -22,17 +23,22 @@ function sunburst(container, settings) {
}

const data = treeData(settings);
const color = treeColor(
settings,
data.map(d => d.extents)
);
const color = treeColor(settings, data);
const sunburstGrid = gridLayoutMultiChart().elementsPrefix("sunburst");

container.datum(data).call(sunburstGrid);

if (color) {
const legend = colorRangeLegend().scale(color);
container.call(legend);
const color_column = settings.realValues[1];
if (settings.mainValues.find(x => x.name === color_column)?.type === "string") {
const legend = colorLegend()
.settings(settings)
.scale(color);
container.call(legend);
} else {
const legend = colorRangeLegend().scale(color);
container.call(legend);
}
}

const sunburstContainer = sunburstGrid.chartContainer();
Expand All @@ -59,7 +65,7 @@ function sunburst(container, settings) {
const svgNode = this.parentNode;
const {width, height} = svgNode.getBoundingClientRect();

const radius = (Math.min(width, height) - 120) / 6;
const radius = (Math.min(width, height) - 24) / (settings.crossValues.length * 2);
sunburstSeries()
.settings(settings)
.split(split)
Expand Down
14 changes: 11 additions & 3 deletions packages/perspective-viewer-d3fc/src/js/charts/treemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {treemapSeries} from "../series/treemap/treemapSeries";
import {tooltip} from "../tooltip/tooltip";
import {gridLayoutMultiChart} from "../layout/gridLayoutMultiChart";
import {colorRangeLegend} from "../legend/colorRangeLegend";
import {colorLegend} from "../legend/legend";

function treemap(container, settings) {
if (settings.crossValues.length === 0) {
Expand All @@ -34,10 +35,17 @@ function treemap(container, settings) {

const treemapGrid = gridLayoutMultiChart().elementsPrefix("treemap");
container.datum(data).call(treemapGrid);

if (color) {
const legend = colorRangeLegend().scale(color);
container.call(legend);
const color_column = settings.realValues[1];
if (settings.mainValues.find(x => x.name === color_column)?.type === "string") {
const legend = colorLegend()
.settings(settings)
.scale(color);
container.call(legend);
} else {
const legend = colorRangeLegend().scale(color);
container.call(legend);
}
}

const treemapEnter = treemapGrid.chartEnter();
Expand Down
11 changes: 7 additions & 4 deletions packages/perspective-viewer-d3fc/src/js/data/treeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {toValue} from "../tooltip/selectionData";
export function treeData(settings) {
const sets = {};
const real_aggs = settings.realValues.map(x => (x === null ? null : settings.mainValues.find(y => y.name === x)));
settings.data.forEach(d => {
settings.data.forEach((d, j) => {
const groups = d.__ROW_PATH__;
const splits = getSplitNames(d);
splits.forEach(split => {
Expand All @@ -29,8 +29,11 @@ export function treeData(settings) {
currentLevel.push(element);
}
if (settings.realValues.length > 1 && settings.realValues[1] !== null) {
const colorValue = getDataValue(d, settings.mainValues[1], split);
element.color = element.color ? element.color + colorValue : colorValue;
const is_leaf = i === groups.length - 1;
const colorValue = is_leaf ? getDataValue(d, settings.mainValues[1], split) : getDataValue(settings.agg_paths[j][i + 1] || d, settings.mainValues[1], split);
if (colorValue) {
element.color = colorValue;
}
}
if (settings.realValues.length > 2 && settings.realValues[2] !== null) {
element.tooltip = [];
Expand Down Expand Up @@ -59,7 +62,7 @@ export function treeData(settings) {
d.mainValues =
settings.realValues.length === 1 || (settings.realValues[1] === null && settings.realValues[2] === null)
? d.value
: [d.value, d.data.color].concat(d.data.tooltip || []).filter(x => x !== null);
: [d.value, d.data.color].concat(d.data.tooltip || []).filter(x => x !== undefined);
d.crossValue = d
.ancestors()
.slice(0, -1)
Expand Down
32 changes: 25 additions & 7 deletions packages/perspective-viewer-d3fc/src/js/plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ function drawChart(chart) {
return async function(el, view, task, end_col, end_row) {
let jsonp, metadata;
const realValues = JSON.parse(this.getAttribute("columns"));
const leaves_only = chart.plugin.type !== "d3_sunburst";
if (end_col && end_row) {
jsonp = view.to_json({end_row, end_col, leaves_only: true});
jsonp = view.to_json({end_row, end_col, leaves_only});
} else if (end_col) {
jsonp = view.to_json({end_col, leaves_only: true});
jsonp = view.to_json({end_col, leaves_only});
} else if (end_row) {
jsonp = view.to_json({end_row, leaves_only: true});
jsonp = view.to_json({end_row, leaves_only});
} else {
jsonp = view.to_json({leaves_only: true});
jsonp = view.to_json({leaves_only});
}
metadata = await Promise.all([this._table.schema(false), view.computed_schema(false), view.schema(false), jsonp, view.get_config()]);

Expand All @@ -74,17 +75,34 @@ function drawChart(chart) {
};

const {columns, row_pivots, column_pivots, filter} = config;
const filtered = row_pivots.length > 0 ? json.filter(col => col.__ROW_PATH__ && col.__ROW_PATH__.length == row_pivots.length) : json;
const filtered =
row_pivots.length > 0
? json.reduce(
(acc, col) => {
if (col.__ROW_PATH__ && col.__ROW_PATH__.length == row_pivots.length) {
acc.agg_paths.push(acc.aggs.slice());
acc.rows.push(col);
} else {
const len = col.__ROW_PATH__.filter(x => x !== undefined).length;
acc.aggs[len] = col;
acc.aggs = acc.aggs.slice(0, len + 1);
}
return acc;
},
{rows: [], aggs: [], agg_paths: []}
)
: {rows: json};
const dataMap = (col, i) => (!row_pivots.length ? {...col, __ROW_PATH__: [i]} : col);
const mapped = filtered.map(dataMap);
const mapped = filtered.rows.map(dataMap);

let settings = {
realValues,
crossValues: row_pivots.map(r => ({name: r, type: get_pivot_column_type(r)})),
mainValues: columns.map(a => ({name: a, type: view_schema[a]})),
splitValues: column_pivots.map(r => ({name: r, type: get_pivot_column_type(r)})),
filter,
data: mapped
data: mapped,
agg_paths: filtered.agg_paths
};

createOrUpdateChart.call(this, el, chart, settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export function seriesColors(settings) {
.domain(domain)();
}

export function seriesColorsFromDistinct(settings, data) {
// const col = settings.data && settings.data.length > 0 ? settings.data[0] : {};
let domain = Array.from(new Set(data));
return colorScale()
.settings(settings)
.domain(domain)();
}

export function seriesColorsFromGroups(settings) {
const col = settings.data && settings.data.length > 0 ? settings.data[0] : {};
const domain = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const drawArc = radius =>
.endAngle(d => d.x1)
.padAngle(d => Math.min((d.x1 - d.x0) / 2, 0.005))
.padRadius(radius)
.innerRadius(d => d.y0 * radius)
.outerRadius(d => Math.max(d.y0 * radius, d.y1 * radius - 1));
.innerRadius(d => Math.max(1, (d.y0 - 1) * radius))
.outerRadius(d => Math.max((d.y0 - 1) * radius, (d.y1 - 1) * radius - 1));

export const arcVisible = d => d.y1 <= 3 && d.y0 >= 1 && d.x1 > d.x0;
export const arcVisible = d => d.y0 >= 1 && d.x1 > d.x0;
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@

import {flattenExtent} from "../../axis/flatten";
import {seriesColorRange} from "../seriesRange";
import {seriesColorsFromDistinct} from "../seriesColors";

export function treeColor(settings, extents) {
export function treeColor(settings, data) {
if (settings.realValues.length > 1 && settings.realValues[1] !== null) {
return seriesColorRange(settings, null, null, flattenExtent(extents));
const color_column = settings.realValues[1];
if (settings.mainValues.find(x => x.name === color_column)?.type === "string") {
const colors = data
.map(d => d.data)
.filter(x => x.height > 0)
.map(x => getColors(x))
.reduce((a, b) => a.concat(b));
return seriesColorsFromDistinct(settings, colors);
} else {
return seriesColorRange(settings, null, null, flattenExtent(data.map(d => d.extents)));
}
}
}

// only get the colors from the bottom level (e.g. nodes with no children)
function getColors(nodes, colors = []) {
nodes.children && nodes.children.length > 0 ? nodes.children.forEach(child => colors.concat(getColors(child, colors))) : nodes.data.color && colors.push(nodes.data.color);
return colors;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const labelVisible = d => d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1

export function labelTransform(d, radius) {
const x = (((d.x0 + d.x1) / 2) * 180) / Math.PI;
const y = ((d.y0 + d.y1) / 2) * radius;
const y = ((d.y0 - 1 + (d.y1 - 1)) / 2) * radius;
return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
*/

import {seriesColorRange} from "../seriesRange";
import {seriesColorsFromDistinct} from "../seriesColors";

export function treeColor(settings, data) {
if (settings.realValues.length < 1 || settings.realValues[1] === null) return;
const color_column = settings.realValues[1];
const colors = data
.filter(x => x.height > 0)
.map(x => getColors(x))
.reduce((a, b) => a.concat(b));
let min = Math.min(...colors);
let max = Math.max(...colors);
return seriesColorRange(settings, null, null, [min, max]);
if (settings.mainValues.find(x => x.name === color_column)?.type === "string") {
return seriesColorsFromDistinct(settings, colors);
} else {
let min = Math.min(...colors);
let max = Math.max(...colors);
return seriesColorRange(settings, null, null, [min, max]);
}
}

// only get the colors from the bottom level (e.g. nodes with no children)
function getColors(nodes, colors = []) {
nodes.children && nodes.children.length > 0 ? nodes.children.forEach(child => colors.concat(getColors(child, colors))) : colors.push(nodes.data.color);
nodes.children && nodes.children.length > 0 ? nodes.children.forEach(child => colors.concat(getColors(child, colors))) : nodes.data.color && colors.push(nodes.data.color);
return colors;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ export function treemapSeries() {
.style("y", d => d.y0)
.style("width", d => calcWidth(d))
.style("height", d => calcHeight(d));
color && rects.style("fill", d => color(d.data.color));

color &&
rects.style("fill", d => {
if (d.data.color) {
return color(d.data.color);
}
});

const labels = nodesMerge
.filter(d => d.value !== 0)
Expand Down
9 changes: 8 additions & 1 deletion packages/perspective-viewer-d3fc/src/less/chart.less
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@
overflow: hidden;
}

.sunburst-container:not(:only-child) svg {
transform: translate(0, -27px);
}

& svg {
width: 100%;
height: 100%;
transform: translate(0, -27px);
}

& path {
Expand Down Expand Up @@ -414,6 +417,10 @@
height: 200px;
}

.d3_treemap .legend-container:not(.legend-color) {
width: 100px;
}

.zoom-controls {
position: absolute;
top: 10px;
Expand Down
25 changes: 15 additions & 10 deletions packages/perspective-viewer-d3fc/test/js/unit/data/testTreeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ export const data = [
}
];

const item = {
__ROW_PATH__: ["East"],
Quantity: 4156
};

export const agg_paths = [
[item, item, item],
[item, item, item],
[item, item, item],
[item, item, item],
[item, item, item],
[item, item, item]
];

export const splitData = [
{
__ROW_PATH__: ["Central", "Furniture"],
Expand Down Expand Up @@ -101,16 +115,7 @@ export const mainValues = [
}
];

export const realValues = [
{
name: "Sales",
type: "float"
},
{
name: "Quantity",
type: "integer"
}
];
export const realValues = ["Sales", "Quantity"];

export const crossValues = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
*/

import {treeData} from "../../../../src/js/data/treeData";
import {data, splitData, mainValues, crossValues, realValues} from "./testTreeData";
import {data, splitData, mainValues, crossValues, realValues, agg_paths} from "./testTreeData";

describe("treeData should", () => {
test("create a structure with the right number of levels", () => {
const {data: result} = treeData({data, mainValues, crossValues, realValues})[0];
const {data: result} = treeData({data, agg_paths, mainValues, crossValues, realValues})[0];
expect(result.height).toEqual(2);
});

test("calculate the correct color extents", () => {
const {extents} = treeData({data, mainValues, crossValues, realValues})[0];
const {extents} = treeData({data, agg_paths, mainValues, crossValues, realValues})[0];
expect(extents).toEqual([1544, 4156]);
});

test("produce tree data for each split", () => {
const result = treeData({data: splitData, mainValues, crossValues, realValues});
const result = treeData({data: splitData, agg_paths, mainValues, crossValues, realValues});
expect(result.length).toEqual(4);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe("tooltip generateHTML should", () => {
settings = {
crossValues: [],
splitValues: [],
mainValues: [{name: "main-1", type: "integer"}]
mainValues: [{name: "main-1", type: "integer"}],
realValues: ["main-1"]
};
});
afterEach(() => {
Expand All @@ -48,6 +49,7 @@ describe("tooltip generateHTML should", () => {

test("show multiple mainValues", () => {
settings.mainValues.push({name: "main-2", type: "float"});
settings.realValues.push("main-2");
const data = {
mainValues: [101, 202.22]
};
Expand Down