# react-chartjs-2の使い方②

- URL: https://tech.fusic.co.jp/posts/reactchartjs2-part2/
- 公開日: 2024-08-29
- 著者: Isse Teruhi
- タグ: react-chartjs-2, chartjs, chartjs-chart-boxplot
- 掲載: Fusic Tech Blog（株式会社Fusic）

> 今回は、react-chartjs-2を使用して箱ひげ図を作成しました。react-chartjs-2自体には箱ひげ図を描画する機能が含まれていないため、代わりにchartjs-chart-boxplot という Chart.js のモジュールを利用しています。

---

## 概要

今回は、react-chartjs-2を使用して箱ひげ図を作成しました。react-chartjs-2自体には箱ひげ図を描画する機能が含まれていないため、代わりにchartjs-chart-boxplot という Chart.js のモジュールを利用しています。

**注意:** まだお読みでない方は、react-chartjs-2の使い方①の記事もご覧ください。

[react-chartjs-2の使い方①](https://tech.fusic.co.jp/posts/reactchartjs2-part1/)

## Install

reactの開発プロジェクトで前回の記事のコマンドに加えて次のコマンドを実行してください。

```bash
npm install chart.js @sgratzl/chartjs-chart-boxplot
```

chartjs-chart-boxplotの利用にはchart.jsやreact-chartjs-2の導入が必要ですのでご注意ください。

## 描画できるグラフ

chartjs-chart-boxplotを使用することでchartjsのみでは描画できなかった次のようなグラフを描画できます。

* 箱ひげ図
* バイオリン図

## グラフ表示

今回は単純な箱ひげ図を表示してみました。コンポーネントを次のように作成しました。

```tsx
import { useRef } from 'react'
import {
  Chart as ChartJS,
  CategoryScale,
  Title,
  Tooltip,
  Legend,
  ChartOptions,
  ChartData,
} from 'chart.js'
import { Chart } from 'react-chartjs-2'
import zoomPlugin from 'chartjs-plugin-zoom'
import {
  BoxPlotController,
  BoxAndWiskers,
  BoxPlotDataPoint,
} from '@sgratzl/chartjs-chart-boxplot'
import { ZoomPluginOptions } from 'chartjs-plugin-zoom/types/options'

ChartJS.register(
  CategoryScale,
  BoxPlotController,
  BoxAndWiskers,
  Title,
  Tooltip,
  Legend,
  zoomPlugin
)

interface BoxplotProps {
  graphData: ChartData<'boxplot', BoxPlotDataPoint[], string>
  title: string
  xtext: string
  ytext: string
  className: string
}

export default function Boxplot({
  graphData,
  title,
  xtext,
  ytext,
  className,
}: BoxplotProps) {
  const chartRef = useRef<ChartJS<'boxplot', BoxPlotDataPoint[], string>>(null)
  
  const onResetZoom = () => {
    chartRef?.current?.resetZoom()
  }
  
  const zoomOptions: Partial<ZoomPluginOptions> = {
    zoom: {
      wheel: {
        enabled: true,
      },
      pinch: {
        enabled: true,
      },
      mode: 'y',
    },
    pan: {
      enabled: true,
      mode: 'y',
    },
  }

  const options: ChartOptions<'boxplot'> = {
    responsive: true,
    scales: {
      x: {
        title: {
          display: true,
          text: xtext,
        },
      },
      y: {
        title: {
          display: true,
          text: ytext,
        },
      },
    },
    plugins: {
      legend: {
        position: 'top',
      },
      title: {
        display: true,
        text: title,
      },
      zoom: zoomOptions,
    },
  }
  return (
    <>
      <Chart
        type="boxplot"
        options={options}
        data={graphData}
        ref={chartRef}
        className={className}
      />
      <button onClick={onResetZoom}>zoom reset</button>
    </>
  )
}
```

## コードの説明

### import

```tsx
import { useRef } from 'react'
import {
  Chart as ChartJS,
  CategoryScale,
  Title,
  Tooltip,
  Legend,
  ChartOptions,
  ChartData,
} from 'chart.js'
import { Chart } from 'react-chartjs-2'
import zoomPlugin from 'chartjs-plugin-zoom'
import {
  BoxPlotController,
  BoxAndWiskers,
  BoxPlotDataPoint,
} from '@sgratzl/chartjs-chart-boxplot'
import { ZoomPluginOptions } from 'chartjs-plugin-zoom/types/options'
```

* ①の記事で必要だったものに加えて(`Line`や`LineElement`などを除く)、`react-chartjs-2`から`Chart`と`@sgratzl/chartjs-chart-boxplot`から必要なものをインポートしています。
* `BoxPlotController`は箱ひげ図の描画、`BoxAndWiskers`は箱ひげ図のビジュアル要素の描画、`BoxPlotDataPoint`は箱ひげ図に必要なデータポイントの型定義に必要となります。

### ChartJS.register()

```tsx
ChartJS.register(
  CategoryScale,
  BoxPlotController,
  BoxAndWiskers,
  Title,
  Tooltip,
  Legend,
  zoomPlugin
)
```

* ①の記事で書いたものと同様に必要なものを登録します。今回は箱ひげ図を利用するのに必要な `BoxPlotController` と`BoxAndWiskers` を追加登録します。

### BoxplotProps

```tsx
interface BoxplotProps {
  graphData: ChartData<'boxplot', BoxPlotDataPoint[], string>
  title: string
  xtext: string
  ytext: string
  className: string
}

export default function Boxplot({
  graphData,
  title,
  xtext,
  ytext,
  className,
}: BoxplotProps) {
  const chartRef = useRef<ChartJS<'boxplot', BoxPlotDataPoint[], string>>(null)
```

* このインターフェースは、①の記事で定義されているものと同様に、`Boxplot`コンポーネントに渡されるプロパティを定義しています。

### 操作関数と各種オプション

```tsx
  const onResetZoom = () => {
    chartRef?.current?.resetZoom()
  }
  const zoomOptions: Partial<ZoomPluginOptions> = {
    zoom: {
      wheel: {
        enabled: true,
      },
      pinch: {
        enabled: true,
      },
      mode: 'y',
    },
    pan: {
      enabled: true,
      mode: 'y',
    },
  }

  const options: ChartOptions<'boxplot'> = {
    responsive: true,
    scales: {
      x: {
        title: {
          display: true,
          text: xtext,
        },
      },
      y: {
        title: {
          display: true,
          text: ytext,
        },
      },
    },
    plugins: {
      legend: {
        position: 'top',
      },
      title: {
        display: true,
        text: title,
      },
      zoom: zoomOptions,
    },
  }
```

* `onResetZoom` では箱ひげ図のズームをリセットするためのボタンを定義しています。
* `zoomOptions`,`options`では箱ひげ図のズームやパンの動作や制限の設定、箱ひげ図自体の設定を定義しています。設定オプションは①の記事に掲載されているものと同様です。詳しくは①の記事をご確認ください。

### コンポーネントのレンダリング

```tsx
  return (
    <>
      <Chart
        type="boxplot"
        options={options}
        data={graphData}
        ref={chartRef}
        className={className}
      />
      <button onClick={onResetZoom}>zoom reset</button>
    </>
  )
}
```

* `Chart`コンポーネントを使用して、チャートを描画します。
* 今回はズームやパンをリセットするボタンのみ追加しています。

### コンポーネントの使い方

```tsx
import Boxplot from './components/Boxplot.tsx'
// ...
<Boxplot title="sample graph" xtext="x軸のテキスト" ytext="y軸のテキスト" graphData={sampleBoxplotDatas}/>
```

* 使い方は前回の記事と同様です。

このコンポーネントを使うことで具体的に次のような箱ひげ図を表示できます。

![boxplot.png](https://d3t2k53lg8fjy7.cloudfront.net/public/f1b76c3cc9b56a1525ea521d1cb3b372c1f53d2c.png "boxplot")

以上になります。今回は参考にできるドキュメントが少なく、実装に苦労しましたが、無事に完成させることができました。

## 参考にした記事・文献

* [chartjs-chart-boxplot公式ドキュメント](https://www.sgratzl.com/chartjs-chart-boxplot/)
* [chart.js 公式ドキュメント](https://www.chartjs.org/)
* [chartjs-plugin-zoom 公式ドキュメント](https://www.chartjs.org/chartjs-plugin-zoom/latest/)
