チャート
チャートはワークシートデータをチャートとしてレンダリングします。チャートモデルとUIパッケージは機能を提供します。
プラグインモード
pnpm add @univerjs-pro/chart-ui @univerjs-pro/sheets-chart @univerjs-pro/sheets-chart-uinpm install @univerjs-pro/chart-ui @univerjs-pro/sheets-chart @univerjs-pro/sheets-chart-uiyarn add @univerjs-pro/chart-ui @univerjs-pro/sheets-chart @univerjs-pro/sheets-chart-uibun add @univerjs-pro/chart-ui @univerjs-pro/sheets-chart @univerjs-pro/sheets-chart-uiimport ChartUIJaJP from '@univerjs-pro/chart-ui/locale/ja-JP'import { UniverSheetsChartPlugin } from '@univerjs-pro/sheets-chart'import { UniverSheetsChartUIPlugin } from '@univerjs-pro/sheets-chart-ui'import SheetsChartUIJaJP from '@univerjs-pro/sheets-chart-ui/locale/ja-JP'import SheetsChartJaJP from '@univerjs-pro/sheets-chart/locale/ja-JP'import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import '@univerjs-pro/chart-ui/facade'import '@univerjs-pro/sheets-chart/facade'import '@univerjs-pro/chart-ui/lib/index.css'import '@univerjs-pro/sheets-chart-ui/lib/index.css'const univer = new Univer({ locale: LocaleType.JA_JP, locales: { [LocaleType.JA_JP]: mergeLocales(ChartUIJaJP, SheetsChartJaJP, SheetsChartUIJaJP), },})univer.registerPlugin(UniverSheetsChartPlugin)univer.registerPlugin(UniverSheetsChartUIPlugin)モバイル対応
モバイルエディタを最初に構成します。 このページのプラグインモード設定では、デスクトップの登録を同じパッケージからモバイルエクスポートに置き換えて、ドキュメントを開く前にを実行します。 モデルプラグイン、スタイル、ロケール、ファサードのインポートを保存し、両方のバリアントを登録しないでください。
| パッケージ | デスクトップの登録 | モバイルの登録 |
|---|---|---|
@univerjs-pro/sheets-chart-ui | UniverSheetsChartUIPlugin | UniverSheetsChartMobileUIPlugin |
プリセットモード
@univerjs/preset-sheets-advancedは既にチャート機能が含まれていますが、実行時に描画プリセットが必要です。
pnpm add @univerjs/preset-sheets-drawing @univerjs/preset-sheets-advancednpm install @univerjs/preset-sheets-drawing @univerjs/preset-sheets-advancedyarn add @univerjs/preset-sheets-drawing @univerjs/preset-sheets-advancedbun add @univerjs/preset-sheets-drawing @univerjs/preset-sheets-advancedimport { UniverSheetsAdvancedPreset } from '@univerjs/preset-sheets-advanced'import UniverPresetSheetsAdvancedJaJP from '@univerjs/preset-sheets-advanced/locales/ja-JP'import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreJaJP from '@univerjs/preset-sheets-core/locales/ja-JP'import { UniverSheetsDrawingPreset } from '@univerjs/preset-sheets-drawing'import UniverPresetSheetsDrawingJaJP from '@univerjs/preset-sheets-drawing/locales/ja-JP'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-advanced/lib/index.css'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-drawing/lib/index.css'const { univerAPI } = createUniver({ locale: LocaleType.JA_JP, locales: { [LocaleType.JA_JP]: mergeLocales( UniverPresetSheetsCoreJaJP, UniverPresetSheetsDrawingJaJP, UniverPresetSheetsAdvancedJaJP, ), }, presets: [UniverSheetsCorePreset(), UniverSheetsDrawingPreset(), UniverSheetsAdvancedPreset()],})チャートを作成・更新する
worksheet.newChart(type)は、離脱したビルダーを返します。チャート情報はworksheet.insertChart(info)に構築され、渡されるまで、ワークシートに何も追加されません。
const workbook = univerAPI.getActiveWorkbook()if (!workbook) throw new Error('No active workbook')const worksheet = workbook.getActiveSheet()const info = worksheet .newChart(univerAPI.Enum.ChartTypeString.Column) .setSource({ range: 'A1:D6', orientation: univerAPI.Enum.ChartSourceOrientation.Columns, }) .setPosition('F2') .setSize(640, 360) .setCategoryField(0) .setValueFields([1, 2, 3]) .setTitle('Quarterly revenue') .build()const chart = await worksheet.insertChart(info)chart.setLegend({ visible: true }).bringToFront()const updatedInfo = chart.toBuilder(univerAPI.Enum.ChartTypeString.Line).setSubtitle('FY 2026').build()await chart.update(updatedInfo)const svg = await chart.exportImage({ format: 'svg' })インサートされたchartでセッターを呼び出すと、すぐに更新されます。toBuilder(type)とupdate(info)を使用して、完全に切り離されたコピーを取得したり、チャートタイプを変更したりできます。exportImage()は、UIのレンダラーが画像を生成することができないときにundefinedを返します。
worksheet.getCharts()とworksheet.getChart(id)はライブチャートを返却します。 await chart.remove()を呼び出してチャートを削除してください。 共有EChartsテーマをuniverAPI.registerTheme(name, theme)に登録してください。
グラフシート
insertChartSheet()は、チャート自体のタブを与えます。 チャートビルダーは、同じワークブックのグリッドシートを参照し、ソースセルはそのシートに残ります。
import '@univerjs-pro/sheets-chart/facade'const workbook = univerAPI.getActiveWorkbook()const data = workbook?.getSheetByName('Data')if (!workbook || !data) throw new Error('The Data sheet is required')const info = data .newChart(univerAPI.Enum.ChartTypeString.Column) .setSource({ sheetId: data.getSheetId(), range: 'A1:B4' }) .build()const chartSheet = await workbook.insertChartSheet(info, { name: 'Sales' })const primaryChart = chartSheet.getPrimaryChart()await chart.moveTo(targetSheet)は既存のチャートを移動します。チャートシートに移動したチャートは、そのシートに追加のチャートになります。チャートシートからプライマリチャートを移動すると、他に何も含まれていないタブが必要です。移動すると、そのタブが削除されます。移動は、元に戻り、元に戻すことができます。プライマリチャートのコンテキストメニューでは、チャートパネルを開く、チャートをコピーする、画像としてエクスポートする操作が行えますが、削除アクションはありません。
FWorkbook.insertChartSheet() · FWorksheet.getPrimaryChart()
誤差範囲
setErrorBars()は、ビルダーまたはライブチャート上で呼び出すことができます。 基本的な2Dカラム、バー、ライン、スキャッターチャートはエラーバーをサポートしています。 seriesIndex はゼロベースで、ソースデータ内のカテゴリフィールドをカウントします。 列と行は、y、バーはxを使用し、スキャッタチャートは、任意の数値軸を使用することができます。
chart.setErrorBars([ { seriesIndex: 1, direction: 'y', type: 'both', valueType: 'percentage', value: 20 },])複数レベルのカテゴリ軸
隣接するソース列が階層を形成する場合、ゼロベースのインデックスをルート・ツー・リーフ・オーダーでsetCategoryFields()に渡し、マルチ・レベル・軸を有効にします。
const info = worksheet .newChart(univerAPI.Enum.ChartTypeString.Column) .setSource({ range: 'A1:D7', orientation: univerAPI.Enum.ChartSourceOrientation.Columns, }) .setCategoryFields([0, 1]) .setMultiLevelCategoryAxis(true) .setValueFields([2, 3]) .setPosition('F2') .build()const chart = await worksheet.insertChart(info)chart.setCategoryFields([0, 1]).setMultiLevelCategoryAxis(true)clearCategoryFields()は、注文したマッピングを削除します。 setMultiLevelCategoryAxis(false) は選択したフィールドを保持し、階層的なレンダリングのみを無効にします。
レーダーチャート
レーダーチャートは、univerAPI.Enum.ChartRadarShape.Polygon(既定値)または univerAPI.Enum.ChartRadarShape.Circle を指定した setShape() と、各系列の領域を塗りつぶす setFill(true) を受け付けます。塗りつぶしを指定しない場合は、輪郭のみが描画されます。setRadarAxisLabel({ visible, color, fontSize, bold, italic, family, format }) は数値軸のラベルをスタイル設定し、resetRadarAxisLabel() は既定値に戻します。軸の目盛りラベルは既定で表示され、目盛りは5つの区間に分割されます。レーダーの線は系列の境界線から色、幅、破線スタイルを継承し、領域の塗りつぶしは系列の色と塗りつぶしの不透明度を使用します。
const info = worksheet .newChart(univerAPI.Enum.ChartTypeString.Radar) .setSource('A1:C6') .setCategoryField(0) .setValueFields([1, 2]) .setShape(univerAPI.Enum.ChartRadarShape.Circle) .setFill(true) .setRadarAxisLabel({ visible: true, fontSize: 12, bold: true }) .build()await worksheet.insertChart(info)FRadarChartBuilder.setRadarAxisLabel()
サポートされているチャート種類
univerAPI.Enum.ChartTypeString 次のタイプを提供します。 Line, LineStacked, LinePercentStacked, Column, ColumnStacked, ColumnPercentStacked, Bar, BarStacked, BarPercentStacked, Pie, Donut, Area, AreaStacked, AreaPercentStacked, Radar, Scatter, Combination, WordCloud, Funnel, Bubble, Relation, Waterfall, Pareto, Sankey, Heatmap, Boxplot, Candlestick, Histogram, Treemap, Sunburst, Gauge, と Chord.
チャートファサード参照 は共有ビルダーの設定 API を文書化します。
LineStackedとLinePercentStackedはスタンドアローン2Dチャートタイプです。サーバーレンダラーやOfficeファイル変換で使用する前に、ターゲットがそれらをサポートすることを確認します。
非表示データと個別カテゴリ
setPlotVisibleOnly(false)には、隠し行と列の値が含まれており、getPlotVisibleOnly()は現在の設定を読み込みます。 別のカテゴリの列を持つソースの場合、setSeriesToCategory() は各値フィールドインデックスをカテゴリフィールドインデックスにマップします。 インデックスは、フィルタリング後に残っているシリーズではなく、物理的なソースベクトルを参照します。 共有カテゴリを離れるエントリ、{}がオーバーライドをクリアします。
列A:Dでは、例えばDの値シリーズはCをカテゴリとして使うことができますが、BはAの共有カテゴリを保持します。
const chart = worksheet.getCharts()[0]if (!chart) throw new Error('No chart')chart.setPlotVisibleOnly(false)chart.setSeriesToCategory({ 3: [2] })集計レポートにはピボットグラフを利用できます。
3D グラフ
3Dチャートは同じプラグイン登録を使用します。3Dチャートタイプを選択し、その削除されたビルダーでビューを設定します。
サポートされるタイプはあります Line3D, Column3D, ColumnStacked3D, ColumnPercentStacked3D, Bar3D, BarStacked3D, BarPercentStacked3D, Pie3D, Area3D, AreaStacked3D, と AreaPercentStacked3D.
const info = worksheet .newChart(univerAPI.Enum.ChartTypeString.Column3D) .setSource('A1:C5') .setCategoryField(0) .setValueFields([1, 2]) .setPosition('F18') .setSize(640, 360) .setThreeD({ view: { rotX: 20, rotY: 30, rightAngleAxes: false, perspective: 30 }, gapDepth: 120, }) .build()await worksheet.insertChart(info)perspectiveはrightAngleAxesがfalseであるときだけ軸線ベースのチャートに適用される。 clearThreeD() は構成されたビューの設定をクリアしますが、3Dチャートタイプと深さ軸を保持します。 clearDepthAxis()を使用して深さの軸線をクリアします。 3D軸チャートは、二次軸や傾向を露出せず、Pie3Dではソリッドパイを描画します。
対数値軸
2Dライン、エリア、列、バー、コンビネーションチャートは、数値軸でscaleType: 'log'を受け入れます。バーチャートの水平数値軸の縦値軸とsetXAxis()用のsetYAxis()を使用します。 setRightYAxis()は、既存の二次数値軸を設定します。
デフォルトベースは10です。カスタムlogBaseはfiniteであり、2と1000の間でする必要があります。 2.5などの僅かな値が許可されています。 数値境界は正でなければなりません。 ゼロと負のデータポイントは、ログアリズムプロットから省略されますが、ソースデータは変更されません。 パーセントスタックされた3Dチャートは、ログクラウドをサポートしていません。
const info = worksheet .newChart(univerAPI.Enum.ChartTypeString.Line) .setSource('A1:B5') .setYAxis({ scaleType: 'log', logBase: 10 }) .setPosition('F36') .setSize(640, 360) .build()const chart = await worksheet.insertChart(info)await chart.update( chart.toBuilder(univerAPI.Enum.ChartTypeString.Line) .setYAxis({ scaleType: 'linear' }) .build(),)新しいチャートを投入し、それをビルドし、それをインサートします。 分離した更新を適用するには、await chart.update(builder.build())を使用します。 scaleType: 'linear'の設定は、logarithmベースを削除し、スケールが保存されていない軸パッチ。
このガイドはいかがですか?