C3 使用分类 JSON 数据创建条形图
C3 creating a bar chart with categorical JSON data
如何为通过 JSON 加载数据的 C3 条形图指定 x 轴标签?我在文档中没有看到任何内容,我找到的最接近的是 this but it only gives an example for data specified in column format, whereas I have data specified in JSON format like in this example
我的尝试:
const chart = c3.generate({
data: {
// x: "title",
json: bookData,
type: "bar",
keys: {
value: ["count"]
}
},
axis: {
x: {
tick: {
values: labels,
rotate: 90,
},
type: "category",
}
},
bindto: "#book-title-histogram",
});
取消注释 x: "title"
会导致情节不再可见。注释掉该行后,轴就是空白。
编辑:bookData
是一个数组,每个元素都有键 count
和 title
看起来映射您的数据有效。
const bookData = [{
count: 3,
title: 'The Foutainhead'
},{
count: 4,
title: 'Fight Club'
},{
count: 2,
title: 'Ender\'s Game'
}]
const titles = bookData.map((obj) => {
return obj.title
})
const counts = bookData.map((obj) => {
return obj.count
})
console.log(titles)
const chart = c3.generate({
data: {
x: 'title',
y: 'count',
json: {
title: titles,
data: counts,
},
type: "bar",
},
axis: {
x: {
tick: {
count: bookData.length,
rotate: 45,
},
type: "category",
}
},
bindto: "#book-title-histogram",
});
如何为通过 JSON 加载数据的 C3 条形图指定 x 轴标签?我在文档中没有看到任何内容,我找到的最接近的是 this but it only gives an example for data specified in column format, whereas I have data specified in JSON format like in this example
我的尝试:
const chart = c3.generate({
data: {
// x: "title",
json: bookData,
type: "bar",
keys: {
value: ["count"]
}
},
axis: {
x: {
tick: {
values: labels,
rotate: 90,
},
type: "category",
}
},
bindto: "#book-title-histogram",
});
取消注释 x: "title"
会导致情节不再可见。注释掉该行后,轴就是空白。
编辑:bookData
是一个数组,每个元素都有键 count
和 title
看起来映射您的数据有效。
const bookData = [{
count: 3,
title: 'The Foutainhead'
},{
count: 4,
title: 'Fight Club'
},{
count: 2,
title: 'Ender\'s Game'
}]
const titles = bookData.map((obj) => {
return obj.title
})
const counts = bookData.map((obj) => {
return obj.count
})
console.log(titles)
const chart = c3.generate({
data: {
x: 'title',
y: 'count',
json: {
title: titles,
data: counts,
},
type: "bar",
},
axis: {
x: {
tick: {
count: bookData.length,
rotate: 45,
},
type: "category",
}
},
bindto: "#book-title-histogram",
});