Skip to content

Commit b48c80d

Browse files
authored
Select the number of items per row from slider (#3405)
* Added a story to visualize the icons * Disable chromatic snapshots * Select the number of items per row from picker * Use const values * Use slider instead * Resize multiview plots * Self review * Fix and add tests * Apply review comments
1 parent 0ab27ea commit b48c80d

File tree

28 files changed

+328
-526
lines changed

28 files changed

+328
-526
lines changed

extension/src/plots/model/index.test.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { PlotsModel } from '.'
22
import {
3+
DEFAULT_NB_ITEMS_PER_REOW,
34
DEFAULT_SECTION_COLLAPSED,
45
DEFAULT_SECTION_NB_ITEMS_PER_ROW,
5-
PlotNumberOfItemsPerRow,
66
Section
77
} from '../webview/contract'
88
import { buildMockMemento } from '../../test/util'
@@ -69,33 +69,25 @@ describe('plotsModel', () => {
6969

7070
it('should change the plotSize when calling setPlotSize', () => {
7171
expect(model.getNbItemsPerRow(Section.CHECKPOINT_PLOTS)).toStrictEqual(
72-
PlotNumberOfItemsPerRow.TWO
72+
DEFAULT_NB_ITEMS_PER_REOW
7373
)
7474

75-
model.setNbItemsPerRow(
76-
Section.CHECKPOINT_PLOTS,
77-
PlotNumberOfItemsPerRow.ONE
78-
)
75+
model.setNbItemsPerRow(Section.CHECKPOINT_PLOTS, 1)
7976

80-
expect(model.getNbItemsPerRow(Section.CHECKPOINT_PLOTS)).toStrictEqual(
81-
PlotNumberOfItemsPerRow.ONE
82-
)
77+
expect(model.getNbItemsPerRow(Section.CHECKPOINT_PLOTS)).toStrictEqual(1)
8378
})
8479

8580
it('should update the persisted plot size when calling setPlotSize', () => {
8681
const mementoUpdateSpy = jest.spyOn(memento, 'update')
8782

88-
model.setNbItemsPerRow(
89-
Section.CHECKPOINT_PLOTS,
90-
PlotNumberOfItemsPerRow.TWO
91-
)
83+
model.setNbItemsPerRow(Section.CHECKPOINT_PLOTS, 2)
9284

9385
expect(mementoUpdateSpy).toHaveBeenCalledTimes(1)
9486
expect(mementoUpdateSpy).toHaveBeenCalledWith(
9587
PersistenceKey.PLOT_NB_ITEMS_PER_ROW + exampleDvcRoot,
9688
{
9789
...DEFAULT_SECTION_NB_ITEMS_PER_ROW,
98-
[Section.CHECKPOINT_PLOTS]: PlotNumberOfItemsPerRow.TWO
90+
[Section.CHECKPOINT_PLOTS]: 2
9991
}
10092
)
10193
})

extension/src/plots/model/index.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import {
2727
Section,
2828
SectionCollapsed,
2929
CustomPlotData,
30-
PlotNumberOfItemsPerRow,
31-
DEFAULT_HEIGHT
30+
DEFAULT_HEIGHT,
31+
DEFAULT_NB_ITEMS_PER_REOW
3232
} from '../webview/contract'
3333
import {
3434
ExperimentsOutput,
@@ -408,18 +408,10 @@ export class PlotsModel extends ModelWithPersistence {
408408
}
409409

410410
public getNbItemsPerRow(section: Section) {
411-
if (
412-
this.nbItemsPerRow[section] &&
413-
[
414-
PlotNumberOfItemsPerRow.ONE,
415-
PlotNumberOfItemsPerRow.TWO,
416-
PlotNumberOfItemsPerRow.THREE,
417-
PlotNumberOfItemsPerRow.FOUR
418-
].includes(this.nbItemsPerRow[section])
419-
) {
411+
if (this.nbItemsPerRow[section]) {
420412
return this.nbItemsPerRow[section]
421413
}
422-
return PlotNumberOfItemsPerRow.TWO
414+
return DEFAULT_NB_ITEMS_PER_REOW
423415
}
424416

425417
public setHeight(section: Section, height: number | undefined) {

extension/src/plots/vega/util.test.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import scatterTemplate from '../../test/fixtures/plotsDiff/templates/scatter'
1818
import smoothTemplate from '../../test/fixtures/plotsDiff/templates/smooth'
1919
import multiSourceTemplate from '../../test/fixtures/plotsDiff/templates/multiSource'
2020
import { copyOriginalColors } from '../../experiments/model/status/colors'
21-
import { PlotNumberOfItemsPerRow } from '../webview/contract'
2221
import { EXPERIMENT_WORKSPACE_ID } from '../../cli/dvc/contract'
22+
import { DEFAULT_NB_ITEMS_PER_REOW } from '../webview/contract'
2323

2424
describe('isMultiViewPlot', () => {
2525
it('should recognize the confusion matrix template as a multi view plot', () => {
@@ -86,7 +86,7 @@ describe('extendVegaSpec', () => {
8686
it('should not add encoding if no color scale is provided', () => {
8787
const extendedSpec = extendVegaSpec(
8888
linearTemplate,
89-
PlotNumberOfItemsPerRow.TWO
89+
DEFAULT_NB_ITEMS_PER_REOW
9090
)
9191
expect(extendedSpec.encoding).toBeUndefined()
9292
})
@@ -98,7 +98,7 @@ describe('extendVegaSpec', () => {
9898
}
9999
const extendedSpec = extendVegaSpec(
100100
linearTemplate,
101-
PlotNumberOfItemsPerRow.TWO,
101+
DEFAULT_NB_ITEMS_PER_REOW,
102102
{
103103
color: colorScale
104104
}
@@ -143,7 +143,7 @@ describe('extendVegaSpec', () => {
143143

144144
it('should truncate all titles from the left to 50 characters for large plots', () => {
145145
const spec = withLongTemplatePlotTitle()
146-
const updatedSpec = extendVegaSpec(spec, PlotNumberOfItemsPerRow.ONE)
146+
const updatedSpec = extendVegaSpec(spec, 1)
147147

148148
const truncatedTitle = '…-many-many-characters-at-least-seventy-characters'
149149
const truncatedHorizontalTitle =
@@ -169,7 +169,7 @@ describe('extendVegaSpec', () => {
169169

170170
it('should truncate all titles from the left to 50 characters for regular plots', () => {
171171
const spec = withLongTemplatePlotTitle()
172-
const updatedSpec = extendVegaSpec(spec, PlotNumberOfItemsPerRow.TWO)
172+
const updatedSpec = extendVegaSpec(spec, DEFAULT_NB_ITEMS_PER_REOW)
173173

174174
const truncatedTitle = '…-many-many-characters-at-least-seventy-characters'
175175
const truncatedHorizontalTitle =
@@ -195,7 +195,7 @@ describe('extendVegaSpec', () => {
195195

196196
it('should truncate all titles from the left to 30 characters for small plots', () => {
197197
const spec = withLongTemplatePlotTitle()
198-
const updatedSpec = extendVegaSpec(spec, PlotNumberOfItemsPerRow.THREE)
198+
const updatedSpec = extendVegaSpec(spec, 3)
199199

200200
const truncatedTitle = '…s-at-least-seventy-characters'
201201
const truncatedHorizontalTitle = '…at-least-seventy-characters-x'
@@ -225,7 +225,7 @@ describe('extendVegaSpec', () => {
225225
text: repeatedTitle
226226
})
227227

228-
const updatedSpec = extendVegaSpec(spec, PlotNumberOfItemsPerRow.THREE)
228+
const updatedSpec = extendVegaSpec(spec, 3)
229229

230230
const truncatedTitle = '…ghijklmnopqrstuvwyz1234567890'
231231

@@ -242,7 +242,7 @@ describe('extendVegaSpec', () => {
242242
const repeatedTitle = 'abcdefghijklmnopqrstuvwyz1234567890'
243243
const spec = withLongTemplatePlotTitle([repeatedTitle, repeatedTitle])
244244

245-
const updatedSpec = extendVegaSpec(spec, PlotNumberOfItemsPerRow.THREE)
245+
const updatedSpec = extendVegaSpec(spec, 3)
246246

247247
const truncatedTitle = '…ghijklmnopqrstuvwyz1234567890'
248248

@@ -262,7 +262,7 @@ describe('extendVegaSpec', () => {
262262
text: [repeatedTitle, repeatedTitle]
263263
})
264264

265-
const updatedSpec = extendVegaSpec(spec, PlotNumberOfItemsPerRow.THREE)
265+
const updatedSpec = extendVegaSpec(spec, 3)
266266

267267
const truncatedTitle = '…ghijklmnopqrstuvwyz1234567890'
268268

@@ -276,17 +276,13 @@ describe('extendVegaSpec', () => {
276276
})
277277

278278
it('should update the multi-source template to remove erroneous shape encoding from the vertical line displayed on hover', () => {
279-
const updatedSpec = extendVegaSpec(
280-
multiSourceTemplate,
281-
PlotNumberOfItemsPerRow.ONE,
282-
{
283-
color: { domain: [], range: [] },
284-
shape: {
285-
field: 'field',
286-
scale: { domain: [], range: [] }
287-
}
279+
const updatedSpec = extendVegaSpec(multiSourceTemplate, 1, {
280+
color: { domain: [], range: [] },
281+
shape: {
282+
field: 'field',
283+
scale: { domain: [], range: [] }
288284
}
289-
)
285+
})
290286

291287
expect(updatedSpec.encoding).not.toBeUndefined()
292288
expect(updatedSpec.layer[1].layer[0].encoding.shape).toBeNull()

extension/src/plots/vega/util.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
} from 'vega-lite/build/src/spec/repeat'
2727
import { TopLevelUnitSpec } from 'vega-lite/build/src/spec/unit'
2828
import isEqual from 'lodash.isequal'
29-
import { ColorScale, PlotNumberOfItemsPerRow } from '../webview/contract'
29+
import { ColorScale, DEFAULT_NB_ITEMS_PER_REOW } from '../webview/contract'
3030
import { ShapeEncoding, StrokeDashEncoding } from '../multiSource/constants'
3131
import { Color } from '../../experiments/model/status/colors'
3232

@@ -220,13 +220,6 @@ const truncateTitleAsArrayOrString = (title: Text, size: number) => {
220220
return truncateTitleString(title as unknown as string, size)
221221
}
222222

223-
const TitleLimit = {
224-
[PlotNumberOfItemsPerRow.ONE]: 50,
225-
[PlotNumberOfItemsPerRow.TWO]: 50,
226-
[PlotNumberOfItemsPerRow.THREE]: 30,
227-
[PlotNumberOfItemsPerRow.FOUR]: 30
228-
}
229-
230223
const truncateTitlePart = (
231224
title: Title,
232225
key: 'text' | 'subtitle',
@@ -261,7 +254,7 @@ const truncateTitle = (
261254
}
262255

263256
export const truncateVerticalTitle = (title: Text | Title, size: number) =>
264-
truncateTitle(title, TitleLimit[size] * 0.75)
257+
truncateTitle(title, (size > 2 ? 30 : 50) * 0.75)
265258

266259
const isEndValue = (valueType: string) =>
267260
['string', 'number', 'boolean'].includes(valueType)
@@ -289,7 +282,7 @@ export const truncateTitles = (
289282
const title = value as unknown as Title
290283
specCopy[key] = vertical
291284
? truncateVerticalTitle(title, size)
292-
: truncateTitle(title, TitleLimit[size])
285+
: truncateTitle(title, size > DEFAULT_NB_ITEMS_PER_REOW ? 30 : 50)
293286
} else if (isEndValue(valueType)) {
294287
specCopy[key] = value
295288
} else if (Array.isArray(value)) {

extension/src/plots/webview/contract.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
import { VisualizationSpec } from 'react-vega'
22
import { Color } from '../../experiments/model/status/colors'
33

4-
// It is easier to keep the numerical order than the alphabetical one
5-
/* eslint-disable sort-keys-fix/sort-keys-fix */
6-
export const PlotNumberOfItemsPerRow = {
7-
ONE: 1,
8-
TWO: 2,
9-
THREE: 3,
10-
FOUR: 4
11-
}
12-
/* eslint-enable sort-keys-fix/sort-keys-fix */
4+
export const DEFAULT_NB_ITEMS_PER_REOW = 2
135

146
export enum Section {
157
CHECKPOINT_PLOTS = 'checkpoint-plots',
@@ -19,10 +11,10 @@ export enum Section {
1911
}
2012

2113
export const DEFAULT_SECTION_NB_ITEMS_PER_ROW = {
22-
[Section.CHECKPOINT_PLOTS]: PlotNumberOfItemsPerRow.TWO,
23-
[Section.TEMPLATE_PLOTS]: PlotNumberOfItemsPerRow.TWO,
24-
[Section.COMPARISON_TABLE]: PlotNumberOfItemsPerRow.TWO,
25-
[Section.CUSTOM_PLOTS]: PlotNumberOfItemsPerRow.TWO
14+
[Section.CHECKPOINT_PLOTS]: DEFAULT_NB_ITEMS_PER_REOW,
15+
[Section.TEMPLATE_PLOTS]: DEFAULT_NB_ITEMS_PER_REOW,
16+
[Section.COMPARISON_TABLE]: DEFAULT_NB_ITEMS_PER_REOW,
17+
[Section.CUSTOM_PLOTS]: DEFAULT_NB_ITEMS_PER_REOW
2618
}
2719

2820
// Height is undefined by default because it is calculated by ratio of the width it'll fill (calculated by the webview)

extension/src/test/fixtures/expShow/base/checkpointPlots.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { copyOriginalColors } from '../../../../experiments/model/status/colors'
22
import {
33
CheckpointPlotsData,
4-
PlotNumberOfItemsPerRow
4+
DEFAULT_NB_ITEMS_PER_REOW
55
} from '../../../../plots/webview/contract'
66

77
const colors = copyOriginalColors()
@@ -91,7 +91,7 @@ const data: CheckpointPlotsData = {
9191
'summary.json:val_loss',
9292
'summary.json:val_accuracy'
9393
],
94-
nbItemsPerRow: PlotNumberOfItemsPerRow.TWO,
94+
nbItemsPerRow: DEFAULT_NB_ITEMS_PER_REOW,
9595
height: undefined
9696
}
9797

extension/src/test/fixtures/expShow/base/customPlots.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
CustomPlotsData,
3-
PlotNumberOfItemsPerRow
3+
DEFAULT_NB_ITEMS_PER_REOW
44
} from '../../../../plots/webview/contract'
55

66
const data: CustomPlotsData = {
@@ -50,7 +50,7 @@ const data: CustomPlotsData = {
5050
]
5151
}
5252
],
53-
nbItemsPerRow: PlotNumberOfItemsPerRow.TWO,
53+
nbItemsPerRow: DEFAULT_NB_ITEMS_PER_REOW,
5454
height: undefined
5555
}
5656

extension/src/test/fixtures/plotsDiff/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111
TemplatePlotGroup,
1212
TemplatePlotsData,
1313
TemplatePlots,
14-
PlotNumberOfItemsPerRow,
1514
Revision,
16-
PlotsComparisonData
15+
PlotsComparisonData,
16+
DEFAULT_NB_ITEMS_PER_REOW
1717
} from '../../../plots/webview/contract'
1818
import { join } from '../../util/path'
1919
import { copyOriginalColors } from '../../../experiments/model/status/colors'
@@ -499,7 +499,7 @@ const extendedSpecs = (plotsOutput: TemplatePlots): TemplatePlotSection[] => {
499499
) || []
500500
}
501501
} as TopLevelSpec,
502-
PlotNumberOfItemsPerRow.TWO,
502+
DEFAULT_NB_ITEMS_PER_REOW,
503503
{
504504
color: {
505505
domain: expectedRevisions,
@@ -659,14 +659,14 @@ export const getRevisions = (): Revision[] => {
659659

660660
export const getMinimalWebviewMessage = () => ({
661661
plots: extendedSpecs(basicVega),
662-
nbItemsPerRow: PlotNumberOfItemsPerRow.TWO,
662+
nbItemsPerRow: DEFAULT_NB_ITEMS_PER_REOW,
663663
height: undefined,
664664
revisions: getRevisions()
665665
})
666666

667667
export const getTemplateWebviewMessage = (): TemplatePlotsData => ({
668668
plots: extendedSpecs({ ...basicVega, ...require('./vega').default }),
669-
nbItemsPerRow: PlotNumberOfItemsPerRow.TWO,
669+
nbItemsPerRow: DEFAULT_NB_ITEMS_PER_REOW,
670670
height: undefined
671671
})
672672

@@ -676,7 +676,7 @@ export const getManyTemplatePlotsWebviewMessage = (
676676
plots: extendedSpecs({
677677
...multipleVega(length)
678678
}),
679-
nbItemsPerRow: PlotNumberOfItemsPerRow.TWO,
679+
nbItemsPerRow: DEFAULT_NB_ITEMS_PER_REOW,
680680
height: undefined
681681
})
682682

@@ -703,7 +703,7 @@ export const getComparisonWebviewMessage = (
703703
return {
704704
revisions: getRevisions(),
705705
plots: plotAcc,
706-
nbItemsPerRow: PlotNumberOfItemsPerRow.TWO,
706+
nbItemsPerRow: DEFAULT_NB_ITEMS_PER_REOW,
707707
height: undefined
708708
}
709709
}

extension/src/test/suite/experiments/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import { DvcExecutor } from '../../../cli/dvc/executor'
6767
import { shortenForLabel } from '../../../util/string'
6868
import { GitExecutor } from '../../../cli/git/executor'
6969
import { WorkspacePlots } from '../../../plots/workspace'
70-
import { PlotNumberOfItemsPerRow } from '../../../plots/webview/contract'
7170
import {
7271
RegisteredCliCommands,
7372
RegisteredCommands
@@ -82,6 +81,7 @@ import * as ProcessExecution from '../../../process/execution'
8281
import { DvcReader } from '../../../cli/dvc/reader'
8382
import { Connect } from '../../../connect'
8483
import { DvcViewer } from '../../../cli/dvc/viewer'
84+
import { DEFAULT_NB_ITEMS_PER_REOW } from '../../../plots/webview/contract'
8585

8686
const { openFileInEditor } = FileSystem
8787

@@ -339,7 +339,7 @@ suite('Experiments Test Suite', () => {
339339
).returns(undefined)
340340

341341
const mockColumnId = 'params:params.yaml:lr'
342-
const mockWidth = PlotNumberOfItemsPerRow.TWO
342+
const mockWidth = DEFAULT_NB_ITEMS_PER_REOW
343343

344344
mockMessageReceived.fire({
345345
payload: { id: mockColumnId, width: mockWidth },

0 commit comments

Comments
 (0)