-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #564 from redbearsam/feature/dual-y-axis
Feature/dual y axis POC
- Loading branch information
Showing
8 changed files
with
342 additions
and
23 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
packages/perspective-viewer-d3fc/src/js/axis/axisSplitter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/****************************************************************************** | ||
* | ||
* Copyright (c) 2017, the Perspective Authors. | ||
* | ||
* This file is part of the Perspective library, distributed under the terms of | ||
* the Apache License 2.0. The full license can be found in the LICENSE file. | ||
* | ||
*/ | ||
import {splitterLabels} from "./splitterLabels"; | ||
|
||
export const axisSplitter = (settings, sourceData) => { | ||
let color; | ||
|
||
// splitMainValues is an array of main-value names to put into the alt-axis | ||
const splitMainValues = settings.splitMainValues || []; | ||
const altValue = name => { | ||
const split = name.split("|"); | ||
return splitMainValues.includes(split[split.length - 1]); | ||
}; | ||
|
||
const haveSplit = settings["mainValues"].some(m => altValue(m.name)); | ||
|
||
// Split the data into main and alt displays | ||
const data = haveSplit ? sourceData.map(d => d.filter(v => !altValue(v.key))) : sourceData; | ||
const altData = haveSplit ? sourceData.map(d => d.filter(v => altValue(v.key))) : null; | ||
|
||
// Renderer to show the special controls for moving between axes | ||
const splitter = selection => { | ||
if (settings["mainValues"].length === 1) return; | ||
|
||
const labelsInfo = settings["mainValues"].map((v, i) => ({ | ||
index: i, | ||
name: v.name | ||
})); | ||
const mainLabels = labelsInfo.filter(v => !altValue(v.name)); | ||
const altLabels = labelsInfo.filter(v => altValue(v.name)); | ||
|
||
const labeller = () => splitterLabels(settings).color(color); | ||
|
||
selection.select(".y-label-container>.y-label").call(labeller().labels(mainLabels)); | ||
selection.select(".y2-label-container>.y-label").call( | ||
labeller() | ||
.labels(altLabels) | ||
.alt(true) | ||
); | ||
}; | ||
|
||
splitter.color = (...args) => { | ||
if (!args.length) { | ||
return color; | ||
} | ||
color = args[0]; | ||
return splitter; | ||
}; | ||
|
||
splitter.haveSplit = () => haveSplit; | ||
splitter.data = () => data; | ||
splitter.altData = () => altData; | ||
|
||
return splitter; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/perspective-viewer-d3fc/src/js/axis/splitterLabels.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/****************************************************************************** | ||
* | ||
* Copyright (c) 2017, the Perspective Authors. | ||
* | ||
* This file is part of the Perspective library, distributed under the terms of | ||
* the Apache License 2.0. The full license can be found in the LICENSE file. | ||
* | ||
*/ | ||
import * as fc from "d3fc"; | ||
import {getChartElement} from "../plugin/root"; | ||
import {withoutOpacity} from "../series/seriesColors.js"; | ||
|
||
// Render a set of labels with the little left/right arrows for moving between axes | ||
export const splitterLabels = settings => { | ||
let labels = []; | ||
let alt = false; | ||
let color; | ||
|
||
const _render = selection => { | ||
selection.text(""); | ||
|
||
const labelDataJoin = fc.dataJoin("span", "splitter-label").key(d => d); | ||
|
||
const disabled = !alt && labels.length === 1; | ||
const coloured = color && settings.splitValues.length === 0; | ||
labelDataJoin(selection, labels) | ||
.classed("disabled", disabled) | ||
.text(d => d.name) | ||
.style("color", d => (coloured ? withoutOpacity(color(d.name)) : undefined)) | ||
.on("click", d => { | ||
if (disabled) return; | ||
|
||
if (alt) { | ||
settings.splitMainValues = settings.splitMainValues.filter(v => v != d.name); | ||
} else { | ||
settings.splitMainValues = [d.name].concat(settings.splitMainValues || []); | ||
} | ||
|
||
redrawChart(selection); | ||
}); | ||
}; | ||
|
||
const redrawChart = selection => { | ||
const chartElement = getChartElement(selection.node()); | ||
chartElement.remove(); | ||
chartElement.draw(); | ||
}; | ||
|
||
_render.labels = (...args) => { | ||
if (!args.length) { | ||
return labels; | ||
} | ||
labels = args[0]; | ||
return _render; | ||
}; | ||
_render.alt = (...args) => { | ||
if (!args.length) { | ||
return alt; | ||
} | ||
alt = args[0]; | ||
return _render; | ||
}; | ||
|
||
_render.color = (...args) => { | ||
if (!args.length) { | ||
return color; | ||
} | ||
color = args[0]; | ||
return _render; | ||
}; | ||
return _render; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.