-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
374 additions
and
142 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { Box, Typography, useTheme } from "@mui/material"; | ||
import React from "react"; | ||
import { Trans, useTranslation } from "react-i18next"; | ||
import { getGlobalHistogram } from "../../data/source/misc"; | ||
|
||
import { GameMode, HistogramData, HistogramGroup, PlayerExtendedStats } from "../../data/types"; | ||
import { formatPercent, sum, useAsyncFactory } from "../../utils"; | ||
import { useModel } from "../gameRecords/model"; | ||
|
||
function generatePath(bins: number[], barMax: number, start: number) { | ||
return `M ${start} 0 ` + bins.map((bin) => `h 1 V ${bin / barMax}`).join(" ") + " V 0 Z"; | ||
} | ||
|
||
function shouldUseClamped(value: number | undefined, data: HistogramGroup) { | ||
return ( | ||
typeof value !== "number" || | ||
(data.histogramClamped && value >= data.histogramClamped.min && value <= data.histogramClamped.max) | ||
); | ||
} | ||
const Histogram = React.memo(function ({ data, value }: { data?: HistogramGroup; value?: number }) { | ||
const theme = useTheme(); | ||
if (!data) { | ||
return <></>; | ||
} | ||
const histogram = shouldUseClamped(value, data) ? data.histogramClamped : data.histogramFull; | ||
if (!histogram) { | ||
return <></>; | ||
} | ||
if (value !== undefined) { | ||
value = Math.max(histogram.min, Math.min(histogram.max, value)); | ||
} | ||
const barMax = Math.max(...histogram.bins); | ||
const binStep = (histogram.max - histogram.min) / histogram.bins.length; | ||
const splitPoint = value === undefined ? histogram.bins.length : Math.ceil((value - histogram.min) / binStep); | ||
const meanBin = Math.floor((data.mean - histogram.min) / binStep); | ||
return ( | ||
<svg width={120} height={40} viewBox={`0 0 ${histogram.bins.length} 1`} preserveAspectRatio="none"> | ||
<g transform="scale(1, -1)" transform-origin="center"> | ||
<path d={generatePath(histogram.bins.slice(0, splitPoint), barMax, 0)} fill={theme.palette.grey[500]} /> | ||
{splitPoint < histogram.bins.length && ( | ||
<path d={generatePath(histogram.bins.slice(splitPoint), barMax, splitPoint)} fill={theme.palette.grey[800]} /> | ||
)} | ||
{!Number.isInteger(binStep) && histogram.bins.length > 60 && ( | ||
<line stroke={theme.palette.grey[50]} x1={meanBin} x2={meanBin} y1={0} y2={1} strokeDasharray={0.1} /> | ||
)} | ||
</g> | ||
</svg> | ||
); | ||
}); | ||
|
||
function getValuePosition(value: number, data: HistogramData) { | ||
const binStep = (data.max - data.min) / data.bins.length; | ||
const bin = Math.floor((value - data.min) / binStep); | ||
if (bin < 0) { | ||
return 0; | ||
} | ||
if (bin >= data.bins.length) { | ||
return sum(data.bins); | ||
} | ||
return sum(data.bins.slice(0, bin)) + data.bins[bin] * ((value - (data.min + binStep * bin)) / binStep); | ||
} | ||
|
||
export const StatHistogram = React.memo(function ({ | ||
statKey, | ||
value, | ||
valueFormatter, | ||
}: { | ||
statKey: keyof PlayerExtendedStats; | ||
value?: number; | ||
valueFormatter: (value: number) => string; | ||
}) { | ||
const { t } = useTranslation(); | ||
const [model] = useModel(); | ||
const globalHistogram = useAsyncFactory(() => getGlobalHistogram().catch(() => null), [], "globalHistogram"); | ||
if (!globalHistogram || model.type !== "player" || model.selectedModes.length !== 1) { | ||
return <></>; | ||
} | ||
const mode = model.selectedModes[0]; | ||
const modeHistogram = globalHistogram[mode]; | ||
if (!modeHistogram || !(statKey in modeHistogram["0"])) { | ||
return <></>; | ||
} | ||
const histogramData = modeHistogram["0"][statKey]; | ||
if (!histogramData?.histogramFull) { | ||
return <></>; | ||
} | ||
const numTotal = sum(histogramData.histogramFull.bins); | ||
const numPos = | ||
value === undefined | ||
? 0 | ||
: shouldUseClamped(value, histogramData) && histogramData.histogramClamped | ||
? getValuePosition(value, histogramData.histogramClamped) + | ||
getValuePosition(histogramData.histogramClamped.min, histogramData.histogramFull) | ||
: getValuePosition(value, histogramData.histogramFull); | ||
return ( | ||
<Box> | ||
<Typography variant="inherit" mb={2}> | ||
<Trans defaults="{{mode}}之间平均值:" values={{ mode: t(GameMode[mode]) }} /> | ||
{valueFormatter(histogramData.mean)} | ||
</Typography> | ||
<Histogram data={histogramData} value={value} /> | ||
{value !== undefined && ( | ||
<Typography variant="inherit"> | ||
<Trans defaults="{{mode}}之间位置:" values={{ mode: t(GameMode[mode]) }} /> | ||
{formatPercent(numPos / numTotal)} | ||
</Typography> | ||
)} | ||
</Box> | ||
); | ||
}); | ||
|
||
export default Histogram; |
Oops, something went wrong.
660ea52
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.
Successfully deployed to the following URLs: