このチュートリアルでは、StrelitLayout、HighCharts、および Yahoo Finance API のデータを使用して、複数の過去株価チャートを含むレイアウトを作成する方法を解説します。より実践的で本格的なユースケースを想定しています。
対話型サンプル
対話型サンプル: HighCharts、YQL、および StrelitLayout
CodePen 上で完全なレイアウト実装を確認、フォーク、テストできます:
アーキテクチャと構成
このサンプルは 3 つの部分で構成されています:
Yahoo Finance から株価データを取得する
StockDataRequestクラスHighCharts、Strelit Layout、およびデータプロバイダー間の対話を処理する
StockChartComponentクラス- レイアウトを構成および初期化するブートストラップスクリプト
このチュートリアルでは、StockChartComponent クラスとレイアウト構成に焦点を当てます。
Strelit Layout 構成
StrelitLayout の構成から始めましょう:
const config = {
root: {
type: 'row',
content: [
{
type: 'component',
componentType: 'stockChart',
componentState: {
companyName: 'Google Inc.',
tickerSymbol: 'GOOGL',
color: '#22c55e',
},
},
{
type: 'component',
componentType: 'stockChart',
componentState: {
companyName: 'Apple Inc.',
tickerSymbol: 'AAPL',
color: '#38bdf8',
},
},
],
},
};
const layoutContainer = document.getElementById('layoutContainer');
const myLayout = new StrelitLayout(layoutContainer);
myLayout.registerComponentConstructor('stockChart', StockChartComponent);
myLayout.loadLayout(config);
行内に 2 つのコンポーネントが並ぶ基本レイアウトを作成します(Strelit Layout
の設定にまだ慣れていない場合は、
はじめにチュートリアル を参照してください)。
componentState 属性には 3 つのキーが含まれています:
companyName: チャートのタイトルおよび系列の説明に使用されますtickerSymbol: 株価を識別し、対象のヒストリカルデータをダウンロードするために使用されますcolor: プロット線の色を定義します
StockChartComponent の実装
StockChart コンポーネントは、HighCharts、Strelit Layout、およびデータソースを連携させる架け橋です。完全なクラス実装は次のとおりです:
class StockChartComponent {
constructor(container, itemConfig) {
const state = itemConfig.componentState || {};
this._container = container;
this._state = state;
this._chart = null;
this._highChartsConfig = {
title: { text: state.companyName + ' の過去株価' },
series: [],
plotOptions: { line: { marker: { enabled: false } } },
xAxis: { type: 'datetime' },
yAxis: { title: { text: '価格 (USD)' } },
chart: {
renderTo: container.element, // ネイティブ HTMLElement を直接サポート
},
};
this._container.setTitle(state.companyName + ' のチャート');
this._container.on('open', () => this._createChart());
}
_createChart() {
this._chart = new Highcharts.Chart(this._highChartsConfig);
this._chart.showLoading('データを読み込み中...');
new StockDataRequest(this._state.tickerSymbol, (data) => this._onDataReady(data));
}
_onDataReady(highchartsData) {
this._chart.addSeries({
color: this._state.color,
name: this._state.companyName,
data: highchartsData,
});
this._chart.hideLoading();
this._bindContainerEvents();
}
_bindContainerEvents() {
this._container.on('resize', () => this._setSize());
this._container.on('destroy', () => {
if (this._chart) this._chart.destroy();
});
}
_setSize() {
if (this._chart) {
this._chart.setSize(this._container.width, this._container.height);
}
}
}
注目すべき重要なポイント:
container.element によるネイティブ DOM への直接描画
モダンな Strelit UI Kit では、container.element がネイティブ HTMLElement
を直接返します。HighCharts の renderTo: container.element に直接渡すことができます。
タイトルの設定
this._container.setTitle(state.companyName + ' のチャート');
Strelit Layout
のすべてのアイテムはタイトルを持つことができます。タイトルはタブやウィンドウヘッダーに表示され、構成で渡すか、
container.setTitle( title ) を呼び出して動的に変更できます。
コンテナが open するまでチャート生成を遅延
this._container.on('open', this._createChart.bind(this));
Strelit Layout がコンポーネントを作成した直後は、まだサイズが確定していないか DOM
に追加されていない場合があります。HighCharts や SlickGrid
などのライブラリは配置先の要素サイズを計測して自身を初期化するため、コンテナの open
イベントまで待機することが推奨されます。
リサイズの処理
StockChartComponent.prototype._bindContainerEvents = function () {
this._container.on('resize', this._setSize.bind(this));
};
StockChartComponent.prototype._setSize = function () {
this._chart.setSize(this._container.width, this._container.height);
};
コンポーネントのリフローが CSS のみで管理されている場合はコンテナサイズに自動追従しますが、Canvas
や SVG チャートを再描画する場合は、resize
イベントを購読して明示的にサイズを更新します。
クリーンアップとメモリ管理
this._container.on('destroy', () => {
if (this._chart) this._chart.destroy();
});
コンポーネントが破棄されたときに内部リソースを解放することは、メモリリークを防ぐために不可欠です。