Skip to content

Commit caf2a4b

Browse files
committed
Added comments
1 parent 371755f commit caf2a4b

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

chartlets.js/src/lib/components/Plot.tsx

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,47 @@ import type { TopLevelParameter } from "vega-lite/src/spec/toplevel";
66
import type { TopLevelSelectionParameter } from "vega-lite/src/selection";
77
import type { Stream } from "vega-typings/types/spec/stream";
88

9+
type SignalHandler = (signalName: string, value: unknown) => void;
10+
911
export 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+
1337
export 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]) => {

chartlets.js/src/lib/types/state/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type CSSProperties } from "react";
22
import { isObject } from "@/lib/utils/isObject";
3-
import type {TopLevelSpec} from "vega-lite/src/spec";
3+
import type { TopLevelSpec } from "vega-lite/src/spec";
44

55
export type ComponentType =
66
| "Box"
@@ -54,7 +54,7 @@ export interface CheckboxState extends ComponentState {
5454
export interface PlotState extends ComponentState {
5555
type: "Plot";
5656
chart:
57-
| TopLevelSpec
57+
| TopLevelSpec // This is the vega-lite specification type
5858
| null;
5959
}
6060

0 commit comments

Comments
 (0)