Skip to content

Commit bbf891e

Browse files
authored
fix: split out visualizations that do not use PlotEnv (#814)
1 parent 08fb9ed commit bbf891e

File tree

7 files changed

+85
-55
lines changed

7 files changed

+85
-55
lines changed

giraffe/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@influxdata/giraffe",
3-
"version": "2.35.0",
3+
"version": "2.36.0",
44
"main": "dist/index.js",
55
"module": "dist/index.js",
66
"license": "MIT",

giraffe/src/components/Plot.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
// Libraries
12
import React, {FunctionComponent, RefObject, useRef} from 'react'
23
import AutoSizer from 'react-virtualized-auto-sizer'
34

5+
// Components
6+
import {PlotResizer, TableResizer} from './PlotResizer'
7+
8+
// Types
49
import {Config, SizedConfig} from '../types'
510

6-
import {PlotResizer} from './PlotResizer'
11+
// Utils
12+
import {hasPlotEnv} from '../utils/hasPlotEnv'
13+
714
interface PlotProps {
815
config: Config
916
axesCanvasRef?: RefObject<HTMLCanvasElement>
@@ -25,7 +32,7 @@ export const Plot: FunctionComponent<PlotProps> = props => {
2532
}
2633

2734
if (config.width && config.height) {
28-
return (
35+
return hasPlotEnv(config) ? (
2936
<div className="giraffe-fixedsizer" style={{position: 'relative'}}>
3037
<PlotResizer
3138
axesCanvasRef={axesCanvasRef}
@@ -37,10 +44,18 @@ export const Plot: FunctionComponent<PlotProps> = props => {
3744
{children}
3845
</PlotResizer>
3946
</div>
47+
) : (
48+
<TableResizer
49+
config={config as SizedConfig}
50+
height={config.height}
51+
width={config.width}
52+
>
53+
{children}
54+
</TableResizer>
4055
)
4156
}
4257

43-
return (
58+
return hasPlotEnv(config) ? (
4459
<AutoSizer className="giraffe-autosizer">
4560
{({width, height}) => (
4661
<PlotResizer
@@ -54,6 +69,18 @@ export const Plot: FunctionComponent<PlotProps> = props => {
5469
</PlotResizer>
5570
)}
5671
</AutoSizer>
72+
) : (
73+
<AutoSizer className="giraffe-autosizer">
74+
{({width, height}) => (
75+
<TableResizer
76+
config={config as SizedConfig}
77+
height={height}
78+
width={width}
79+
>
80+
{children}
81+
</TableResizer>
82+
)}
83+
</AutoSizer>
5784
)
5885
}
5986

giraffe/src/components/PlotResizer.tsx

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, {FC, RefObject, useMemo} from 'react'
22

3-
import {LayerTypes, PlotDimensions, SizedConfig} from '../types'
3+
import {PlotDimensions, SizedConfig} from '../types'
44

5-
import {get} from '../utils/get'
65
import {resizePlotWithStaticLegend} from '../utils/legend/resizePlot'
76
import {normalizeConfig} from '../utils/normalizeConfig'
87
import {usePlotEnv} from '../utils/usePlotEnv'
@@ -40,21 +39,11 @@ export const PlotResizer: FC<PlotResizerProps> = props => {
4039

4140
const env = usePlotEnv(normalizedConfig)
4241
const spec = env.getSpec(0)
43-
const graphType = get(config, 'layers.0.type')
4442

4543
if (width === 0 || height === 0) {
4644
return null
4745
}
4846

49-
if (
50-
graphType === LayerTypes.Table ||
51-
graphType === LayerTypes.RawFluxDataTable ||
52-
graphType === LayerTypes.Gauge ||
53-
graphType === LayerTypes.SimpleTable
54-
) {
55-
return <SizedTable config={normalizedConfig}>{children}</SizedTable>
56-
}
57-
5847
return (
5948
<>
6049
<SizedPlot
@@ -78,3 +67,28 @@ export const PlotResizer: FC<PlotResizerProps> = props => {
7867
</>
7968
)
8069
}
70+
71+
interface TableResizerProps {
72+
config: SizedConfig
73+
height: number
74+
width: number
75+
}
76+
77+
export const TableResizer: FC<TableResizerProps> = props => {
78+
const {children, config, height, width} = props
79+
80+
const normalizedConfig: SizedConfig = useMemo(
81+
() => ({
82+
...normalizeConfig(config),
83+
height,
84+
width,
85+
}),
86+
[config, height, width]
87+
)
88+
89+
if (width === 0 || height === 0) {
90+
return null
91+
}
92+
93+
return <SizedTable config={normalizedConfig}>{children}</SizedTable>
94+
}

giraffe/src/components/SimpleTable/PagedTable.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ interface ExtendedColumn {
2323
data: any[]
2424
}
2525

26-
const MAXIMUM_ESTIMATED_ROW_HEIGHT = 42
27-
2826
/*
2927
* @param result - the result of the query
3028
* @param paginationOffset - the start index of the first row of the current page
@@ -40,24 +38,24 @@ const getNumberOfRowsOnCurrentPage = (
4038
headerHeight: number,
4139
rowHeight: number
4240
): number => {
43-
if (totalAvailableHeight === 0) {
41+
if (totalAvailableHeight <= 0) {
4442
return 0
4543
}
4644

47-
const minimumLength = result?.table?.length ?? 1
48-
const estimatedRowHeight = Math.min(
49-
Math.ceil(totalAvailableHeight / minimumLength),
50-
MAXIMUM_ESTIMATED_ROW_HEIGHT
51-
)
52-
const visibleRowHeight = Math.max(rowHeight, estimatedRowHeight)
45+
// this means that no rows have been mounted or measured, so we need to
46+
// mount one row to measure the row height
47+
if (rowHeight <= 0) {
48+
return 1
49+
}
5350

5451
let runningHeight = 14
5552
let rowIdx = paginationOffset
5653
let currentTable
5754
let lastSignature
5855
let signature
5956

60-
const lastVisibleRowMinimumHeight = 0.2 * visibleRowHeight
57+
// rowHeight is now guaranteed to be greater than zero
58+
const lastVisibleRowMinimumHeight = 0.2 * rowHeight
6159

6260
while (rowIdx < result.table.length) {
6361
if (result.table.columns?.table?.data?.[rowIdx] !== currentTable) {
@@ -92,7 +90,7 @@ const getNumberOfRowsOnCurrentPage = (
9290
continue
9391
}
9492

95-
runningHeight += visibleRowHeight
93+
runningHeight += rowHeight
9694

9795
if (runningHeight + lastVisibleRowMinimumHeight >= totalAvailableHeight) {
9896
break
@@ -402,7 +400,7 @@ const PagedTable: FC<Props> = ({result, showAll}) => {
402400
numberOfRowsOnCurrentPage,
403401
showAll
404402
),
405-
[numberOfRowsOnCurrentPage, paginationOffset, result] // eslint-disable-line react-hooks/exhaustive-deps
403+
[numberOfRowsOnCurrentPage, paginationOffset, result, showAll]
406404
)
407405

408406
const inner = useMemo(() => {

giraffe/src/components/SizedTable.tsx

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,11 @@ import {FluxTablesTransform} from './FluxTablesTransform'
1616
import {TableGraphLayer} from './Table'
1717
import {SimpleTableLayer} from './SimpleTable'
1818

19-
import {usePlotEnv} from '../utils/usePlotEnv'
20-
2119
interface Props {
2220
config: SizedConfig
2321
}
2422

25-
export const SizedTable: FunctionComponent<Props> = ({
26-
config: userConfig,
27-
children,
28-
}) => {
29-
const env = usePlotEnv(userConfig)
30-
31-
const {config} = env
23+
export const SizedTable: FunctionComponent<Props> = ({config, children}) => {
3224
const {width, height} = config
3325

3426
config.showAxes = false
@@ -53,7 +45,7 @@ export const SizedTable: FunctionComponent<Props> = ({
5345
<div
5446
className="giraffe-inner-plot"
5547
style={{
56-
cursor: `${userConfig.cursor || 'auto'}`,
48+
cursor: `${config.cursor || 'auto'}`,
5749
}}
5850
>
5951
<div className="giraffe-layers" style={fullsizeStyle}>
@@ -113,22 +105,6 @@ export const SizedTable: FunctionComponent<Props> = ({
113105
</FluxTablesTransform>
114106
)
115107

116-
case LayerTypes.Custom:
117-
const renderProps = {
118-
key: layerIndex,
119-
width,
120-
height,
121-
innerWidth: env.innerWidth,
122-
innerHeight: env.innerHeight,
123-
xScale: env.xScale,
124-
yScale: env.yScale,
125-
xDomain: env.xDomain,
126-
yDomain: env.yDomain,
127-
columnFormatter: env.getFormatterForColumn,
128-
yColumnType: env.yColumnType,
129-
}
130-
return layerConfig.render(renderProps)
131-
132108
default:
133109
return null
134110
}

giraffe/src/utils/hasPlotEnv.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Config, LayerTypes} from '../types'
2+
import {get} from './get'
3+
4+
export const hasPlotEnv = (config: Config): boolean => {
5+
const graphType = get(config, 'layers.0.type')
6+
if (
7+
graphType === LayerTypes.Gauge ||
8+
graphType === LayerTypes.RawFluxDataTable ||
9+
graphType === LayerTypes.SimpleTable ||
10+
graphType === LayerTypes.Table
11+
) {
12+
return false
13+
}
14+
return true
15+
}

stories/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@influxdata/giraffe-stories",
3-
"version": "2.35.0",
3+
"version": "2.36.0",
44
"license": "MIT",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)