最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Formatter AxisLabel ECharts, Format label value - Stack Overflow

programmeradmin0浏览0评论

I have used BAR chart from ECharts and now I want to format its Y-Axis value to some other value.

Right now EChart generated my chart like above, But I want to display its Y-Axis value instead of 30,000 or 60,000 I want to display it as 30K, 60K, 80K.., and 150K.

I have tried with Formatter but it's not calling function runtime while EChart generating a chart to convert the value, Looks like this way we can just add Prefix/Suffix to the value

yAxis: [
    {
      type: 'value',
      data: [],
      axisLine: {
        show: true,
        lineStyle: {
          show: true,
          color: "black",
         },
       },
       axisLabel: {
         formatter: '{value} K'
       }
    },

I have also tried to give the function in formatter but I didn't find a way to pass the current value as a parameter in this function.

yAxis: [
    {
      type: 'value',
      data: [],
      axisLine: {
        show: true,
        lineStyle: {
          show: true,
          color: "black",
         },
       },
       axisLabel: {
         formatter: getFormattedValue(VALUE)
       }
    },

UPDATED: WITH NEW TRY, BUT STILL IT's NOT WORKING

When we use it like below on EChart official site then it's working

      axisLabel: {
        formatter: val => `${val / 1000}K`
      }

Updated chart with correct format

But when I use it like below then it's not working, Even function is not getting called, the Value remain as it is

      axisLabel: {
        formatter: val => `${val / 1000}K`
      }

Chart not getting updated

I have used BAR chart from ECharts and now I want to format its Y-Axis value to some other value.

Right now EChart generated my chart like above, But I want to display its Y-Axis value instead of 30,000 or 60,000 I want to display it as 30K, 60K, 80K.., and 150K.

I have tried with Formatter but it's not calling function runtime while EChart generating a chart to convert the value, Looks like this way we can just add Prefix/Suffix to the value

yAxis: [
    {
      type: 'value',
      data: [],
      axisLine: {
        show: true,
        lineStyle: {
          show: true,
          color: "black",
         },
       },
       axisLabel: {
         formatter: '{value} K'
       }
    },

I have also tried to give the function in formatter but I didn't find a way to pass the current value as a parameter in this function.

yAxis: [
    {
      type: 'value',
      data: [],
      axisLine: {
        show: true,
        lineStyle: {
          show: true,
          color: "black",
         },
       },
       axisLabel: {
         formatter: getFormattedValue(VALUE)
       }
    },

UPDATED: WITH NEW TRY, BUT STILL IT's NOT WORKING

When we use it like below on EChart official site then it's working https://echarts.apache/handbook/en/basics/release-note/5-3-0/#formatting-of-values-in-tooltip

      axisLabel: {
        formatter: val => `${val / 1000}K`
      }

Updated chart with correct format

But when I use it like below then it's not working, Even function is not getting called, the Value remain as it is

      axisLabel: {
        formatter: val => `${val / 1000}K`
      }

Chart not getting updated

Share Improve this question edited Mar 19, 2023 at 9:46 stefan 126k6 gold badges38 silver badges75 bronze badges asked Jan 8, 2023 at 10:22 Soft OnzSoft Onz 511 gold badge1 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You are very close to the solution, actually...

let option = {
  xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
  },
  yAxis: {
    type: 'value',
    axisLabel: {
      formatter: val => `${val / 1000}K`
    }
  },
  series: [
    {
      data: [30000, 60000, 90000, 120000, 150000],
      type: 'line'
    }
  ]
};

let myChart = echarts.init(document.getElementById('main'));
myChart.setOption(option);
#main {
  width: 100%;
  height: 350px;
}
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/echarts.min.js"></script>

<div id="main"></div>

The formatter is documented here: Documentation - Apache ECharts

发布评论

评论列表(0)

  1. 暂无评论