-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adding units to the gauge #580
Open
zoesteinkamp
wants to merge
7
commits into
master
Choose a base branch
from
unitsgauge
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea2421f
feat: adding the abilities to add bytes
zoesteinkamp 5d496b0
feat: adding more unit options
zoesteinkamp 0a409e3
fix: add usd
zoesteinkamp b39906c
fix: the line count issue
zoesteinkamp 1267e5d
fix: remove boolean
zoesteinkamp 8e68aec
fix: fixed the errors for the two units
zoesteinkamp 6ea52ff
doc: adding update to read docs
zoesteinkamp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ interface Props { | |
decimalPlaces: DecimalPlaces | ||
theme: GaugeTheme | ||
gaugeSize: number | ||
gaugeUnit: string[] | ||
} | ||
|
||
const resetCanvas = ( | ||
|
@@ -55,7 +56,7 @@ const updateCanvas = ( | |
canvasRef: React.MutableRefObject<HTMLCanvasElement>, | ||
props: Props | ||
): void => { | ||
const {width, height, colors, theme, gaugeSize} = props | ||
const {width, height, colors, theme, gaugeSize, gaugeUnit} = props | ||
resetCanvas(canvasRef, width, height) | ||
|
||
const canvas = canvasRef.current | ||
|
@@ -89,6 +90,15 @@ const updateCanvas = ( | |
) | ||
) | ||
|
||
//gauge line needs to be changed if gauge unit is being used | ||
if (gaugeUnit.toString() === 'bytes') { | ||
theme.lineCount = 5 | ||
} else if (gaugeUnit.toString() === 'time') { | ||
theme.lineCount = 5 | ||
} else if (gaugeUnit.toString() === 'USD') { | ||
theme.lineCount = 5 | ||
} | ||
|
||
// The following functions must be called in the specified order | ||
if (colors.length === MIN_THRESHOLDS) { | ||
drawGradientGauge( | ||
|
@@ -122,9 +132,17 @@ const updateCanvas = ( | |
theme, | ||
gaugeSize | ||
) | ||
drawGaugeLabels(ctx, radius, gradientThickness, minValue, maxValue, props) | ||
drawGaugeLabels( | ||
ctx, | ||
radius, | ||
gradientThickness, | ||
minValue, | ||
maxValue, | ||
gaugeUnit, | ||
props | ||
) | ||
drawGaugeValue(ctx, radius, labelValueFontSize, props) | ||
drawNeedle(ctx, radius, minValue, maxValue, props) | ||
drawNeedle(ctx, radius, minValue, maxValue, gaugeUnit, props) | ||
} | ||
|
||
const drawGradientGauge = ( | ||
|
@@ -295,55 +313,121 @@ const drawGaugeLabels = ( | |
gradientThickness: number, | ||
minValue: number, | ||
maxValue: number, | ||
gaugeUnit: string[], | ||
props: Props | ||
): void => { | ||
const {tickPrefix, tickSuffix, decimalPlaces, gaugeSize} = props | ||
const {lineCount, labelColor, labelFontSize} = props.theme | ||
|
||
const tickValues = [ | ||
...range(minValue, maxValue, Math.abs(maxValue - minValue) / lineCount), | ||
maxValue, | ||
] | ||
|
||
const labels = tickValues.map(tick => | ||
formatStatValue(tick, { | ||
decimalPlaces, | ||
prefix: tickPrefix, | ||
suffix: tickSuffix, | ||
}) | ||
) | ||
const startDegree = Math.PI - (gaugeSize - Math.PI) / 2 | ||
const arcLength = gaugeSize | ||
const arcIncrement = arcLength / lineCount | ||
let labelRadius: number | ||
|
||
// Format labels text | ||
ctx.font = `bold ${labelFontSize}px Rubik` | ||
ctx.fillStyle = labelColor | ||
ctx.textBaseline = 'middle' | ||
ctx.textAlign = 'right' | ||
let labelRadius: number | ||
|
||
for (let i = 0; i <= lineCount; i++) { | ||
labelRadius = radius + gradientThickness + 23 | ||
if (lineCount === 1 && i === 1) { | ||
ctx.textAlign = 'left' | ||
} else if (lineCount % 2 === 0 && lineCount / i === 2) { | ||
ctx.textAlign = 'center' | ||
} else if (i / (lineCount + 1) >= 0.5) { | ||
ctx.textAlign = 'left' | ||
if (gaugeUnit.toString() === 'bytes') { | ||
const labels = ['0b', '1024Kb', '1024Mb', '1024Gb', '1024Tb', '1024Pb'] | ||
const lineCount = 5 | ||
for (let i = 0; i <= lineCount; i++) { | ||
labelRadius = radius + gradientThickness + 23 | ||
if (i / (lineCount + 1) >= 0.5) { | ||
ctx.textAlign = 'left' | ||
} | ||
|
||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
ctx.fillText(labels[i], 0, 0) | ||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(-labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
} | ||
} else if (gaugeUnit.toString() === 'time') { | ||
const labels = ['0', '60s', '60m', '12h', '24h', '30d'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is the most useful way to do units. |
||
const lineCount = 5 | ||
for (let i = 0; i <= lineCount; i++) { | ||
labelRadius = radius + gradientThickness + 23 | ||
if (i / (lineCount + 1) >= 0.5) { | ||
ctx.textAlign = 'left' | ||
} | ||
|
||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
ctx.fillText(labels[i], 0, 0) | ||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(-labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
} | ||
} else if (gaugeUnit.toString() === 'USD') { | ||
const labels = ['0', '100', '1000', '1000m', '1000b', '1000t'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is the most useful way to do units. |
||
const lineCount = 5 | ||
for (let i = 0; i <= lineCount; i++) { | ||
labelRadius = radius + gradientThickness + 23 | ||
if (i / (lineCount + 1) >= 0.5) { | ||
ctx.textAlign = 'left' | ||
} | ||
|
||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
ctx.fillText(labels[i], 0, 0) | ||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(-labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
} | ||
} else { | ||
const tickValues = [ | ||
...range(minValue, maxValue, Math.abs(maxValue - minValue) / lineCount), | ||
maxValue, | ||
] | ||
|
||
const labels = tickValues.map(tick => | ||
formatStatValue(tick, { | ||
decimalPlaces, | ||
prefix: tickPrefix, | ||
suffix: tickSuffix, | ||
}) | ||
) | ||
|
||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
ctx.fillText(labels[i], 0, 0) | ||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(-labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
for (let i = 0; i <= lineCount; i++) { | ||
labelRadius = radius + gradientThickness + 23 | ||
if (lineCount === 1 && i === 1) { | ||
ctx.textAlign = 'left' | ||
} else if (lineCount % 2 === 0 && lineCount / i === 2) { | ||
ctx.textAlign = 'center' | ||
} else if (i / (lineCount + 1) >= 0.5) { | ||
ctx.textAlign = 'left' | ||
} | ||
|
||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
ctx.fillText(labels[i], 0, 0) | ||
ctx.rotate(startDegree) | ||
ctx.rotate(i * arcIncrement) | ||
ctx.translate(-labelRadius, 0) | ||
ctx.rotate(i * -arcIncrement) | ||
ctx.rotate(-startDegree) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -377,6 +461,7 @@ const drawNeedle = ( | |
radius: number, | ||
minValue: number, | ||
maxValue: number, | ||
gaugeUnit: string[], | ||
props: Props | ||
): void => { | ||
const {gaugePosition, gaugeSize, decimalPlaces} = props | ||
|
@@ -394,6 +479,9 @@ const drawNeedle = ( | |
digits = Math.min(digits, MAX_DECIMAL_PLACES) | ||
|
||
const formattedGaugePosition = Number(gaugePosition.toFixed(digits)) | ||
if (gaugeUnit.toString() === 'bytes') { | ||
maxValue = 5120 | ||
} | ||
|
||
if (formattedGaugePosition < minValue) { | ||
needleRotation = 0 - overflowDelta | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the most useful way to do units.