-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add confidence intervals to line and bar chart #111
- adapt colors to colorblind friendly colors
- Loading branch information
1 parent
6e66e2d
commit 7243c6d
Showing
24 changed files
with
470 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
45 changes: 45 additions & 0 deletions
45
components/src/preact/components/confidence-interval-selector.tsx
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,45 @@ | ||
import { type FunctionComponent } from 'preact'; | ||
|
||
import { Select } from './select'; | ||
import type { ConfidenceIntervalMethod } from '../shared/charts/confideceInterval'; | ||
|
||
type ConfidenceIntervalSelectorProps = { | ||
confidenceIntervalMethod: ConfidenceIntervalMethod; | ||
setConfidenceIntervalMethod: (method: ConfidenceIntervalMethod) => void; | ||
confidenceIntervalMethods: ConfidenceIntervalMethod[]; | ||
}; | ||
|
||
export const ConfidenceIntervalSelector: FunctionComponent<ConfidenceIntervalSelectorProps> = ({ | ||
confidenceIntervalMethod, | ||
setConfidenceIntervalMethod, | ||
confidenceIntervalMethods, | ||
}) => { | ||
if (confidenceIntervalMethods.length === 0) { | ||
return null; | ||
} | ||
|
||
const items = [ | ||
{ label: 'Confidence interval method', value: 'none', disabled: true }, | ||
...confidenceIntervalMethods.concat('none').map((method) => { | ||
switch (method) { | ||
case 'wilson': | ||
return { label: 'Wilson, 95% CI', value: 'wilson' }; | ||
case 'none': | ||
return { label: 'None', value: 'none' }; | ||
} | ||
}), | ||
]; | ||
|
||
return ( | ||
<Select | ||
items={items} | ||
selected={confidenceIntervalMethod === undefined ? 'none' : confidenceIntervalMethod} | ||
onChange={(event: Event) => { | ||
const select = event.target as HTMLSelectElement; | ||
const value = select.value as ConfidenceIntervalMethod; | ||
setConfidenceIntervalMethod(value); | ||
}} | ||
selectStyle={'select-xs select-bordered'} | ||
/> | ||
); | ||
}; |
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
106 changes: 106 additions & 0 deletions
106
components/src/preact/prevalenceOverTime/prevalence-over-time-bar-chart.tsx
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,106 @@ | ||
import { Chart, type ChartConfiguration, registerables, type TooltipItem } from 'chart.js'; | ||
import { BarWithErrorBar, BarWithErrorBarsController } from 'chartjs-chart-error-bars'; | ||
|
||
import { type PrevalenceOverTimeData, type PrevalenceOverTimeVariantData } from '../../query/queryPrevalenceOverTime'; | ||
import GsChart from '../components/chart'; | ||
import { LogitScale } from '../shared/charts/LogitScale'; | ||
import { singleGraphColorRGBA } from '../shared/charts/colors'; | ||
import { type ConfidenceIntervalMethod, wilson95PercentConfidenceInterval } from '../shared/charts/confideceInterval'; | ||
import { getYAxisScale, type ScaleType } from '../shared/charts/getYAxisScale'; | ||
|
||
interface PrevalenceOverTimeBarChartProps { | ||
data: PrevalenceOverTimeData; | ||
yAxisScaleType: ScaleType; | ||
confidenceIntervalMethod: ConfidenceIntervalMethod; | ||
} | ||
|
||
Chart.register(...registerables, LogitScale, BarWithErrorBarsController, BarWithErrorBar); | ||
|
||
const PrevalenceOverTimeBarChart = ({ | ||
data, | ||
yAxisScaleType, | ||
confidenceIntervalMethod, | ||
}: PrevalenceOverTimeBarChartProps) => { | ||
const config: ChartConfiguration = { | ||
type: BarWithErrorBarsController.id, | ||
data: { | ||
labels: data[0]?.content.map((dateRange) => dateRange.dateRange?.toString() ?? 'Unknown') || [], | ||
datasets: data.map((graphData, index) => datasets(graphData, index, confidenceIntervalMethod)), | ||
}, | ||
options: { | ||
animation: false, | ||
scales: { | ||
// @ts-expect-error-next-line -- chart.js typings are not complete with custom scales | ||
y: getYAxisScale(yAxisScaleType), | ||
}, | ||
plugins: { | ||
legend: { | ||
display: false, | ||
}, | ||
tooltip: tooltip(confidenceIntervalMethod), | ||
}, | ||
}, | ||
}; | ||
|
||
return <GsChart configuration={config} />; | ||
}; | ||
|
||
const datasets = ( | ||
prevalenceOverTimeVariant: PrevalenceOverTimeVariantData, | ||
index: number, | ||
confidenceIntervalMethod: ConfidenceIntervalMethod, | ||
) => { | ||
const generalConfig = { | ||
borderWidth: 1, | ||
pointRadius: 0, | ||
label: prevalenceOverTimeVariant.displayName, | ||
backgroundColor: singleGraphColorRGBA(index, 0.3), | ||
borderColor: singleGraphColorRGBA(index), | ||
}; | ||
|
||
switch (confidenceIntervalMethod) { | ||
case 'wilson': | ||
return { | ||
...generalConfig, | ||
data: prevalenceOverTimeVariant.content.map((dataPoint) => ({ | ||
y: dataPoint.prevalence, | ||
yMin: wilson95PercentConfidenceInterval(dataPoint.count, dataPoint.total).lowerLimit, | ||
yMax: wilson95PercentConfidenceInterval(dataPoint.count, dataPoint.total).upperLimit, | ||
})), | ||
}; | ||
default: | ||
return { | ||
...generalConfig, | ||
data: prevalenceOverTimeVariant.content.map((dataPoint) => dataPoint.prevalence), | ||
}; | ||
} | ||
}; | ||
|
||
const tooltip = (confidenceIntervalMethod: ConfidenceIntervalMethod) => { | ||
const generalConfig = { | ||
mode: 'index' as const, | ||
intersect: false, | ||
}; | ||
|
||
switch (confidenceIntervalMethod) { | ||
case 'wilson': | ||
return { | ||
...generalConfig, | ||
callbacks: { | ||
label: (context: TooltipItem<'barWithErrorBars'>) => { | ||
const value = context.dataset.data[context.dataIndex] as { | ||
y: number; | ||
yMin: number; | ||
yMax: number; | ||
}; | ||
|
||
return `${context.dataset.label}: ${value.y.toFixed(3)} (${value.yMin.toFixed(3)} - ${value.yMax.toFixed(3)})`; | ||
}, | ||
}, | ||
}; | ||
default: | ||
return generalConfig; | ||
} | ||
}; | ||
|
||
export default PrevalenceOverTimeBarChart; |
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
49 changes: 0 additions & 49 deletions
49
components/src/preact/prevalenceOverTime/prevalence-over-time-line-bar-chart.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.