This guide outlines the steps for adding a new example to the SciChart examples repository. The examples are designed to work across multiple frameworks (React, vanilla JavaScript/TypeScript, and Angular) and are dynamically loaded via Webpack contexts. All example-related files reside in the same folder to simplify sharing common data, libraries, and drawing logic.
Each example consists of several components:
-
exampleInfo File:
Each example includes anexampleInfofile (typicallyexampleInfo.tsx) containing metadata and configuration. For example:- In
BuilderApi/ChartFromJSON/exampleInfo.tsx, metadata is set using the functioncreateExampleInfoand includes fields such as:reactComponent: Specifies the React component name (e.g. "ChartFromJSON").id: Unique identifier for the example used as a key for the menu and throughout the app.imagePath: Filename for the example thumbnail.description: A detailed description with embedded markdown for emphasis.- Framework-specific data under the
frameworksobject, where each framework (React, JavaScript, Angular) has its own title, pageTitle, metaDescription, subtitle, and optionally markdownContent.
- Note that even though comments (e.g., "//// This metadata is computer generated - do not edit!") suggest that some fields are generated, developers must supply initial values when creating a new example.
- In
-
Framework Implementations:
Each example typically provides implementations for three fronts:- React:
A main file (usuallyindex.tsx) exports a React component that initializes the chart using SciChart. For instance, inBuilderApi/CustomTypes/index.tsx, the React component renders<SciChartReact>with an initialization function (drawExample). - Vanilla JavaScript/TypeScript:
Files such asvanilla.jsandvanilla.tsdemonstrate how to run the example without React. They import the samedrawExamplefunction and handle chart creation/disposal manually. - Angular:
An Angular component (e.g.angular.tsfrom the CustomTypes folder) shows how to integrate the drawing functionality into an Angular application by using components likeScichartAngularComponent.
- React:
-
Shared Drawing Logic:
The actual chart creation is encapsulated in shared files (e.g.,drawExample.jsanddrawExample.ts). These files:- Define the steps to build a SciChart surface.
- Register custom types, such as PaletteProviders (as seen in the CustomTypes examples).
- Include logic to generate data series and configure annotations. This allows all three implementations (React, vanilla, Angular) to use the same underlying drawing code.
-
Dynamic Loading Mechanism:
The repository employs a dynamic loading strategy:- In examplePages.ts, a Webpack context (
require.context) scans recursively for files matching the patternexampleInfo(.js|.ts|.tsx)in theExamplesdirectory. The metadata from these files constructs the collection of available examples. - In getExampleComponent.ts, the system dynamically loads the corresponding framework component by using:
- The
exampleDirectoryproperty (embedded in the metadata) to determine the folder. - The
reactComponentfield to locate the correct exported component in the correspondingindex.tsxfile. This design ensures that new examples are automatically integrated without manual registration.
- The
- In examplePages.ts, a Webpack context (
-
Copy an Existing Example Folder
Begin by duplicating an existing folder from the repository (for example, one fromBuilderApi/ChartFromJSONorBuilderApi/CustomTypes). This gives you a working template with correctly structured files:- An
exampleInfo.tsxfile with metadata. - Implementation files such as
index.tsx,vanilla.js/vanilla.ts, andangular.ts. - Shared drawing logic files (e.g.
drawExample.js/drawExample.ts).
- An
-
Create/Modify the
exampleInfoFile- Duplicate the Template:
Copy the existingexampleInfo.tsxfile into your new example folder. - Update Metadata:
Edit the metadata fields to reflect your new example:- Set
reactComponentto the name of your React component. - Choose unique value for
id. The pattern is to use the menu path/directory path, but this is not requrired. Examples can be in multiple places in the menu. eg featuredApps_showcases_fancyExample. - Update
imagePathwith the new thumbnail image filename. - Revise the
descriptionto accurately describe your example. - Modify all framework-specific properties in the
frameworksobject (titles, pageTitles, metaDescriptions, and subtitles). - Ensure fields such as
path,filepath, and any extra dependencies are updated as necessary.
- Set
Important: Although metadata comments (e.g., "This metadata is computer generated") might suggest not to modify it, initial values must be provided by the developer. These values will feed into the dynamic example loading during the build. IT IS ABSOLUTELY IMPERATIVE THAT THE COMMENTS ARE MAINTAINED IN THE CORRECT PLACE as this file will be the target for generated content.
-
Run script to add example to the list known by the app
npm run generateExamplePaths -
Add to the menu structure in components\AppRouter\examples.ts Use the id you set earlier. Examples can be in multiple places in the menu.
{ id: "multichart", title: "Multiple Charts", submenu: [ EXAMPLES_PAGES.chart2D_multiChart_syncMultiChart, EXAMPLES_PAGES.chart2D_createStockCharts_MultiPaneStockCharts, EXAMPLES_PAGES.featuredApps_showcases_servertrafficdashboard, // New entry EXAMPLES_PAGES.featuredApps_showcases_fancyExample ], },
- Duplicate the Template:
-
Implement the Example Code
- React Implementation:
Create or update the main React entry file (typicallyindex.tsx). This file should export a React component that calls the shared drawing function (e.g.,drawExample) wrapped in a SciChart React component. - Vanilla JavaScript/TypeScript Implementation:
Providevanilla.jsand/orvanilla.tsfiles that import and execute thedrawExamplefunction to set up the chart on a given HTML element. They should also include a cleanup mechanism. - Angular Implementation:
Develop an Angular component (such asangular.ts) that imports the drawing function and integrates it using SciChart’s Angular component (ScichartAngularComponent). The example should demonstrate the Angular-specific way of handling components and data binding.
- React Implementation:
-
Create Thumbnail for the Example
This image should be saved in the same folder as the example and referenced in the
exampleInfo.tsxfile under theimagePathandthumbnailImagefield. It should also have a unique name, related to the example it represents.- Recommended resolution: 750 x 500px.
For this you can either use Snipping Tool for Windows, or Preview for Mac (also, for Retina displays, the preview should be at 375 x 250px since it will be scaled up to 750 x 500px)
- Recommended format: JPG.
- Recommended size is between: 20kb and 50kb.
For format & size compression, I really like this repo: pic-smaller, since compressing more than 10 images at a time online often requires subscriptions. Tutorial to use locally: https://github.com/joye61/pic-smaller/blob/master/README.md#develop
- Recommended resolution: 750 x 500px.
-
Update sitemap by running:
npm run generateSitemap # this will generate a new `sitemap.xml` file-
Share Data and Libraries
By keeping all example files in one folder, you guarantee:- Consistent access to shared resources like themes (e.g.,
appTheme) and utility functions. - Simplified dependency management since all implementations refer to the same drawing logic.
- Consistent access to shared resources like themes (e.g.,
-
Dynamic Loading of Examples
The system automatically incorporates new examples using the following approach:- Webpack Context:
As described in examplePages.ts, the repository creates a Webpack context that looks forexampleInfofiles across the examples. This gathers all metadata into a single collection. - Component Importer:
getExampleComponent.ts uses the metadata (includingexampleDirectoryandreactComponent) to dynamically import the correct component for rendering.
Reviewing these two files will provide further insight into how your new example will be integrated during runtime.
- Webpack Context:
-
Final Notes and Recommendations
- Always start by copying a fully working
exampleInfofile from an existing example. - Ensure that all three framework implementations (React, vanilla JavaScript/TypeScript, and Angular) are updated consistently and correctly point to shared drawing logic.
- Verify that your metadata fields are updated accurately—even though some parts might be regenerated during the build, the initial configuration must be correct.
- Familiarize yourself with the dynamic example loading process by examining examplePages.ts and getExampleComponent.ts.
- If the example does not show up, check that you ran
npm run generateExamplePathsand that the example is listed in components\AppRouter\examplePaths.ts
- Always start by copying a fully working
By following these detailed steps and referring to the provided examples in your repository (such as those in the BuilderApi directories), developers can add new examples that seamlessly integrate into the dynamically loaded system across all supported frameworks.
Happy coding!