Skip to content

Commit

Permalink
patch: format floats in chart min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
acheronfail committed Sep 28, 2024
1 parent 39cbee2 commit fae173f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/components/Chart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { ticks, type TickOptions } from '../lib/chart-helpers';
import type { MouseEventHandler, TouchEventHandler } from 'svelte/elements';
import { assert } from '../lib/misc';
import { assert, formatFloat } from '../lib/misc';
import { untrack } from 'svelte';
const DEFAULT_COLOUR = 'red';
Expand Down Expand Up @@ -352,15 +352,15 @@
{/if}
{#if minShown}
<div class="flex flex-row gap-2">
<span style:color={line.color}>min: {min}{unit}</span>
<span style:color={line.color}>min: {formatFloat(min)}{unit}</span>
</div>
{/if}
{#if maxShown && minShown}
<span class="text-slate-500">-</span>
{/if}
{#if maxShown}
<div class="flex flex-row gap-2">
<span style:color={line.color}>max: {max}{unit}</span>
<span style:color={line.color}>max: {formatFloat(max)}{unit}</span>
</div>
{/if}
{/if}
Expand Down
3 changes: 1 addition & 2 deletions src/components/Details.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import Button from './Button.svelte';
import { ChartColours } from '../lib/chart-helpers';
import { empty, State } from '../lib/parse/types';
import { formatFloat } from '../lib/misc';
let { data = empty, batterySpecs, mapSpeed, units }: Props = $props();
let voltsPerCell = $derived(batterySpecs.cellCount ? data.voltage / batterySpecs.cellCount : NaN);
let cellVoltsLow = $derived(voltsPerCell && batterySpecs.cellMinVolt && voltsPerCell < batterySpecs.cellMinVolt);
let formatSpeed = $derived((x: number) => (Number.isNaN(x) ? '??' : mapSpeed(x).toFixed(1)));
const formatFloat = (n: number) => (Number.isNaN(n) ? '??' : n.toFixed(1));
const getStateColor = (state: string): string | undefined => {
switch (state.toLowerCase()) {
case 'riding':
Expand Down
2 changes: 2 additions & 0 deletions src/lib/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ export const speedMapper = (inUnit: Units, outUnit: Units): ((input: number) =>
if (outUnit === Units.Imperial) return kmToMi;
return miToKm;
};

export const formatFloat = (n: number) => (Number.isNaN(n) ? '??' : n.toFixed(1));

0 comments on commit fae173f

Please sign in to comment.