如何在 react-chartjs-2 中操作数据
How to manipulate data in react-chartjs-2
我想在 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)}`
}
}
}
}}
/>
Y-axis可以在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)}`
}
},
}
]
}
}
}
我想在 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)}`
}
}
}
}}
/>
Y-axis可以在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)}`
}
},
}
]
}
}
}