Skip to content

Commit

Permalink
small performance improvement on selection events
Browse files Browse the repository at this point in the history
  • Loading branch information
dvmartinweigl committed Apr 29, 2024
1 parent 3e9d225 commit 9abc51a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/vis/scatter/ScatterVis.tsx
@@ -1,6 +1,7 @@
import { Center, Group, Stack } from '@mantine/core';
import * as d3 from 'd3v7';
import uniqueId from 'lodash/uniqueId';
import { XAxisName, YAxisName } from 'plotly.js-dist-min';
import * as React from 'react';
import { useEffect, useMemo, useState } from 'react';
import { useAsync } from '../../hooks';
Expand All @@ -20,10 +21,8 @@ const annotationsForRegressionStats = (results: IRegressionResult[]) => {
annotations.push({
x: 0.02,
y: 0.98,
// @ts-ignore
xref: `${r.xref} domain`,
// @ts-ignore
yref: `${r.yref} domain`,
xref: `${r.xref} domain` as XAxisName,
yref: `${r.yref} domain` as YAxisName,
text: `<b>n: ${r.stats.n}</b><br><b>r²: ${r.stats.r2}</b><br><b>corr: ${r.stats.correlation}</b><br>`,
showarrow: false,
font: {
Expand Down Expand Up @@ -196,11 +195,13 @@ export function ScatterVis({
.forEach((p) => {
const temp = [];

(p.data.ids as any).forEach((currId, index) => {
if (selectedMap[currId] || (selectedList.length === 0 && config.color)) {
temp.push(index);
}
});
if (selectedList.length > 0) {
selectedList.forEach((selectedId) => {
temp.push(p.data.ids.indexOf(selectedId));
});
} else if (config.color && selectedList.length === 0) {
temp.push(...Array.from({ length: p.data.ids.length }, (_, i) => i));
}

p.data.selectedpoints = temp;
// @ts-ignore
Expand Down Expand Up @@ -230,7 +231,7 @@ export function ScatterVis({
}

return [];
}, [traces, selectedList.length, config.showLabels, config.color, config.alphaSliderVal, selectedMap]);
}, [traces, selectedList, config.color, config.showLabels, config.alphaSliderVal]);

const plotlyData = useMemo(() => {
if (traces) {
Expand Down
2 changes: 1 addition & 1 deletion src/vis/scatter/utils.ts
Expand Up @@ -44,7 +44,7 @@ const defaultConfig: IScatterConfig = {
dragMode: EScatterSelectSettings.RECTANGLE,
alphaSliderVal: 0.5,
sizeSliderVal: 8,
showLabels: ELabelingOptions.SELECTED,
showLabels: ELabelingOptions.NEVER,
regressionLineOptions: {
type: ERegressionLineType.NONE,
fitOptions: { order: 2, precision: 3 },
Expand Down
3 changes: 2 additions & 1 deletion src/vis/stories/Vis/Scatter/ScatterRandom.stories.tsx
Expand Up @@ -106,12 +106,13 @@ const Template: ComponentStory<typeof Vis> = (args) => {
const columns = React.useMemo(() => fetchData(args.pointCount), [args.pointCount]);

const [selected, setSelected] = React.useState<string[]>([]);
const [config, setConfig] = React.useState<BaseVisConfig>(args.externalConfig);

return (
<VisProvider>
<div style={{ height: '100vh', width: '100%', display: 'flex', justifyContent: 'center', alignContent: 'center', flexWrap: 'wrap' }}>
<div style={{ width: '70%', height: '80%' }}>
<Vis {...args} setExternalConfig={() => {}} selected={selected} selectionCallback={setSelected} columns={columns} />
<Vis {...args} externalConfig={config} setExternalConfig={setConfig} selected={selected} selectionCallback={setSelected} columns={columns} />
</div>
</div>
</VisProvider>
Expand Down

0 comments on commit 9abc51a

Please sign in to comment.