-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui-speedspacechart: add declivities layer in gev v2
- Loading branch information
1 parent
dfce11d
commit 46ceff3
Showing
10 changed files
with
310 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
ui-speedspacechart/src/components/helpers/drawElements/declivity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { clearCanvas, slopesValues, maxPositionValues } from '../../utils'; | ||
import type { Store } from '../../../types/chartTypes'; | ||
import { MARGINS } from '../../const'; | ||
import { SLOPE_FILL_COLOR } from '../../../components/const'; | ||
|
||
const { CURVE_MARGIN_SIDES, MARGIN_TOP, MARGIN_BOTTOM, RIGHT_TICK_MARGINS } = MARGINS; | ||
|
||
export const drawDeclivity = ( | ||
ctx: CanvasRenderingContext2D, | ||
width: number, | ||
height: number, | ||
store: Store | ||
) => { | ||
const { slopes, ratioX, leftOffset } = store; | ||
|
||
const { maxGradient } = slopesValues(store); | ||
const { maxPosition } = maxPositionValues(store); | ||
|
||
if (!slopes || slopes.length === 0) { | ||
console.error('Slopes data is missing or empty.'); | ||
return; | ||
} | ||
|
||
clearCanvas(ctx, width, height); | ||
|
||
ctx.save(); | ||
ctx.translate(leftOffset, 0); | ||
|
||
// Calculate total height available for drawing, excluding the margins | ||
const availableHeight = height - MARGIN_TOP - MARGIN_BOTTOM - RIGHT_TICK_MARGINS / 2; | ||
|
||
// Calculate the vertical center of the chart | ||
const centerY = MARGIN_TOP + RIGHT_TICK_MARGINS / 2 + availableHeight / 2; | ||
|
||
ctx.fillStyle = SLOPE_FILL_COLOR; | ||
|
||
try { | ||
const coef = ((width - CURVE_MARGIN_SIDES) / maxPosition) * ratioX; | ||
|
||
for (let i = 0; i < slopes.length - 1; i++) { | ||
const current = slopes[i]; | ||
const next = slopes[i + 1]; | ||
|
||
const x1 = current.position * coef + CURVE_MARGIN_SIDES / 2; | ||
const x2 = next.position * coef + CURVE_MARGIN_SIDES / 2; | ||
|
||
const rectWidth = x2 - x1; | ||
|
||
const rectHeight = (current.gradient / maxGradient) * (availableHeight / 2); | ||
|
||
const rectY = current.gradient >= 0 ? centerY - rectHeight : centerY; | ||
|
||
ctx.fillRect(x1, rectY, rectWidth, Math.abs(rectHeight)); | ||
} | ||
} catch (error) { | ||
console.error('Error while drawing declivity:', error); | ||
} | ||
ctx.restore(); | ||
}; |
64 changes: 64 additions & 0 deletions
64
ui-speedspacechart/src/components/helpers/drawElements/tickYRight.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { clearCanvas, slopesValues } from '../../utils'; | ||
import type { Store } from '../../../types/chartTypes'; | ||
import { MARGINS, RIGHT_TICK_HEIGHT_OFFSET } from '../../const'; | ||
|
||
const { MARGIN_LEFT, MARGIN_TOP, MARGIN_BOTTOM, RIGHT_TICK_MARGINS } = MARGINS; | ||
|
||
export const drawTickYRight = ( | ||
ctx: CanvasRenderingContext2D, | ||
width: number, | ||
height: number, | ||
store: Store | ||
) => { | ||
clearCanvas(ctx, width, height); | ||
|
||
const { minGradient, maxGradient } = slopesValues(store); | ||
|
||
// Calculate total height available for ticks excluding the margins | ||
const availableHeight = height - MARGIN_TOP - MARGIN_BOTTOM - RIGHT_TICK_MARGINS; | ||
|
||
const tickSpacing = availableHeight / 12; // 12 intervals for 13 ticks | ||
|
||
// Calculate the vertical center of the chart | ||
const centerY = | ||
MARGIN_TOP + RIGHT_TICK_MARGINS / 2 + availableHeight / 2 + RIGHT_TICK_HEIGHT_OFFSET; | ||
|
||
const textOffsetX = width - MARGIN_LEFT + 10; | ||
const tickWidth = 6; | ||
|
||
ctx.font = 'normal 12px IBM Plex Sans'; | ||
ctx.lineWidth = 0.5; | ||
|
||
// Calculate gradient step to avoid decimals | ||
const maxAbsGradient = Math.max(maxGradient, minGradient); | ||
const roundedMaxAbsGradient = Math.ceil(maxAbsGradient / 6) * 6; | ||
const gradientStep = roundedMaxAbsGradient / 6; | ||
|
||
ctx.beginPath(); | ||
for (let i = -6; i <= 6; i++) { | ||
const tickValue = i * gradientStep; | ||
const tickY = centerY - i * tickSpacing; | ||
|
||
ctx.moveTo(width - MARGIN_LEFT - tickWidth, tickY); | ||
ctx.lineTo(width - MARGIN_LEFT, tickY); | ||
ctx.strokeStyle = 'rgb(121, 118, 113)'; | ||
|
||
ctx.textAlign = 'left'; | ||
const text = tickValue.toString(); | ||
const textPositionYRight = tickY + 4; | ||
const opacity = 1; | ||
|
||
ctx.fillStyle = `rgba(182, 179, 175, ${opacity})`; | ||
ctx.fillText(text, textOffsetX, textPositionYRight); | ||
|
||
const maxTickY = centerY - 6 * tickSpacing; | ||
ctx.fillText('‰', width - MARGIN_LEFT, height / 12); // 12 ticks intervals | ||
console.log({ maxTickY, tickSpacing, height }, 'for ticks'); | ||
} | ||
ctx.stroke(); | ||
|
||
// prevent overlapping with margin top | ||
ctx.clearRect(0, 0, width, MARGIN_TOP); | ||
ctx.clearRect(width - MARGIN_LEFT, height - MARGIN_BOTTOM, width, MARGIN_BOTTOM); | ||
ctx.clearRect(0, height - MARGIN_BOTTOM + 6, width - MARGIN_LEFT, MARGIN_BOTTOM); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
ui-speedspacechart/src/components/layers/DeclivityLayer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
import type { Store } from '../../types/chartTypes'; | ||
import { drawDeclivity } from '../helpers/drawElements/declivity'; | ||
import { useCanvas } from '../hooks'; | ||
import cx from 'classnames'; | ||
|
||
type DeclivityLayerProps = { | ||
width: number; | ||
height: number; | ||
store: Store; | ||
}; | ||
|
||
const DeclivityLayer = ({ width, height, store }: DeclivityLayerProps) => { | ||
const canvas = useCanvas(drawDeclivity, width, height, store); | ||
|
||
return ( | ||
<canvas | ||
id="declivity-layer" | ||
className={cx('absolute rounded-t-xl', { 'bg-white-25': store.layersDisplay.declivities })} | ||
ref={canvas} | ||
width={width} | ||
height={height} | ||
/> | ||
); | ||
}; | ||
|
||
export default DeclivityLayer; |
Oops, something went wrong.