Skip to content

Commit

Permalink
refactor: improved code readibility
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Aug 16, 2021
1 parent f19f14b commit 4228efc
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 57 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function shapeViewModel(
chartDimensions: Dimensions,
heatmapTable: HeatmapTable,
colorScale: ColorScaleType['scale'],
visibilityFilterRanges: Array<[number, number]>,
bandsToHide: Array<[number, number]>,
{ height, pageSize }: GridHeightParams,
): ShapeViewModel {
const gridStrokeWidth = config.grid.stroke.width ?? 1;
Expand Down Expand Up @@ -208,7 +208,7 @@ export function shapeViewModel(
width: config.cell.border.strokeWidth,
},
value: d.value,
visible: !isValueVisible(d.value, visibilityFilterRanges),
visible: !isValueHidden(d.value, bandsToHide),
formatted: spec.valueFormatter(d.value),
};
return acc;
Expand Down Expand Up @@ -391,6 +391,6 @@ function getCellKey(x: NonNullable<PrimitiveValue>, y: NonNullable<PrimitiveValu
return [String(x), String(y)].join('&_&');
}

function isValueVisible(value: number, visibilityFilterRanges: Array<[number, number]>) {
return visibilityFilterRanges.some(([min, max]) => min <= value && value < max);
function isValueHidden(value: number, rangesToHide: Array<[number, number]>) {
return rangesToHide.some(([min, max]) => min <= value && value < max);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ const EMPTY_LEGEND: LegendItem[] = [];
/** @internal */
export const computeLegendSelector = createCustomCachedSelector(
[getSpecOrNull, getColorScale, getDeselectedSeriesSelector],
(spec, { bands, scale }, deselectedDataSeries): LegendItem[] => {
(spec, { bands }, deselectedDataSeries): LegendItem[] => {
if (spec === null) {
return EMPTY_LEGEND;
}

return bands.map(({ start, formattedStart }, i) => {
const color = scale(start);
const seriesIdentifier = {
key: String(start),
specId: String(start),
};

return bands.map(({ label, color }) => {
return {
// the band label is considered unique by construction
seriesIdentifiers: [
{
key: label,
specId: label,
},
],
color,
label: `${i === 0 ? '≥' : '>'} ${formattedStart}`,
seriesIdentifiers: [seriesIdentifier],
isSeriesHidden: deselectedDataSeries.some((dataSeries) => dataSeries.key === seriesIdentifier.key),
label,
isSeriesHidden: deselectedDataSeries.some((dataSeries) => dataSeries.key === label),
isToggleable: true,
path: [{ index: 0, value: seriesIdentifier.key }],
path: [{ index: 0, value: label }],
keys: [],
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,21 @@ export const geometries = createCustomCachedSelector(
deselectedSeries,
gridHeightParams,
): ShapeViewModel => {
const deselectedRanges = new Set(
// instead of using the specId, each legend item is associated with an unique band label
const disabledBandLabels = new Set(
deselectedSeries.map(({ specId }) => {
return Number(specId);
return specId;
}),
);
const visibilityFilterRanges = bands.reduce<Array<[number, number]>>((acc, { start }, i) => {
if (deselectedRanges.has(start)) {
const rangeEnd = bands.length === i + 1 ? Infinity : bands[i + 1].start;
acc.push([start, rangeEnd]);
}
return acc;
}, []);

const bandsToHide: Array<[number, number]> = bands
.filter(({ label }) => {
return disabledBandLabels.has(label);
})
.map(({ start, end }) => [start, end]);

return heatmapSpec
? render(
heatmapSpec,
settingSpec,
chartDimensions,
heatmapTable,
colorScale,
visibilityFilterRanges,
gridHeightParams,
)
? render(heatmapSpec, settingSpec, chartDimensions, heatmapTable, colorScale, bandsToHide, gridHeightParams)
: nullShapeViewModel();
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

import { ScaleType } from '../../../../scales/constants';
import { createCustomCachedSelector } from '../../../../state/create_selector';
import { identity } from '../../../../utils/common';
import { Color, identity } from '../../../../utils/common';
import { HeatmapSpec } from '../../specs/heatmap';
import { HeatmapTable } from './compute_chart_dimensions';
import { getHeatmapSpecSelector } from './get_heatmap_spec';
Expand All @@ -31,6 +31,8 @@ type ScaleModelType<S> = {
bands: Array<{ start: number }>;
};

type Band = { start: number; end: number; label: string; color: Color };

type ScaleLinearType = ScaleModelType<ScaleLinear<string, string>>;
type ScaleQuantizeType = ScaleModelType<ScaleQuantize<string>>;
type ScaleQuantileType = ScaleModelType<ScaleQuantile<string>>;
Expand Down Expand Up @@ -59,21 +61,23 @@ export const getColorScale = createCustomCachedSelector(
const { scale, bands } = SCALE_TYPE_TO_SCALE_FN[spec.colorScale ?? DEFAULT_COLOR_SCALE_TYPE](spec, heatmapTable);
return {
scale,
bands: dedupBands(bands, spec),
bands: dedupBands(bands, spec, scale),
};
},
);

function dedupBands(bands: Array<{ start: number }>, spec: HeatmapSpec) {
function dedupBands(
bands: Array<{ start: number }>,

This comment has been minimized.

Copy link
@monfera

monfera Aug 16, 2021

Contributor

Is it the same as Band[]? (looking commit by commit so maybe it was adopted already)

spec: HeatmapSpec,
scale: ScaleLinear<string, string> | ScaleQuantile<string> | ScaleQuantize<string> | ScaleThreshold<number, string>,
) {
const formatter = spec.valueFormatter ?? identity;
const bandsWithFormattedStarts = bands.reduce<Map<string, { start: number; formattedStart: string }>>(
(acc, { start }) => {
const formattedStart = `${formatter(start)}`;
acc.set(formattedStart, { start, formattedStart });
return acc;
},
new Map(),
);
const bandsWithFormattedStarts = bands.reduce<Map<string, Band>>((acc, { start }, index, array) => {
const label = `≥ ${formatter(start)}`;
const end = array.length === index + 1 ? Infinity : array[index + 1].start;
acc.set(label, { start, end, label, color: scale(start) });
return acc;
}, new Map());
return [...bandsWithFormattedStarts.values()];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function render(
chartDimensions: Dimensions,
heatmapTable: HeatmapTable,
colorScale: ColorScaleType['scale'],
visibilityFilterRanges: Array<[number, number]>,
bandsToHide: Array<[number, number]>,
gridHeightParams: GridHeightParams,
): ShapeViewModel {
const textMeasurer = document.createElement('canvas');
Expand All @@ -46,7 +46,7 @@ export function render(
chartDimensions,
heatmapTable,
colorScale,
visibilityFilterRanges,
bandsToHide,
gridHeightParams,
);
}
20 changes: 10 additions & 10 deletions packages/charts/src/utils/data_samples/test_anomaly_swim_lane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,52 @@ export const SWIM_LANE_DATA = [
{
laneLabel: 'i-71a7f77b',
time: 1572847200,
value: 1.393504,
value: 0,
},
{
laneLabel: 'i-71a7f77b',
time: 1572892200,
value: 98.22463906512188,
value: 3,
},
{
laneLabel: 'i-71a7f77b',
time: 1572894000,
value: 35.80307,
value: 25,
},
{
laneLabel: 'i-71a7f77b',
time: 1572895800,
value: 0.22128246815642463,
value: 50,
},
{
laneLabel: 'i-71a7f77b',
time: 1572897600,
value: 0.28036102544259556,
value: 75,
},
{
laneLabel: 'i-71a7f77b',
time: 1572899400,
value: 23.045729601789432,
value: 1,
},
{
laneLabel: 'i-71a7f77b',
time: 1572901200,
value: 0.5144934818824536,
value: 2,
},
{
laneLabel: 'i-71a7f77b',
time: 1572903000,
value: 0.2689519481619711,
value: 24,
},
{
laneLabel: 'i-71a7f77b',
time: 1572904800,
value: 0.2141724473865323,
value: 49,
},
{
laneLabel: 'i-71a7f77b',
time: 1572906600,
value: 0.10464817254089763,
value: 74,
},
{
laneLabel: 'i-71a7f77b',
Expand Down

0 comments on commit 4228efc

Please sign in to comment.