This repository was archived by the owner on Jun 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
WIP: basic marimekko #1011
Draft
adrianmroz-allegro
wants to merge
6
commits into
master
Choose a base branch
from
marimekko
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP: basic marimekko #1011
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
35593e4
WIP: basic marimekko
adrianmroz-allegro 403557e
Due to changes in cube-view around chartStage. Maybe we should change…
adrianmroz-allegro 8941b99
WIP: use plywoodKeys instead of generic variable names
adrianmroz-allegro 98cc98b
WIP: divs everywhere!
adrianmroz-allegro 4205505
WIP: adaptive text color
adrianmroz-allegro 89517cf
WIP: lint fixes
adrianmroz-allegro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,27 @@ | ||
| /* | ||
| * Copyright 2017-2022 Allegro.pl | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| @import '../../imports'; | ||
|
|
||
| .marimekko .absolute { | ||
| @include pin-full; | ||
| } | ||
|
|
||
| .marimekko .rect { | ||
| @include ellipsis; | ||
| padding: 8px; | ||
| border: 2px solid #fff; | ||
| } |
This file contains hidden or 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,209 @@ | ||
| /* | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is as WIP as it gets. |
||
| * Copyright 2017-2022 Allegro.pl | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as d3 from "d3"; | ||
| import { sum } from "d3"; | ||
| import { Dataset, Datum } from "plywood"; | ||
| import React, { CSSProperties } from "react"; | ||
| import { ChartProps } from "../../../common/models/chart-props/chart-props"; | ||
| import { findDimensionByName } from "../../../common/models/dimension/dimensions"; | ||
| import { Essence } from "../../../common/models/essence/essence"; | ||
| import { percentFormatter } from "../../../common/models/series/series-format"; | ||
| import { Stage } from "../../../common/models/stage/stage"; | ||
| import { flatMap } from "../../../common/utils/functional/functional"; | ||
| import { mapValues } from "../../../common/utils/object/object"; | ||
| import makeQuery from "../../../common/utils/query/visualization-query"; | ||
| import { LegendSpot } from "../../components/pinboard-panel/pinboard-panel"; | ||
| import { selectFirstSplitDatums, selectSplitDatums } from "../../utils/dataset/selectors/selectors"; | ||
| import { | ||
| ChartPanel, | ||
| DefaultVisualizationControls, | ||
| VisualizationProps | ||
| } from "../../views/cube-view/center-panel/center-panel"; | ||
| import { useSettingsContext } from "../../views/cube-view/settings-context"; | ||
| import { Legend } from "../line-chart/legend/legend"; | ||
| import "./marimekko.scss"; | ||
|
|
||
| function prepareData(data: Dataset, essence: Essence) { | ||
| const series = essence.getConcreteSeries().first(); | ||
| const xSplit = essence.splits.getSplit(1); | ||
|
|
||
| const ySplit = essence.splits.getSplit(0); | ||
|
|
||
| const dataset = selectFirstSplitDatums(data); | ||
|
|
||
| const baseYs = dataset.map(datum => ySplit.selectValue(datum)); | ||
|
|
||
| const xs: Record<string, Datum[]> = {}; | ||
|
|
||
| dataset.forEach(datum => { | ||
| const nested = selectSplitDatums(datum); | ||
| const yValue = ySplit.selectValue(datum); | ||
| const y = { | ||
| [ySplit.reference]: yValue | ||
| }; | ||
| nested.forEach(splitDatum => { | ||
| const x = String(xSplit.selectValue(splitDatum)); | ||
| if (xs[x] === undefined) { | ||
| xs[x] = []; | ||
| } | ||
| xs[x].push({ ...splitDatum, ...y }); | ||
| }); | ||
| }); | ||
|
|
||
| const xs2 = mapValues(xs, (data, key): Datum => { | ||
| const measure = { | ||
| [series.plywoodKey()]: d3.sum(data, datum => series.selectValue(datum)) | ||
| }; | ||
| const x = { | ||
| [xSplit.toKey()]: key | ||
| }; | ||
| return { | ||
| ...x, | ||
| ...measure, | ||
| nest: Dataset.fromJS(data) | ||
| }; | ||
| }); | ||
|
|
||
| function stackYs(ys: Datum[]): Datum[] { | ||
| const sorted = flatMap(baseYs, y => { | ||
| const found = ys.find(datum => ySplit.selectValue(datum) === y); | ||
| return found ? [found] : []; | ||
| }); | ||
| return sorted.map((datum, index, coll) => { | ||
| const y0 = sum(coll.slice(0, index), datum => series.selectValue(datum)); | ||
|
|
||
| return { | ||
| ...datum, | ||
| y0 | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| const xs3 = Object.values(xs2) | ||
| .sort((a, b) => series.selectValue(b) - series.selectValue(a)) | ||
| .map((datum, index, coll) => { | ||
| const x0 = sum(coll.slice(0, index), d => series.selectValue(d)); | ||
| const stackedNest = stackYs((datum.nest as Dataset).data); | ||
| return { | ||
| ...datum, | ||
| x0, | ||
| nest: Dataset.fromJS(stackedNest) | ||
| }; | ||
| }); | ||
|
|
||
| return xs3; | ||
| } | ||
|
|
||
| const X_AXIS_HEIGHT = 30; | ||
|
|
||
| const Marimekko: React.FunctionComponent<ChartProps> = props => { | ||
| const { stage, data: dataset, essence } = props; | ||
| const { dataCube: { dimensions } } = essence; | ||
| const { customization } = useSettingsContext(); | ||
| const colors = customization.visualizationColors.series; | ||
| const chartStage = new Stage({ | ||
| x: 10, | ||
| y: 20, | ||
| height: stage.height - 30, | ||
| width: stage.width - 20 | ||
| }); | ||
|
|
||
| const series = essence.getConcreteSeries().first(); | ||
|
|
||
| const ySplit = essence.splits.getSplit(0); | ||
| const xSplit = essence.splits.getSplit(1); | ||
| const yDimension = findDimensionByName(dimensions, ySplit.reference); | ||
| const colorValues = selectFirstSplitDatums(dataset).map(datum => String(ySplit.selectValue(datum))); | ||
|
|
||
| const colorScale = d3.scaleOrdinal<string>() | ||
| .range(colors) | ||
| .domain(colorValues); | ||
|
|
||
| const data = prepareData(dataset, essence); | ||
|
|
||
| const total = sum(data, datum => series.selectValue(datum)); | ||
| const xScale = d3.scaleLinear() | ||
| .range([0, chartStage.width]) | ||
| .domain([0, total]); | ||
|
|
||
| const stackHeight = chartStage.height - X_AXIS_HEIGHT; | ||
|
|
||
| return <> | ||
| <LegendSpot> | ||
| <Legend values={colorValues} title={ySplit.getTitle(yDimension)}/> | ||
| </LegendSpot> | ||
| <div className="absolute" style={chartStage.getLeftTop()}> | ||
| {data.map(datum => { | ||
| const x = series.selectValue(datum); | ||
| const x0 = datum.x0 as number; | ||
| const name = String(xSplit.selectValue(datum)); | ||
| const left = xScale(x0); | ||
| const width = xScale(x); | ||
| const ys = (datum.nest as Dataset).data; | ||
|
|
||
| const yScale = d3.scaleLinear() | ||
| .range([0, stackHeight]) | ||
| .domain([0, x]); | ||
|
|
||
| return ( | ||
| <div className="absolute" style={{ left, width }} key={name}> | ||
| <span> | ||
| {name}: {series.formatter()(x)} ({percentFormatter(x / total)}) | ||
| </span> | ||
| <div className="absolute" style={{ top: X_AXIS_HEIGHT }}> | ||
| {ys.map(datum => { | ||
| const y = series.selectValue(datum); | ||
| const y0 = datum.y0 as number; | ||
| const name = String(ySplit.selectValue(datum)); | ||
|
|
||
| const top = yScale(y0); | ||
| const height = yScale(y); | ||
| const width = xScale(x); | ||
|
|
||
| const backgroundColor = colorScale(name); | ||
| const textColor = d3.hsl(backgroundColor).l > 0.5 ? "#000" : "#fff"; | ||
|
|
||
| const styles: CSSProperties = { | ||
| top, | ||
| height, | ||
| width, | ||
| backgroundColor, | ||
| color: textColor | ||
| }; | ||
|
|
||
| return <div | ||
| key={name} | ||
| className="absolute rect" | ||
| style={styles} | ||
| > | ||
| <span>{name}: {series.formatter()(y)} ({percentFormatter(y / x)})</span> | ||
| </div>; | ||
| })} | ||
| </div> | ||
| </div> | ||
| ); | ||
| })} | ||
| </div> | ||
| </>; | ||
| }; | ||
|
|
||
| export default function marimekkoVisualization(props: VisualizationProps) { | ||
| return <React.Fragment> | ||
| <DefaultVisualizationControls {...props} /> | ||
| <ChartPanel {...props} queryFactory={makeQuery} chartComponent={Marimekko}/> | ||
| </React.Fragment>; | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ import { LinearScale } from "../../../utils/linear-scale/linear-scale"; | |
| const MARGIN = 40; | ||
| const X_AXIS_HEIGHT = 50; | ||
| const Y_AXIS_WIDTH = 50; | ||
| const BREAKPOINT_SMALL = 768; | ||
| const BREAKPOINT_SMALL = 748; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we change it here? Or adjust e2e test for Large Screen? |
||
| const TICK_COUNT = 10; | ||
| const EXTENT_EXTEND_FACTOR = 0.05; | ||
| const X_AXIS_LABEL_OFFSET = 55; | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { assoc, Unary } from "../functional/functional"; | ||
| import { assoc, Binary, Unary } from "../functional/functional"; | ||
| import { isTruthy } from "../general/general"; | ||
|
|
||
| export function extend(source: any, target: any): any { | ||
|
|
@@ -37,9 +37,9 @@ export function omitFalsyValues<T>(obj: T): Partial<T> { | |
|
|
||
| type Key = string; | ||
|
|
||
| export function mapValues<K extends Key, S, T>(obj: Record<K, S>, fn: Unary<S, T>): Record<K, T> { | ||
| export function mapValues<K extends Key, S, T>(obj: Record<K, S>, fn: Binary<S, K, T>): Record<K, T> { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add unit test for added keys parameter |
||
| return Object.keys(obj).reduce((result: Record<K, T>, key: K) => { | ||
| result[key] = fn(obj[key]); | ||
| result[key] = fn(obj[key], key); | ||
| return result; | ||
| }, {} as Record<K, T>); | ||
| } | ||
|
|
||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, somehow we should resolve this. Right now visualisations have bigger stage than is available.