在环形图中,中心区域有大量的空白,我想利用这些区域,在单击某个扇区时,在图表正中间显示该扇形对应的数值。
VChart的为饼图了设计了indicator组件,支持配置指标的标题与多行文本内容,提供对fixed
和 hover
两种交互方式与常用的样式配置功能。
import { useEffect, useRef } from "react";
import VChart from "@visactor/vchart";
export const Chart = () => {
const containerRef = useRef(null);
useEffect(() => {
const spec = {
type: "pie",
data: [
{
id: "data",
values: [
{ type: "oxygen", value: "46.60" },
{ type: "silicon", value: "27.72" },
{ type: "aluminum", value: "8.13" },
{ type: "iron", value: "5" },
{ type: "calcium", value: "3.63" },
{ type: "sodium", value: "2.83" },
{ type: "potassium", value: "2.59" },
{ type: "others", value: "3.5" },
],
},
],
outerRadius: 0.6,
innerRadius: 0.4,
padAngle: 0.6,
valueField: "value",
categoryField: "type",
pie: {
style: {
cornerRadius: 10,
},
state: {
hover: {
outerRadius: 0.65,
},
selected: {
outerRadius: 0.65,
},
},
},
label: {
visible: true,
},
indicator: {
visible: true,
trigger: "fixed",
title: {
visible: true,
style: {
fontSize: 18,
text: (data) => {
if (data) {
const value = data["type"];
return value ? value : null;
}
return null;
},
},
},
content: [
{
visible: true,
style: {
fontSize: 18,
text: "",
},
},
{
visible: true,
style: {
fontSize: 18,
text: (data) => {
if (data) {
const value = data["value"];
return value ? `${value}%` : null;
}
return null;
},
},
},
],
},
};
const vchart = new VChart(spec, {
dom: containerRef.current,
});
vchart.renderSync();
return () => vchart.release();
}, []);
return (
<div
ref={containerRef}
style={{
width: 520,
height: 520,
border: "1px solid #ccc",
}}
/>
);
};
在线示例:https://codesandbox.io/s/quizzical-babycat-wcgpzk?file=/src/A...
8 回答4.7k 阅读✓ 已解决
6 回答3.4k 阅读✓ 已解决
5 回答2.8k 阅读✓ 已解决
6 回答2.3k 阅读
5 回答6.3k 阅读✓ 已解决
4 回答2.3k 阅读✓ 已解决
4 回答2.8k 阅读✓ 已解决
可以参考如下options