Skip to content

Commit

Permalink
number fmt tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-williams committed Oct 12, 2023
1 parent 9ac1d8c commit 351fb36
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ export function Body() {
const label = `{ ${containerIdxs.map(idx => sets[idx].name).join(', ')} }`
const gridArea = totalRegionAreas && totalRegionAreas[key] || 0
const area = gridArea / curStep.total_area.v * curStep.targets.total_area
const areaLabel = fmt(area, 4)
const areaLabel = fmt(area)
const target = targets.all.get(key) || 0
// console.log("key:", key, "hoveredRegion:", hoveredRegion)
return (
Expand All @@ -1324,7 +1324,7 @@ export function Body() {
show={key == hoveredRegion}
overlay={<Tooltip className={css.regionTooltip} onMouseOver={() => setHoveredRegion(key)}>
<p className={css.regionTooltipLabel}>{label}</p>
{fmt(target, 4)}{areaLabel}
{fmt(target)}{areaLabel}
</Tooltip>}>
<text
transform={`translate(${center.x}, ${center.y}) scale(1, -1)`}
Expand Down
2 changes: 1 addition & 1 deletion src/components/tables/targets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function TargetsTable(
const valueStr =
editingValue && editingValue[0] == key
? editingValue[1]
: fmt(value, 4)
: fmt(value)
return <tr className={className} key={key}>
<td className={`${css.val} ${negativeKey}`}>{name}</td>
<td className={`${css.val} ${css.targetVal}`}>
Expand Down
10 changes: 8 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Edge from "./edge";
import {Point} from "../components/point";
import {XY} from "./ellipse";
import {r3} from "./math";
import { abs, r3 } from "./math";
import {Dispatch, SetStateAction} from "react";

export const cmp = (a: number, b: number) => a - b;
Expand Down Expand Up @@ -106,4 +106,10 @@ export const spaces = (n: number) => {

export type State<T> = [T, Dispatch<SetStateAction<T>>]

export const fmt = (value: number, n: number = 3) => value.toPrecision(n).replace(/\.?0+$/, '')
export const fmt = (value: number, n?: number) => {
if (n === undefined) {
// TODO: precision based on max sibling value
n = abs(value) >= 1000 ? 4 : 3
}
return value.toPrecision(n).replace(/\.?0+$/, '')
}

0 comments on commit 351fb36

Please sign in to comment.