DEV Community

XinYu Zhou
XinYu Zhou

Posted on

How to customize the shape of Tooltip in Vchart?

Title

How to customize the shape of Tooltip in Vchart?

Description

Is there a way to customize the shape of the tooltip in Vchart?

Solution

In the Vchart Spec, there are tooltip-related configurations. Configure the tooltip property to customize the tooltip shape. The mark attribute of the tooltip represents the effect when hovering over the graphic element. https://visactor.io/vchart/option/pieChart#tooltip.mark. The mark.content represents the configuration of the content. Configure the shapeType field in mark.content to customize the shape of the tooltip.

Code Example

const spec = {
  type: 'pie',
  data: [
    {
      id: 'id0',
      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' }
      ]
    }
  ],
  tooltip: { 
    mark: 
    { 
      content: { 
        key: datum => datum['type'],
        value: datum => datum['value'] + '%',
        shapeType: 'square' 
      } 
    },
  },
  outerRadius: 0.8,
  valueField: 'value',
  categoryField: 'type',
  title: {
    visible: true,
    text: 'Statistics of Surface Element Content'
  },
  legends: {
    visible: true,
    orient: 'left'
  },
  label: {
    visible: true
  }
};

const vchart = new VChart(spec, { dom: CONTAINER_ID });
vchart.renderSync();

// Just for the convenience of console debugging, DO NOT COPY!
window['vchart'] = vchart;
Enter fullscreen mode Exit fullscreen mode

Result

After running the code, the symbol of the tooltip becomes a rectangle.

Online demo: https://codesandbox.io/p/sandbox/tooltip-shape-cdzny7?file=%2Fsrc%2Findex.ts%3A44%2C2

Related Documents

Top comments (0)