How to Add a Component to Unovis
This guide will take you through the steps of adding a new component to our library.
Prerequisites
Before you start, make sure you familiarize yourself with our library's architecture. You should also be comfortable working with TypeScript and D3.
Getting Started
SVG vs HTML components
SVG components are rendered as g elements, and therefore are designed to exist inside of a svg
container (XYContainer or SingleContainer). HTML elements are designed to stand alone. In
general, "core" components that display data (i.e. charts, plots and diagrams) will be SVGs and
auxiliary components (i.e. annotations, legends and tooltips) will be HTML. Note the following
exceptions:
- A core component will be type HTML in special cases, for example when HTML canvas is needed for rendering (i.e. LeafletMap)
- An auxiliary component should be SVG if it is designed to exclusively work with XY data, in which case it should extend the XYComponentCore superclass which uses SVG type
Data Types
An important thing to consider beforehand is your component's expected data type. This will
determine the component's superclass (if applicable), the corresponding datamodel and generics.
We use the Series data model for components that expect an array of arbitrary values (type any[]).
This is the model used by XY Components and simpler charts like Donut. For more complex types, we
have Graph and MapGraph datamodels, which expect links that connect arbitrary data values through
source and target properties.
| Data model | typeof data |
|---|---|
| Series | any[] |
| Graph | { nodes: N[]; links: L[] } |
| MapGraph | { areas?: any[]; points?: N[]; links?: L[]; } |
where N extends NodeDatum and Link extends LinkDatum as defined below:
export interface NodeDatum {
id?: string;
}
export interface LinkDatum {
id?: number | string;
source: number | string | NodeDatum;
target: number | string | NodeDatum;
}
These data formats are not strictly defined because we also utilize accessor functions throughout the
library in config files. For example, NodeDatum could contain an id property, but to allow for
flexibility it would also be recommended to add a nodeId property to the component's configuration
with the type StringAccessor<NodeDatum>.
XY/Core vs Standalone components
Data-bound components are considered Core and belong in a container. There are two subtypes:
XY Components are for plotting data on XY axes/coordinate system and should extend
XYComponentCoreInterface and Core Components are for plotting any other kind of data and should
extend ComponentCoreInterface.
Auxiliary components should not extend from a superclass unless there is a valid reason to do so (for example if it is meant to work with XY data).
Component Code
File Structure
Your component will exist in the @unovis/ts library. Once you decide on a name for the component,
create a new directory inside src/components called your-component-name. Note that we use kebab
case naming convention for all files in this package.
The component directories follow this structure:
component-name
├── modules
│ └── # any extraneous logic/helper functions belong here in separate files
├── config.ts # ┓