SciChart.js Vue Boilerplate
To use SciChart.js in a Vue.js app with npm, carry out the following steps:
Step 1: Install SciChart.js
If you haven’t already, add SciChart.js to your Vue.js application.
npm install scichart
Step 2: Wasm file deployment
SciChart.js uses WebAssembly files which must be served. The easiest way to do this is to copy the wasm files from the node_modules/scichart/_wasm folder to your output folder.
Vue.js requires *.data file to be in the /public output folder, and *.wasm files to be in the /public/js folder. This is done using the copy-files-from-to
npm package:
// copy-files-from-to.json
{
"copyFilesSettings": {
"whenFileExists": "overwrite"
},
"copyFiles": [
{
"from": "./node_modules/scichart/_wasm/scichart2d.data",
"to": "./public/scichart2d.data"
},
{
"from": "./node_modules/scichart/_wasm/scichart2d.wasm",
"to": "./public/js/scichart2d.wasm"
},
{
"from": "./node_modules/scichart/_wasm/scichart3d.data",
"to": "./public/scichart3d.data"
},
{
"from": "./node_modules/scichart/_wasm/scichart3d.wasm",
"to": "./public/js/scichart3d.wasm"
}
]
}
// package.json
{
// ...
"scripts": {
"copyWasm": "copy-files-from-to --config copy-files-from-to.json",
"dev": "npm run copyWasm && vue-cli-service serve",
"build": "npm run copyWasm && vue-cli-service build",
// ...
},
// ...
"devDependencies": {
"copy-files-from-to": "^3.2.1",
}
Note: other methods to load wasm from CDN are available to simplify getting started
Step 3: Initialising the Chart
Create a function which initialises a SciChartSurface like this:
import {
SciChartSurface,
NumericAxis,
FastLineRenderableSeries,
XyDataSeries,
EllipsePointMarker,
SweepAnimation,
SciChartJsNavyTheme,
NumberRange
} from "scichart";
async function initSciChart() {
// LICENSING
// Commercial licenses set your license code here
// Purchased license keys can be viewed at https://www.scichart.com/profile
// How-to steps at https://www.scichart.com/licensing-scichart-js/
// SciChartSurface.setRuntimeLicenseKey("YOUR_RUNTIME_KEY");
// Initialize SciChartSurface. Don't forget to await!
const { sciChartSurface, wasmContext } = await SciChartSurface.create("scichart-root", {
theme: new SciChartJsNavyTheme(),
title: "SciChart.js First Chart",
titleStyle: { fontSize: 22 }
});
// Create an XAxis and YAxis with growBy padding
const growBy = new NumberRange(0.1, 0.1);
sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { axisTitle: "X Axis", growBy }));
sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { axisTitle: "Y Axis", growBy }));
// Create a line series with some initial data
sciChartSurface.renderableSeries.add(new FastLineRenderableSeries(wasmContext, {
stroke: "steelblue",
strokeThickness: 3,
dataSeries: new XyDataSeries(wasmContext, {
xValues: [0,1,2,3,4,5,6,7,8,9],
yValues: [0, 0.0998, 0.1986, 0.2955, 0.3894, 0.4794, 0.5646, 0.6442, 0.7173, 0.7833]
}),
pointMarker: new EllipsePointMarker(wasmContext, { width: 11, height: 11, fill: "#fff" }),
animation: new SweepAnimation({ duration: 300, fadeEffect: true })
}));
return sciChartSurface;
}
Step 4: Creating a Vue Component
Next, create a vue component:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<div id="scichart-root" style="width: 600px; height: 400px; margin: auto;"></div>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
async function initSciChart() {
// defined above
}
export default defineComponent({
// Best practise in Vue.js is to ensure that sciChartSurface is deleted on component unmount.
// Here's one way to do this
data() {
return {
chartInitializationPromise: undefined,
};
},
mounted() {
console.log("SciChart2d.vue onMounted");
this.chartInitializationPromise = initSciChart();
},
beforeUnmount() {
console.log("SciChart2d.vue beforeUnmount");
this.chartInitializationPromise.then((sciChartSurface) => {
console.log("..deleting sciChartSurface");
sciChartSurface.delete();
});
},
name: "scichart2d",
props: {
msg: String
}
});
</script>
Step 5: Include Components in your App
Finally, include Vue components in your app:
<template>
<Scichart2d msg="2D Charts" />
</template>
<script lang="ts">
import { defineComponent } from "vue";
import Scichart2d from "./components/Scichart2d.vue";
export default defineComponent({
name: "App",
components: {
Scichart2d
}
});
</script>
Running the example
Get the full example on Github to explore further.
npm install
npm start
Open On Github