javascript - 将标签数组放入chartjs?
问题描述
我有这些代码: let arr = [];
items &&
items.map((index) => {
arr.push(index.itemName);
});
这console.log(arr);
表明:
[“项目 1”、“项目 2”、“项目 3”、“项目 4”]
下面是我的图表:如何循环遍历arr
要放在标签内的对象?
<Pie
data={{
labels: , <-- How can I loop through all of the `arr` here?
datasets: [
{
label: "1st Dose",
data: [10, 20, 30, 50, 30],
backgroundColor: ["red", "orange", "yellow", "Green", "Blue"],
borderColor: ["rgba(255, 99, 132, 1)"],
borderWidth: 1,
},
],
}}
height={400}
width={600}
options={{
maintainAspectRatio: false,
title: {
display: true,
text: "Hello",
fontSize: 20,
},
legend: {
labels: {
fontSize: 25,
},
},
}}
/>
解决方案
图表标签接收一个字符串或字符串数组:
interface ChartData {
labels?: Array<string | string[]>;
datasets?: ChartDataSets[];
}
因此,您将数组直接放在标签上,如下所示:
data={{
labels: arr,
datasets: [
{
label: "1st Dose",
data: [10, 20, 30, 50, 30],
backgroundColor: ["red", "orange", "yellow", "Green", "Blue"],
borderColor: ["rgba(255, 99, 132, 1)"],
borderWidth: 1,
},
],
}}
一个活生生的例子供你学习:
推荐阅读
- html - 我的变量在我的 JS 函数 JSX 中没有递增
- javascript - 当我想要 dockerize 我的 nextjs 项目时出现问题
- javascript - VueJS路由器中的`path`和`fullPath`有什么区别?
- glibc - LFS 8.3 Glibc-2.28 使检查失败'stdlib/test-bz22786'
- oracle - 创建 Oracle 表时出错,无效标识符
- pydicom - Orthanc匿名dcm文件,有没有办法直接转换dcm文件而不先生成匿名文件
- ios - WKWebView 没有将内容滚动到映射的 HTML
- c++ - FastCGI via C : FCGX_Accept_r 偶尔执行“连接重置...”
- vba - VBA知道段落是否是图像
- android - Glide 和 Picasso 无法加载资源,API 19 的 javax.net.ssl.SSLHandshakeException