SciChart.js React 보일러 플레이트
npm과 webpack을 사용하는 React 앱에서 SciChart.js를 활용하려면 공식 scichart-react 라이브러리를 사용하는 것을 권장합니다.
1단계: SciChart.js와 scichart-react 설치하기
아직 설치하지 않았다면, React 애플리케이션에 SciChart.js와 scichart-react를 추가해 주세요.
npm install scichart
npm install scichart-react
2단계: Wasm 파일 배포
SciChart.js는 WebAssembly 파일을 사용하므로, 이를 배포해야 합니다. 가장 간단한 방법은 node_modules/scichart/_wasm 폴더에서 출력 폴더로 wasm 파일을 복사하는 것입니다.
예: webpack.config.js를 사용하는 경우:
plugins: [
new CopyPlugin({
patterns: [
{ from: "src/index.html", to: "" },
{ from: "node_modules/scichart/_wasm/scichart2d.wasm", to: "" },
// 3D 차트를 사용하는 경우의 옵션
{ from: "node_modules/scichart/_wasm/scichart3d.wasm", to: "" },
],
})
],
참고: 도입을 간소화하기 위해, CDN에서 wasm을 불러오는 다른 방법도 이용할 수 있습니다
3단계: 차트 생성
그 후, 다음과 같이 SciChartSurface를 생성하는 함수를 만들 수 있습니다.
import {
SciChartSurface,
NumericAxis,
FastLineRenderableSeries,
XyDataSeries,
EllipsePointMarker,
SweepAnimation,
SciChartJsNavyTheme,
NumberRange
} from "scichart";
async function initSciChart() {
// SciChartSurface를 초기화합니다. await를 잊지 마세요!
const { sciChartSurface, wasmContext } = await SciChartSurface.create("scichart-root", {
theme: new SciChartJsNavyTheme(),
title: "SciChart.js First Chart",
titleStyle: { fontSize: 22 }
});
// growBy 패딩을 사용한 XAxis와 YAxis 생성
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 }));
// 초기 데이터를 포함한 선 그래프 시리즈 생성
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 };
}
4단계: React 컴포넌트 생성
차트는 SciChartReact 컴포넌트를 사용하여 초기화할 수 있습니다. 이를 통해 언마운트 시 제거를 포함한 컴포넌트의 전체 라이프사이클이 처리됩니다.
function App() {
return (
<div className="App">
<SciChartReact initChart={initSciChart}
onInit={(initResult) => console.log(initResult.sciChartSurface.id + `가 생성되었습니다`);
onDelete={(initResult) => console.log(initResult.sciChartSurface.id + `가 삭제되었습니다`);
style={{ maxWidth: 900 }} />
</div>
);
}
예제 동작 확인
더 자세히 알아보려면 Github에서 전체 예제를 확인하세요.
기타 사용 예시는 다음을 참조하십시오:
npm install
npm start
GitHub에서 열기