コンテンツにスキップ
Page

Chart

プロットの基本要素で組み立てる棒グラフと折れ線グラフです。

MonTueWedThuFriSatSun
  • Visits
import {
Chart,
ChartBody,
ChartContent,
ChartPlot,
ChartGrid,
ChartBars,
ChartLine,
ChartYAxisLabels,
ChartAxisLabels,
ChartLegend,
ChartLegendItem,
} from "@pelagornis/page/components";

上のプレビューはこのファイルを描画したものです。src/components/ にコピーして使い始めてください。

src/components/ChartExample.tsx
import {
Chart,
ChartAxisLabels,
ChartBars,
ChartBody,
ChartContent,
ChartGrid,
ChartLegend,
ChartLegendItem,
ChartPlot,
ChartYAxisLabels,
} from "@pelagornis/page/components";
const visits = [45, 52, 38, 65, 42, 80, 55];
const days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
export default function ChartDemo() {
return (
<Chart className="w-full">
<ChartBody>
<ChartContent>
<ChartYAxisLabels values={visits} />
<ChartPlot aria-label="Weekly page visits">
<ChartGrid values={visits} />
<ChartBars values={visits} seriesColor="brand" />
</ChartPlot>
<ChartAxisLabels labels={days} />
</ChartContent>
</ChartBody>
<ChartLegend>
<ChartLegendItem seriesColor="brand">Visits</ChartLegendItem>
</ChartLegend>
</Chart>
);
}

Chart は React の状態を共有する複数のパーツで構成されます。MDX に書いた子要素はそれぞれ別のアイランドとして描画されるため、パーツ同士がつながりません。上の例のように .tsx ファイルで組み立て、そのファイルをインポートしてください。

src/content/docs/example.mdx
---
title: Example
---
import ChartExample from "../../components/ChartExample";
<ChartExample client:load />

client:load はページの読み込み時にアイランドをハイドレートします。ページの下の方にあるコンテンツには client:visible を使ってください。

Markdoc のタグは Astro コンポーネントを描画しますが、このコンポーネントにはクライアントの状態が必要です。例をハイドレートする .astro ファイルで包み、そのファイルをタグとして登録してください。

src/components/markdoc/ChartExample.astro
---
import ChartExample from "../ChartExample";
---
<ChartExample client:load />
markdoc.config.mjs
import { component, defineMarkdocConfig } from "@astrojs/markdoc/config";
import starlightMarkdoc from "@astrojs/starlight-markdoc";
import pageMarkdoc from "@pelagornis/page/markdoc";
export default defineMarkdocConfig({
extends: [starlightMarkdoc(), pageMarkdoc()],
tags: {
"chart-example": {
render: component("./src/components/markdoc/ChartExample.astro"),
selfClosing: true,
},
},
});
src/content/docs/example.mdoc
{% chart-example /%}

コンポーネントを構成するパーツと、それぞれが描画する data-refineui のロールです。

パーツロール
rootchart
axis_labelschart-axis-labels
barchart-bar
bar_trackchart-bar-track
barschart-bars
bodychart-body
contentchart-content
gridchart-grid
hit_areachart-hit-area
legendchart-legend
legend_itemchart-legend-item
linechart-line
line_areachart-line-area
line_strokechart-line-stroke
plotchart-plot
plot_framechart-plot-frame
pointchart-point
tooltipchart-tooltip
y_axischart-y-axis