zenno/mspeed: calculator for front runner mechanical speed bonuses
This commit is contained in:
@@ -2,23 +2,42 @@
|
||||
import * as Plot from '@observablehq/plot';
|
||||
import * as d3 from 'd3';
|
||||
import { Stat } from './race';
|
||||
import type { ComputedSeries } from './chart';
|
||||
import type { ComputedSeries, HorizontalRule } from './chart';
|
||||
import type { ClassValue } from 'svelte/elements';
|
||||
import type { Attachment } from 'svelte/attachments';
|
||||
|
||||
interface Props {
|
||||
/** The stat the chart shows. */
|
||||
stat: Stat;
|
||||
/** Series to show in the chart. */
|
||||
y: ComputedSeries | Array<ComputedSeries | null>;
|
||||
/** Label for the dependent variable. */
|
||||
yLabel: string;
|
||||
range?: [number, number];
|
||||
/**
|
||||
* Range of the dependent variable to show.
|
||||
* If not given, the limits are the minimum and maximum values plotted.
|
||||
* If given as a triple, the second value is the default maximum and
|
||||
* the third is the maximum when the chart is expanded to 2000.
|
||||
*/
|
||||
range?: [number, number] | [number, number, number];
|
||||
/** Range of the stat to plot. */
|
||||
xRange?: [number, number] | null;
|
||||
/**
|
||||
* Vertical rules to place on the graph.
|
||||
* Each rule gets a corresponding horizontal rule at the intersection
|
||||
* with each series.
|
||||
*/
|
||||
xRule?: number | number[];
|
||||
/**
|
||||
* Horizontal rules to place on the graph.
|
||||
*/
|
||||
yRule?: HorizontalRule[];
|
||||
|
||||
class?: ClassValue | null;
|
||||
plotOptions?: Omit<Plot.PlotOptions, 'marks' | 'x' | 'y'>;
|
||||
}
|
||||
|
||||
let { stat, y, yLabel, range, xRange, xRule = [], class: className, plotOptions = {} }: Props = $props();
|
||||
let { stat, y, yLabel, range, xRange, xRule = [], yRule = [], class: className, plotOptions = {} }: Props = $props();
|
||||
|
||||
let width = $state(0);
|
||||
let height = $state(0);
|
||||
@@ -48,7 +67,10 @@
|
||||
const vals = $derived(series.flatMap(({ y, label }) => xVal.map((x) => ({ x, y: y(x), label }))));
|
||||
const yRange: [number, number] = $derived.by(() => {
|
||||
if (range != null) {
|
||||
return range;
|
||||
if (range.length === 2) {
|
||||
return range;
|
||||
}
|
||||
return [range[0], expand ? range[2] : range[1]];
|
||||
}
|
||||
const l = d3.min(vals, ({ y }) => y) ?? 0;
|
||||
const r = d3.max(vals, ({ y }) => y) ?? 1;
|
||||
@@ -81,6 +103,8 @@
|
||||
Plot.ruleX([thrX], { strokeOpacity: 0.25 }),
|
||||
Plot.ruleX(xLines, { strokeOpacity: 0.5 }),
|
||||
Plot.ruleY(yLines, { y: 'y', stroke: 'label', strokeOpacity: 0.5 }),
|
||||
Plot.ruleY(yRule, { y: 'y', strokeOpacity: 0.75 }),
|
||||
Plot.tip(yRule, { x: xMax, y: 'y', title: 'label', anchor: 'top-right', className: 'plot-tip' }),
|
||||
Plot.frame(),
|
||||
Plot.line(vals, { x: 'x', y: 'y', stroke: 'label', strokeWidth: 3 }),
|
||||
Plot.tip(vals, Plot.pointerY({ x: 'x', y: 'y', stroke: 'label', className: 'plot-tip' })),
|
||||
|
||||
Reference in New Issue
Block a user