|
| 1 | +import ScatterPlot from '@veupathdb/components/lib/plots/ScatterPlot'; |
| 2 | +import { isFaceted } from '@veupathdb/components/lib/types/guards'; |
| 3 | +import { |
| 4 | + useDataClient, |
| 5 | + useFindEntityAndVariable, |
| 6 | + useStudyMetadata, |
| 7 | +} from '@veupathdb/eda/lib/core'; |
| 8 | +import { DocumentationContainer } from '@veupathdb/eda/lib/core/components/docs/DocumentationContainer'; |
| 9 | +import { scatterplotResponseToData } from '@veupathdb/eda/lib/core/components/visualizations/implementations/ScatterplotVisualization'; |
| 10 | +import { useCachedPromise } from '@veupathdb/eda/lib/core/hooks/cachedPromise'; |
| 11 | +import { VariableDescriptor } from '@veupathdb/eda/lib/core/types/variable'; |
| 12 | +import { WorkspaceContainer } from '@veupathdb/eda/lib/workspace/WorkspaceContainer'; |
| 13 | +import { edaServiceUrl } from '../../config'; |
| 14 | + |
| 15 | +interface Props { |
| 16 | + datasetId: string; |
| 17 | + xAxisVariable: VariableDescriptor; |
| 18 | + yAxisVariable: VariableDescriptor; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * A simplified EDA ScatterPlot component. |
| 23 | + * |
| 24 | + * This will render a plot and a legend. |
| 25 | + */ |
| 26 | +export function EdaScatterPlot(props: Props) { |
| 27 | + const { datasetId } = props; |
| 28 | + return ( |
| 29 | + <DocumentationContainer> |
| 30 | + <WorkspaceContainer studyId={datasetId} edaServiceUrl={edaServiceUrl}> |
| 31 | + <ScatterPlotAdapter {...props} /> |
| 32 | + </WorkspaceContainer> |
| 33 | + </DocumentationContainer> |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +interface AdapterProps { |
| 38 | + xAxisVariable: VariableDescriptor; |
| 39 | + yAxisVariable: VariableDescriptor; |
| 40 | +} |
| 41 | + |
| 42 | +function ScatterPlotAdapter(props: AdapterProps) { |
| 43 | + const { xAxisVariable, yAxisVariable } = props; |
| 44 | + const { id: studyId } = useStudyMetadata(); |
| 45 | + const dataClient = useDataClient(); |
| 46 | + const findEntityAndVariable = useFindEntityAndVariable(); |
| 47 | + const data = useCachedPromise( |
| 48 | + async function getData() { |
| 49 | + const response = await dataClient.getScatterplot('xyrelationships', { |
| 50 | + studyId, |
| 51 | + filters: [], |
| 52 | + config: { |
| 53 | + outputEntityId: xAxisVariable.entityId, |
| 54 | + valueSpec: 'raw', |
| 55 | + xAxisVariable, |
| 56 | + yAxisVariable, |
| 57 | + // maxAllowedDataPoints: 1_000, |
| 58 | + }, |
| 59 | + }); |
| 60 | + return scatterplotResponseToData(response).dataSetProcess; |
| 61 | + }, |
| 62 | + ['ScatterPlotAdapter', studyId, xAxisVariable, yAxisVariable] |
| 63 | + ); |
| 64 | + |
| 65 | + const xAxisEntityAndVariable = findEntityAndVariable(xAxisVariable); |
| 66 | + const yAxisEntityAndVariable = findEntityAndVariable(yAxisVariable); |
| 67 | + |
| 68 | + if (isFaceted(data.value)) { |
| 69 | + throw new Error('Received unexpected faceted data.'); |
| 70 | + } |
| 71 | + |
| 72 | + if (data.error) { |
| 73 | + return <div>Error: {String(data.error)}</div>; |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <ScatterPlot |
| 78 | + interactive |
| 79 | + showSpinner={data.pending} |
| 80 | + markerBodyOpacity={0.5} |
| 81 | + data={data.value} |
| 82 | + dependentAxisLabel={yAxisEntityAndVariable?.variable.displayName} |
| 83 | + independentAxisLabel={xAxisEntityAndVariable?.variable.displayName} |
| 84 | + /> |
| 85 | + ); |
| 86 | +} |
0 commit comments