Phi
Custom Chart
@dicehub/phiv1.0.0-beta.1

Custom Chart

Example charts using the Chart component.

Custom Chart

<Chart :echarts="echarts" :options="options" :height="400" />

Installation

Chart requires ECharts with the renderer and chart modules registered by the consumer.

pnpm add echarts

Imports

import { Chart, type PhiChartOption } from "@dicehub/phi/components/chart";

Usage

<script setup lang="ts">
import { Chart, type PhiChartOption } from "@dicehub/phi/components/chart";
import * as echarts from "echarts/core";
import { PieChart } from "echarts/charts";
import { TooltipComponent } from "echarts/components";
import { CanvasRenderer } from "echarts/renderers";

echarts.use([CanvasRenderer, PieChart, TooltipComponent]);

const options: PhiChartOption = {
  animation: true,
  animationDuration: 2000,
  tooltip: { show: true },
  series: [
    {
      type: "pie",
      data: [
        { value: 101, name: "Series A" },
        { value: 202, name: "Series B" },
        { value: 303, name: "Series C" },
        { value: 404, name: "Series D" },
        { value: 505, name: "Series E" },
      ],
    },
  ],
};
</script>

<template>
  <Chart :echarts="echarts" :options="options" :height="400" />
</template>

Examples

Custom Tooltip with HTML

Use dangerousHtmlFormatter when a tooltip needs trusted HTML content. Escape dynamic values before returning markup.

<script setup lang="ts">
import { Chart, type PhiChartOption } from "@dicehub/phi/components/chart";
import * as echarts from "echarts/core";
import { PieChart } from "echarts/charts";
import { TooltipComponent } from "echarts/components";
import { CanvasRenderer } from "echarts/renderers";

echarts.use([CanvasRenderer, PieChart, TooltipComponent]);

const options: PhiChartOption = {
  tooltip: {
    trigger: "item",
    dangerousHtmlFormatter(params) {
      const item = params as { name?: unknown; value?: unknown; percent?: unknown };
      const rawPercent = Number(item.percent ?? 0);
      const percent = Number.isFinite(rawPercent) ? Math.round(rawPercent) : 0;
      const safeName = echarts.format.encodeHTML(String(item.name ?? ""));
      const safeValue = echarts.format.encodeHTML(String(item.value ?? ""));
      const safePercent = echarts.format.encodeHTML(String(percent));

      return `
        <div style="padding: 8px;">
          <div style="font-weight: 600; margin-bottom: 4px;">${safeName}</div>
          <div>Value: <strong>${safeValue}</strong></div>
          <div style="font-size: 12px; opacity: 0.7; margin-top: 4px;">
            ${safePercent}% of total
          </div>
        </div>
      `;
    },
  },
  series: [
    {
      type: "pie",
      data: [
        { value: 101, name: "Series A" },
        { value: 202, name: "Series B" },
        { value: 150, name: "<img src=x onerror=alert('XSS')>" },
        { value: 303, name: "Series C" },
        { value: 404, name: "Series D" },
      ],
    },
  ],
};
</script>

<template>
  <Chart :echarts="echarts" :options="options" :height="400" />
</template>

API Reference

PropTypeDefaultDescription
echartstypeof echarts-ECharts core instance with the required modules registered.
optionsPhiChartOption-ECharts option object passed through to setOption.
optionUpdateBehaviorSetOptionOpts-Additional options passed to chart.setOption.
classNamestring-Optional class passed to the chart element.
aspectRationumber | string-Responsive container ratio used when height is not set.
heightnumber350Chart container height in pixels.
isDarkModebooleanfalseInitializes ECharts with dark mode and dark chart colors.
onEventsPartial<ChartEvents>-Event handlers registered on the ECharts instance.