首頁  >  問答  >  主體

如何使用 vue.js 從 ApexChart 的選項中呼叫方法

我是 vue 和 apex 圖表的新手,基本上我需要的是從 apex 圖表選項呼叫方法,我創建了一個顯示我遇到的問題的檔案:

https://jsfiddle.net/wr3uo5va/

我需要從 chartOptions.dataLabels 呼叫方法 currencyValue

    dataLabels: {
      enabled: true,
      offsetX: -25,
      formatter: function(val) {
        return val + " Reais";  <--- This works
       // return this.currencyValue(val)  <--- This does not work
      },
    },

有什麼建議嗎?

P粉373596828P粉373596828205 天前324

全部回覆(2)我來回復

  • P粉662089521

    P粉6620895212024-03-28 15:58:45

    您可以將 chartOptions 放在方法中而不是資料中。 下面是工作程式碼

    const currencyValue = (val) => {
      return "R$" + val;
    }
    
    new Vue({
      el: "#app",
      data() {
        return {
          series: [450, 300, 500]
        }
      },
      methods: {
        chartOptions() {
          return {
            labels: ['Paid', 'Pending', 'Rejected'],
            plotOptions: {
                radialBar: {
                    size: 165,
                    offsetY: 30,
                    hollow: {
                        size: '20%'
                    },
                    track: {
                        background: "#ebebeb",
                        strokeWidth: '100%',
                        margin: 15,
                    },
                    dataLabels: {
                        show: true,
                        name: {
                            fontSize: '18px',
                        },
                        value: {
                            fontSize: '16px',
                            color: "#636a71",
                            offsetY: 11
                        },
                        total: {
                            show: true,
                            label: 'Total',
                            formatter: function() {
                                return 42459
                            }
                        }
                    }
                },
            },
            responsive: [{
                breakpoint: 576,
                options: {
                    plotOptions: {
                        radialBar: {
                            size: 150,
                            hollow: {
                                size: '20%'
                            },
                            track: {
                                background: "#ebebeb",
                                strokeWidth: '100%',
                                margin: 15,
                            },
                        }
                    }
                }
            }],
            colors: ['#7961F9', '#FF9F43', '#EA5455'],
            fill: {
                type: 'gradient',
                gradient: {
                    // enabled: true,
                    shade: 'dark',
                    type: 'vertical',
                    shadeIntensity: 0.5,
                    gradientToColors: ['#9c8cfc', '#FFC085', '#f29292'],
                    inverseColors: false,
                    opacityFrom: 1,
                    opacityTo: 1,
                    stops: [0, 100]
                },
            },
            stroke: {
                lineCap: 'round'
            },
            chart: {
                dropShadow: {
                    enabled: true,
                    blur: 3,
                    left: 1,
                    top: 1,
                    opacity: 0.1
                },
            },
            tooltip: {
              x: {
                formatter: function (val) {
                  return val;
                },
              },
              y: {
                formatter: function (val) {
                  return currencyValue(val);
                },
              },
            },            
          }
        }
      },
      components: {
        VueApexCharts
      }
    })

    方法不能在 datacompulated 中調用,可以在 methods 中調用

    html 中需要修改的一件事如下

    
    

    回覆
    0
  • P粉078945182
  • 取消回覆