Reactjs 更改Chart.js图例文本
我正在尝试查看Chart.js是否可以将图例文本从默认的“right”更改为“left”。我还需要能够在usePointStyle的两侧放置两种类型的数据。我目前正在使用ReactJS 这就是我目前拥有的: 我需要Chart.js将图例文本呈现为: 我试着在chart.js上查找整个文档,但结果是空手而归 以下是我的代码片段:Reactjs 更改Chart.js图例文本,reactjs,chart.js,Reactjs,Chart.js,我正在尝试查看Chart.js是否可以将图例文本从默认的“right”更改为“left”。我还需要能够在usePointStyle的两侧放置两种类型的数据。我目前正在使用ReactJS 这就是我目前拥有的: 我需要Chart.js将图例文本呈现为: 我试着在chart.js上查找整个文档,但结果是空手而归 以下是我的代码片段: <Doughnut width={120} height={100} data={tradeFileNames}
<Doughnut
width={120}
height={100}
data={tradeFileNames}
options={{
cutoutPercentage: 55,
elements: {
center: {
text: `${numeral(total).format("0,0")} ${innerTopText} ${innerMiddleText} ${innerBottomText}`,
fontColor: "#666666",
fontFamily: "Allianz-Neo",
fontStyle: "bold",
minFontSize: 15,
maxFontSize: 20
}
},
plugins: {
outlabels: {
backgroundColor: "white", // Background color of Label
borderColor: "none", // Border color of Label
borderRadius: 0, // Border radius of Label
borderWidth: 0, // Thickness of border
color: "black", // Font color
display: false,
lineWidth: 1, // Thickness of line between chart arc and Label
padding: 0,
lineColor: "black",
textAlign: "center",
stretch: 45,
},
labels: false
},
legend: {
display: true,
position: "right",
align: "center",
fontFamily: "Allianz-Neo",
textDirection: 'ltr',
labels: {
usePointStyle: true,
fontColor: "#006192",
}
}
}}
/>
我相信您需要在
选项中添加一个legendCallback
-请参阅此处的HTML图例部分:
- 优点:你可以渲染任何你想要的东西
- 缺点:您需要自己调用
generateLegend
来生成图例
这里有一个解决方案,您可以尝试:
如果您应用了该代码,您的外观如下所示:
<>
<Doughnut
ref="chart"
width={120}
height={100}
data={tradeFileNames}
options={{
cutoutPercentage: 55,
elements: {
center: {
text: `${numeral(total).format("0,0")} ${innerTopText} ${innerMiddleText} ${innerBottomText}`,
fontColor: "#666666",
fontFamily: "Allianz-Neo",
fontStyle: "bold",
minFontSize: 15,
maxFontSize: 20
}
},
plugins: {
outlabels: {
backgroundColor: "white", // Background color of Label
borderColor: "none", // Border color of Label
borderRadius: 0, // Border radius of Label
borderWidth: 0, // Thickness of border
color: "black", // Font color
display: false,
lineWidth: 1, // Thickness of line between chart arc and Label
padding: 0,
lineColor: "black",
textAlign: "center",
stretch: 45,
},
labels: false
},
legendCallback: (chart) => {
// return whatever you need here based on chart.data
},
legend: {
display: true,
position: "right",
align: "center",
fontFamily: "Allianz-Neo",
textDirection: 'ltr',
labels: {
usePointStyle: true,
fontColor: "#006192",
}
}
}}
/>
{this.refs.chart && this.refs.chart.chartInstance.generateLegend()}
</>
{
//根据chart.data在此处返回所需内容
},
图例:{
显示:对,
位置:“对”,
对齐:“居中”,
fontFamily:“新安联”,
textDirection:'ltr',
标签:{
usePointStyle:true,
fontColor:#006192“,
}
}
}}
/>
{this.refs.chart&&this.refs.chart.chartInstance.GenerateGend()}
我相信您需要在选项中添加一个legendCallback
-请参阅此处的HTML图例部分:
- 优点:你可以渲染任何你想要的东西
- 缺点:您需要自己调用
generateLegend
来生成图例
这里有一个解决方案,您可以尝试:
如果您应用了该代码,您的外观如下所示:
<>
<Doughnut
ref="chart"
width={120}
height={100}
data={tradeFileNames}
options={{
cutoutPercentage: 55,
elements: {
center: {
text: `${numeral(total).format("0,0")} ${innerTopText} ${innerMiddleText} ${innerBottomText}`,
fontColor: "#666666",
fontFamily: "Allianz-Neo",
fontStyle: "bold",
minFontSize: 15,
maxFontSize: 20
}
},
plugins: {
outlabels: {
backgroundColor: "white", // Background color of Label
borderColor: "none", // Border color of Label
borderRadius: 0, // Border radius of Label
borderWidth: 0, // Thickness of border
color: "black", // Font color
display: false,
lineWidth: 1, // Thickness of line between chart arc and Label
padding: 0,
lineColor: "black",
textAlign: "center",
stretch: 45,
},
labels: false
},
legendCallback: (chart) => {
// return whatever you need here based on chart.data
},
legend: {
display: true,
position: "right",
align: "center",
fontFamily: "Allianz-Neo",
textDirection: 'ltr',
labels: {
usePointStyle: true,
fontColor: "#006192",
}
}
}}
/>
{this.refs.chart && this.refs.chart.chartInstance.generateLegend()}
</>
{
//根据chart.data在此处返回所需内容
},
图例:{
显示:对,
位置:“对”,
对齐:“居中”,
fontFamily:“新安联”,
textDirection:'ltr',
标签:{
usePointStyle:true,
fontColor:#006192“,
}
}
}}
/>
{this.refs.chart&&this.refs.chart.chartInstance.GenerateGend()}