Skip to content

Instantly share code, notes, and snippets.

@hungtcs
Created July 22, 2019 10:00
Show Gist options
  • Save hungtcs/3ba0f26676123fff313ad12d6eb2e303 to your computer and use it in GitHub Desktop.
Save hungtcs/3ba0f26676123fff313ad12d6eb2e303 to your computer and use it in GitHub Desktop.
echarts tooltip formatter function with unit supported
import { EChartOption } from 'echarts';
declare interface _Format extends EChartOption.Tooltip.Format {
marker: string;
axisValueLabel: string;
}
const formatter: EChartOption.Tooltip.Formatter = function(params: _Format|Array<_Format>, _ticket, _callback) {
if(params instanceof Array) {
if(params.length) {
let message = '';
message += `${ params[0].axisValueLabel }`;
params.forEach(param => {
message += `<br/>${ param.marker }${ param.seriesName }: ${ param.value }${ param.data.unit || '' }`;
});
return message;
} else {
return null;
}
} else {
let message = '';
message += `${ params[0].axisValueLabel }`;
message += `<br/>${ params.marker }${ params.seriesName }: ${ params.value }${ params.data.unit || '' }`;
return message;
}
};
export default formatter;
@hungtcs
Copy link
Author

hungtcs commented Jul 22, 2019

Usage

import formatter from './tooltip-formatter.ts';

// echarts options
{
  series: [
    {
      data: array.map(item => ({ value: item.somevalue, unit: '%' })),
    },
  ],
  tooltip: {
    trigger: 'axis',
    formatter: formatter,
  },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment