11import { forwardRef , useMemo } from 'react' ;
22import { useApi } from '@deephaven/jsapi-bootstrap' ;
33import { useConnection } from '@deephaven/jsapi-components' ;
4- import { assertNotNull } from '@deephaven/utils' ;
54import {
65 ChartModel ,
76 ChartModelFactory ,
87 ChartTheme ,
98 useChartTheme ,
109} from '@deephaven/chart' ;
11- import type { dh as DhType , IdeConnection } from '@deephaven/jsapi-types' ;
10+ import type {
11+ dh as DhType ,
12+ Figure ,
13+ IdeConnection ,
14+ } from '@deephaven/jsapi-types' ;
1215import { IrisGridUtils } from '@deephaven/iris-grid' ;
1316import { getTimeZone , store } from '@deephaven/redux' ;
1417import { type WidgetComponentProps } from '@deephaven/plugin' ;
1518import {
1619 ChartPanelMetadata ,
1720 GLChartPanelState ,
1821 isChartPanelDehydratedProps ,
22+ isChartPanelFigureMetadata ,
1923 isChartPanelTableMetadata ,
2024} from './panels' ;
2125import ConnectedChartPanel , {
@@ -28,6 +32,7 @@ async function createChartModel(
2832 chartTheme : ChartTheme ,
2933 connection : IdeConnection ,
3034 metadata : ChartPanelMetadata ,
35+ fetch : ( ) => Promise < Figure > ,
3136 panelState ?: GLChartPanelState
3237) : Promise < ChartModel > {
3338 let settings ;
@@ -43,7 +48,9 @@ async function createChartModel(
4348 } else {
4449 settings = { } ;
4550 tableName = '' ;
46- figureName = metadata . name ?? metadata . figure ;
51+ figureName = isChartPanelFigureMetadata ( metadata )
52+ ? metadata . figure
53+ : metadata . name ;
4754 tableSettings = { } ;
4855 }
4956 if ( panelState != null ) {
@@ -64,26 +71,38 @@ async function createChartModel(
6471 }
6572 }
6673
74+ if ( figureName == null && tableName == null ) {
75+ const figure = await fetch ( ) ;
76+
77+ return ChartModelFactory . makeModel ( dh , settings , figure , chartTheme ) ;
78+ }
79+
6780 if ( figureName != null ) {
68- const definition = {
69- title : figureName ,
70- name : figureName ,
71- type : dh . VariableType . FIGURE ,
72- } ;
73- const figure = await connection . getObject ( definition ) ;
81+ let figure : Figure ;
82+
83+ if ( metadata . type === dh . VariableType . FIGURE ) {
84+ const definition = {
85+ name : figureName ,
86+ type : dh . VariableType . FIGURE ,
87+ } ;
88+ figure = await connection . getObject ( definition ) ;
89+ } else {
90+ figure = await fetch ( ) ;
91+ }
7492
7593 return ChartModelFactory . makeModel ( dh , settings , figure , chartTheme ) ;
7694 }
7795
7896 const definition = {
79- title : figureName ,
8097 name : tableName ,
8198 type : dh . VariableType . TABLE ,
8299 } ;
83100 const table = await connection . getObject ( definition ) ;
84- const timeZone = getTimeZone ( store . getState ( ) ) ;
85- assertNotNull ( timeZone ) ;
86- new IrisGridUtils ( dh ) . applyTableSettings ( table , tableSettings , timeZone ) ;
101+ new IrisGridUtils ( dh ) . applyTableSettings (
102+ table ,
103+ tableSettings ,
104+ getTimeZone ( store . getState ( ) )
105+ ) ;
87106
88107 return ChartModelFactory . makeModelFromSettings (
89108 dh ,
@@ -100,18 +119,17 @@ export const ChartPlugin = forwardRef(
100119 const chartTheme = useChartTheme ( ) ;
101120 const connection = useConnection ( ) ;
102121
122+ const panelState = isChartPanelDehydratedProps ( props )
123+ ? ( props as unknown as ChartPanelProps ) . panelState
124+ : undefined ;
125+
126+ const { fetch, metadata, localDashboardId } = props ;
127+
103128 const hydratedProps = useMemo (
104129 ( ) => ( {
105- ...( props as unknown as ChartPanelProps ) ,
106- metadata : props . metadata as ChartPanelMetadata ,
107- localDashboardId : props . localDashboardId ,
130+ metadata : metadata as ChartPanelMetadata ,
131+ localDashboardId,
108132 makeModel : ( ) => {
109- const { metadata } = props ;
110-
111- const panelState = isChartPanelDehydratedProps ( props )
112- ? ( props as unknown as ChartPanelProps ) . panelState
113- : undefined ;
114-
115133 if ( metadata == null ) {
116134 throw new Error ( 'Metadata is required for chart panel' ) ;
117135 }
@@ -121,15 +139,24 @@ export const ChartPlugin = forwardRef(
121139 chartTheme ,
122140 connection ,
123141 metadata as ChartPanelMetadata ,
142+ fetch as unknown as ( ) => Promise < Figure > ,
124143 panelState
125144 ) ;
126145 } ,
127146 } ) ,
128- [ props , dh , chartTheme , connection ]
147+ [
148+ dh ,
149+ connection ,
150+ fetch ,
151+ panelState ,
152+ metadata ,
153+ localDashboardId ,
154+ chartTheme ,
155+ ]
129156 ) ;
130157
131158 // eslint-disable-next-line react/jsx-props-no-spreading
132- return < ConnectedChartPanel ref = { ref } { ...hydratedProps } /> ;
159+ return < ConnectedChartPanel ref = { ref } { ...props } { ... hydratedProps } /> ;
133160 }
134161) ;
135162
0 commit comments