Javascript 如何使用dc.js在饼图中心显示值
我使用dc.js创建了一个饼图,如下所示 这里我想在圆形空白区域中显示饼图中间的一些值。我没有发现任何与此相关的例子 我正在下面添加我的代码。多谢各位Javascript 如何使用dc.js在饼图中心显示值,javascript,dc.js,crossfilter,Javascript,Dc.js,Crossfilter,我使用dc.js创建了一个饼图,如下所示 这里我想在圆形空白区域中显示饼图中间的一些值。我没有发现任何与此相关的例子 我正在下面添加我的代码。多谢各位 let data = [ { productCount: 1, customer : 200 }, { productCount: 2, customer : 100 }, { productCount: 3,
let data = [
{
productCount: 1,
customer : 200
},
{
productCount: 2,
customer : 100
},
{
productCount: 3,
customer : 300
},
{
productCount: 4,
customer : 400
},
{
productCount: 5,
customer : 560
},
{
productCount: 6,
customer : 2100
},
]
在第二行的上述代码中,变量名totalCustomer包含需要放置在圆形空白饼图圆圈之间的客户总数。使用d3(您当前使用的)将文本元素附加到svg饼图。只需选择标记,然后将元素附加到它。您也可以使用
您可能希望将此代码添加到
预转换
事件处理程序中,以便在每次重新绘制饼图时执行它。
let totalCustomerArray = data.map(data => data.customer);
let totalCustomer = totalCustomerArray.reduce((a, b) => a + b);
let ndx = crossfilter(data);
let all = ndx.groupAll();
let productCountDimension = ndx.dimension(d => d.productCount);
let groupCustomer = productCountDimension.group().reduceSum(d => d.customer);
const pie = dc.pieChart('.pie-chart');
pie
.width(400)
.height(400)
.radius(140)
.innerRadius(90)
.dimension(productCountDimension)
.group(groupCustomer)
.on('pretransition', (chart) => {
pie.selectAll('text.pie-slice').text(d => (
dc.utils.printSingleValue((d.endAngle - d.startAngle) / (2 * Math.PI) * 100) + '%'
))
})
pie.render();
var svg = d3.select("svg"); // select the svg element where you are putting your pie chart.
svg.append("text") // appent the "text" element
.attr("x", 220) // x position
.attr("y", 220) // y position
.text("1234") // the text content