Skip to content

Commit ccef34b

Browse files
committed
Extract more utility functions
1 parent 8017f50 commit ccef34b

1 file changed

Lines changed: 91 additions & 67 deletions

File tree

assets/js/dashboard/stats/graph/main-graph.tsx

Lines changed: 91 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -175,35 +175,6 @@ export const MainGraph = ({
175175
.attr('class', tickLineClass)
176176
)
177177

178-
const drawLine = (
179-
svg: SelectedSVG,
180-
dataset: GraphDatum[],
181-
isDefined: (d: GraphDatum) => boolean,
182-
yAccessor: (d: GraphDatum, index: number) => number,
183-
className?: string
184-
) => {
185-
const line = d3
186-
.line<GraphDatum>()
187-
.defined(isDefined)
188-
.x((_d, index) => x(index))
189-
.y(yAccessor)
190-
191-
svg
192-
.append('path')
193-
.attr('fill', 'none')
194-
.attr('class', classNames(sharedPathClass, className))
195-
.attr('stroke-linejoin', 'round')
196-
.attr('stroke-linecap', 'round')
197-
.datum(dataset)
198-
.attr('d', line)
199-
}
200-
201-
const drawDot = (className: string) => {
202-
const dot = svg.append('g').attr('display', 'none')
203-
dot.append('circle').attr('r', 2.5).attr('class', className)
204-
return dot
205-
}
206-
207178
const mainGradientId = addGradient({
208179
svg,
209180
id: 'main',
@@ -219,44 +190,46 @@ export const MainGraph = ({
219190

220191
const yBottomEdge = height - marginBottom
221192

222-
paintUnderLine(
193+
drawAreaUnderLine({
223194
svg,
224-
mainGradientId,
225-
(d) => d.timeLabel !== null,
226-
(_d, index) => x(index),
227-
yBottomEdge,
228-
(d) => y(d.value!),
229-
remappedData
230-
)
195+
gradientId: mainGradientId,
196+
isDefined: (d) => d.timeLabel !== null,
197+
xAccessor: (_d, index) => x(index),
198+
y0Accessor: yBottomEdge,
199+
y1Accessor: (d) => y(d.value!),
200+
datum: remappedData
201+
})
231202

232-
paintUnderLine(
203+
drawAreaUnderLine({
233204
svg,
234-
comparisonGradientId,
235-
(d) => d.comparisonTimeLabel !== null,
236-
(_d, index) => x(index),
237-
yBottomEdge,
238-
(d) => y(d.comparisonValue!),
239-
remappedData
240-
)
205+
gradientId: comparisonGradientId,
206+
isDefined: (d) => d.comparisonTimeLabel !== null,
207+
xAccessor: (_d, index) => x(index),
208+
y0Accessor: yBottomEdge,
209+
y1Accessor: (d) => y(d.comparisonValue!),
210+
datum: remappedData
211+
})
241212

242-
drawLine(
213+
drawLine({
243214
svg,
244-
remappedData,
245-
(d) => d.timeLabel !== null,
246-
(d) => y(d.value!),
247-
mainPathClass
248-
)
215+
datum: remappedData,
216+
isDefined: (d) => d.timeLabel !== null,
217+
xAccessor: (_d, index) => x(index),
218+
yAccessor: (d) => y(d.value!),
219+
className: mainPathClass
220+
})
249221

250-
drawLine(
222+
drawLine({
251223
svg,
252-
remappedData,
253-
(d) => d.comparisonTimeLabel !== null,
254-
(d) => y(d.comparisonValue!),
255-
comparisonPathClass
256-
)
224+
datum: remappedData,
225+
isDefined: (d) => d.comparisonTimeLabel !== null,
226+
xAccessor: (_d, index) => x(index),
227+
yAccessor: (d) => y(d.comparisonValue!),
228+
className: comparisonPathClass
229+
})
257230

258-
const dot = drawDot(mainDotClass)
259-
const comparisonDot = drawDot(comparisonDotClass)
231+
const dot = drawDot({ svg, className: mainDotClass })
232+
const comparisonDot = drawDot({ svg, className: comparisonDotClass })
260233

261234
svg
262235
.on('pointermove', (event) => {
@@ -691,15 +664,23 @@ const addGradient = ({
691664
return id
692665
}
693666

694-
const paintUnderLine = (
695-
svg: SelectedSVG,
696-
gradientId: string,
697-
isDefined: (d: GraphDatum) => boolean,
698-
xAccessor: (d: GraphDatum, index: number) => number,
699-
y0Accessor: number,
700-
y1Accessor: (d: GraphDatum, index: number) => number,
667+
const drawAreaUnderLine = ({
668+
svg,
669+
gradientId,
670+
isDefined,
671+
xAccessor,
672+
y0Accessor,
673+
y1Accessor,
674+
datum
675+
}: {
676+
svg: SelectedSVG
677+
gradientId: string
678+
isDefined: (d: GraphDatum) => boolean
679+
xAccessor: (d: GraphDatum, index: number) => number
680+
y0Accessor: number
681+
y1Accessor: (d: GraphDatum, index: number) => number
701682
datum: GraphDatum[]
702-
) => {
683+
}) => {
703684
const area = d3
704685
.area<GraphDatum>()
705686
.x(xAccessor)
@@ -715,4 +696,47 @@ const paintUnderLine = (
715696
.attr('d', area)
716697
}
717698

699+
const drawLine = ({
700+
svg,
701+
datum,
702+
isDefined,
703+
xAccessor,
704+
yAccessor,
705+
className
706+
}: {
707+
svg: SelectedSVG
708+
datum: GraphDatum[]
709+
isDefined: (d: GraphDatum) => boolean
710+
xAccessor: (d: GraphDatum, index: number) => number
711+
yAccessor: (d: GraphDatum, index: number) => number
712+
className?: string
713+
}) => {
714+
const line = d3
715+
.line<GraphDatum>()
716+
.defined(isDefined)
717+
.x(xAccessor)
718+
.y(yAccessor)
719+
720+
svg
721+
.append('path')
722+
.attr('fill', 'none')
723+
.attr('class', classNames(sharedPathClass, className))
724+
.attr('stroke-linejoin', 'round')
725+
.attr('stroke-linecap', 'round')
726+
.datum(datum)
727+
.attr('d', line)
728+
}
729+
730+
const drawDot = ({
731+
svg,
732+
className
733+
}: {
734+
svg: SelectedSVG
735+
className: string
736+
}) => {
737+
const dot = svg.append('g').attr('display', 'none')
738+
dot.append('circle').attr('r', 2.5).attr('class', className)
739+
return dot
740+
}
741+
718742
type SelectedSVG = d3.Selection<SVGSVGElement, unknown, null, undefined>

0 commit comments

Comments
 (0)