Skip to content

Commit

Permalink
Implement a link for each trial
Browse files Browse the repository at this point in the history
  • Loading branch information
keisuke-umezawa committed Sep 3, 2024
1 parent 8bba0d2 commit de2efee
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
10 changes: 10 additions & 0 deletions optuna_dashboard/ts/components/GraphHistory.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { useTheme } from "@mui/material"
import { PlotHistory } from "@optuna/react"
import React, { FC } from "react"
import { useNavigate } from "react-router-dom"
import { StudyDetail } from "ts/types/optuna"
import { useConstants } from "../constantsProvider"
import { usePlotlyColorTheme } from "../state"

export const GraphHistory: FC<{
studies: StudyDetail[]
logScale: boolean
includePruned: boolean
}> = ({ studies, logScale, includePruned }) => {
const { url_prefix } = useConstants()
const theme = useTheme()
const colorTheme = usePlotlyColorTheme(theme.palette.mode)
const linkURL = (studyId: number, trialNumber: number) => {
return url_prefix + `/studies/${studyId}/trials?numbers=${trialNumber}`
}
const navigate = useNavigate()

return (
<PlotHistory
studies={studies}
logScale={logScale}
includePruned={includePruned}
colorTheme={colorTheme}
linkURL={linkURL}
router={navigate}
/>
)
}
44 changes: 43 additions & 1 deletion tslib/react/src/components/PlotHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export const PlotHistory: FC<{
logScale?: boolean
includePruned?: boolean
colorTheme?: Partial<Plotly.Template>
}> = ({ studies, logScale, includePruned, colorTheme }) => {
linkURL?: (studyId: number, trialNumber: number) => string
// biome-ignore lint/suspicious/noExplicitAny: It will accept any routers of each library.
router?: any
}> = ({ studies, logScale, includePruned, colorTheme, linkURL, router }) => {
const { graphComponentState, notifyGraphDidRender } = useGraphComponentState()

const theme = useTheme()
Expand Down Expand Up @@ -108,6 +111,45 @@ export const PlotHistory: FC<{
colorThemeUsed,
markerSize
)?.then(notifyGraphDidRender)

const element = document.getElementById(plotDomId)
if (
element !== null &&
studies.length >= 1 &&
linkURL !== undefined &&
router !== undefined
) {
// @ts-ignore
element.on("plotly_click", (data) => {
if (data.points[0].data.mode !== "lines") {
let studyId = 1
if (data.points[0].data.name.includes("Infeasible Trial of")) {
const studyInfo: { id: number; name: string }[] = []
for (const study of studies) {
studyInfo.push({ id: study.id, name: study.name })
}
const dataPointStudyName = data.points[0].data.name.replace(
"Infeasible Trial of ",
""
)
const targetId = studyInfo.find(
(s) => s.name === dataPointStudyName
)?.id
if (targetId !== undefined) {
studyId = targetId
}
} else {
studyId = studies[Math.floor(data.points[0].curveNumber / 2)].id
}
const trialNumber = data.points[0].x
router(linkURL(studyId, trialNumber))
}
})
return () => {
// @ts-ignore
element.removeAllListeners("plotly_click")
}
}
}
}, [
historyPlotInfos,
Expand Down

0 comments on commit de2efee

Please sign in to comment.