@@ -6,28 +6,47 @@ import type { TopLevelParameter } from "vega-lite/src/spec/toplevel";
66import type { TopLevelSelectionParameter } from "vega-lite/src/selection" ;
77import type { Stream } from "vega-typings/types/spec/stream" ;
88
9+ type SignalHandler = ( signalName : string , value : unknown ) => void ;
10+
911export interface PlotProps extends Omit < PlotState , "type" > {
1012 onChange : ComponentChangeHandler ;
1113}
1214
15+ /*
16+ There are two types of Parameters in Vega-lite. Variable and Selection parameters. We need to check if the provided
17+ parameter in the chart from the server is a Selection parameter so that we can extract the selection event types
18+ (point or interval) and further, the events from the `select.on` property (e.g. click, mouseover, keydown etc.) if
19+ that property `on` exists. We need these events and their names to create the signal listeners and pass them to the
20+ Vega-lite element for event-handling (signal-listeners).
21+ */
22+ function isTopLevelSelectionParameter (
23+ param : TopLevelParameter ,
24+ ) : param is TopLevelSelectionParameter {
25+ return "select" in param ;
26+ }
27+
28+ /*
29+ The signal name extracted from the `select.on` property can be either a string or of Stream type (internal Vega
30+ type). But since we create the selection events in Altair (in the server) using `on="click"` etc., the event type
31+ will be a string, but we need this type-guard to be sure. If it is a Stream object, that case is not handled yet.
32+ */
33+ function isString ( signal_name : string | Stream ) : signal_name is string {
34+ return typeof signal_name === "string" ;
35+ }
36+
1337export function Plot ( { id, style, chart, onChange } : PlotProps ) {
1438 if ( ! chart ) {
1539 return < div id = { id } style = { style } /> ;
1640 }
1741
18- function isTopLevelSelectionParameter (
19- param : TopLevelParameter ,
20- ) : param is TopLevelSelectionParameter {
21- return "select" in param ;
22- }
23-
24- function isString ( signal_name : string | Stream ) : signal_name is string {
25- return typeof signal_name === "string" ;
26- }
27-
2842 const signals : { [ key : string ] : string } = { } ;
2943
44+ /*
45+ Here, we loop through all the params to create map of signals which will be then used to create the map of
46+ signal-listeners
47+ */
3048 chart . params ?. forEach ( ( param ) => {
49+ console . log ( "param" , param ) ;
3150 if ( isTopLevelSelectionParameter ( param ) ) {
3251 if (
3352 typeof param . select === "object" &&
@@ -37,6 +56,12 @@ export function Plot({ id, style, chart, onChange }: PlotProps) {
3756 const signal_name = param . select . on ;
3857 if ( isString ( signal_name ) ) {
3958 signals [ signal_name ] = param . name ;
59+ } else {
60+ console . warn (
61+ "The signal " +
62+ param +
63+ " is of Stream type (internal Vega-lite type) which is not handled yet." ,
64+ ) ;
4065 }
4166 }
4267 }
@@ -55,13 +80,16 @@ export function Plot({ id, style, chart, onChange }: PlotProps) {
5580 }
5681 } ;
5782
58- type SignalHandler = ( signalName : string , value : unknown ) => void ;
59-
60- // Currently, we only have click events support, but if more are required, they can be implemented and added in the map below.
83+ /*
84+ Currently, we only have click events support, but if more are required, they can be implemented and added in the map below.
85+ */
6186 const signalHandlerMap : { [ key : string ] : SignalHandler } = {
6287 click : handleClickSignal ,
6388 } ;
6489
90+ /*
91+ This function creates the map of signal listeners based on the `signals` map computed above.
92+ */
6593 const createSignalListeners = ( signals : { [ key : string ] : string } ) => {
6694 const signalListeners : { [ key : string ] : SignalHandler } = { } ;
6795 Object . entries ( signals ) . forEach ( ( [ event , signalName ] ) => {
0 commit comments