【问题标题】:How to manipulate data in react-chartjs-2如何在 react-chartjs-2 中操作数据
【发布时间】:2020-11-05 12:16:00
【问题描述】:

我想在 y 轴上显示带有特定货币符号的数据,例如在工具提示中的突出显示区域。我已经自定义了工具提示,但是提供给图形的动态数据不受我控制。

这是我尝试过的代码

<Line
ref="chart"
data={ {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [
{
     label: "",
     fill: false,
     lineTension: 0,
     backgroundColor: "rgba(75,192,192,0.4)",
     borderColor: "#71C898",
     borderCapStyle: "butt",
     borderDash: [],
     borderDashOffset: 0.0,
     borderJoinStyle: "miter",
     pointBorderColor: "#71C898",
     pointBackgroundColor: "#2D9A5E",
     pointBorderWidth: 1,
     pointHoverRadius: 5,
     pointHoverBackgroundColor: "#71C898",
     pointHoverBorderColor: "rgba(220,220,220,1)",
     pointHoverBorderWidth: 2,
     pointRadius: 5,
     pointHitRadius: 10,
     data: this.props.data.length > 0 ? this.props.data : [],
}],
}} 
height={ 200 } 
options={ {
    tooltips: {
       callbacks: {
           label: (tooltipItems, data) => {
               return `${currencyFormatter(tooltipItems.value)}`
           }
       }
    }
}}
/>

【问题讨论】:

  • 你能把你试过的代码分享一下吗?

标签: reactjs chart.js customization react-chartjs-2


【解决方案1】:

Y 轴可以在 options => scales => yAxis => ticks 中格式化。在那里,您可以使用与工具提示相同的样式格式化标签。

    options={ 
      {
        tooltips: {
          callbacks: {
              label: (tooltipItems, data) => {
                  return `${currencyFormatter(tooltipItems.value)}`
              }
          }
        },
        scales:{
          yAxes:[
            {
              ticks: {
                callback: function(label, index, labels) {
                    return `${currencyFormatter(label)}`
                }
              },
            }
          ]
        }
      }
  }

【讨论】:

  • 感谢它对我的帮助。感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 2018-02-13
相关资源
最近更新 更多