diff --git a/nezha-fronted/src/components/chart/chart/chartPieD3.vue b/nezha-fronted/src/components/chart/chart/chartPieD3.vue index af590cdd1..e1facb4c6 100644 --- a/nezha-fronted/src/components/chart/chart/chartPieD3.vue +++ b/nezha-fronted/src/components/chart/chart/chartPieD3.vue @@ -158,7 +158,7 @@ export default { // 绘制饼图 drawArcPie (data) { this.$nextTick(() => { - let showAngle = 0 + // const showAngle = 0 d3.select(`#chart-canvas-${this.chartId}`).selectAll('svg').remove()// 清空作图区域 // 重新赋值宽高 try { @@ -210,7 +210,7 @@ export default { return arc(d) }) // 边界线 - // .attr('stroke', 'white') + .attr('stroke', 'white') // 颜色 .attr('fill', (d, i) => { if (d.data.mapping) { @@ -220,75 +220,126 @@ export default { } }) - // 文字 - arcs - .append('foreignObject') - .attr('y', d => { // y轴居中减文字大小的一半 - const y = arc.centroid(d)[1] - // 居中 - let height = 90 - if (Math.ceil(this.outerRadius / 8) < 30) { - height = Math.ceil(this.outerRadius / 8) * 3 - } - if ((d.endAngle - d.startAngle) > 5) { - height += this.outerRadius - } - return y - height / 2 - }) - .attr('x', d => { // y轴居中减文字大小的一半 - const x = arc.centroid(d)[0] - // 居中 - let width = 0 - if ((d.endAngle - d.startAngle) < 0.25) { - width = 0 - } else { - if ((d.endAngle - d.startAngle) > 5) { - width = this.outerRadius * 2 - } - width = this.outerRadius * (d.endAngle - d.startAngle) / 3.14 - width = width > this.outerRadius / 3 ? width : this.outerRadius / 3 - } - return x - width / 2 - }) - .style('font-size', (i) => { - if (Math.ceil(this.outerRadius / 8) > 30) { - return 30 - } else { - return Math.ceil(this.outerRadius / 8) - } - }) + /* 添加文字信息 */ + arcs.append('foreignObject') .attr('width', (d, i) => { if ((d.endAngle - d.startAngle) < 0.25) { return 0 + } else if ((d.endAngle - d.startAngle) > 6) { + return this.outerRadius + } else if ((d.endAngle - d.startAngle) > 3 && (d.endAngle - d.startAngle) < 6) { + return this.outerRadius - this.outerRadius / 5 } else { - if ((d.endAngle - d.startAngle) > 5) { - return this.outerRadius * 2 - } - let width = 0 - showAngle += d.endAngle - d.startAngle - if (showAngle > 0.75) { - width = this.outerRadius * (d.endAngle - d.startAngle) / 3.14 - width = width > this.outerRadius / 3 ? width : this.outerRadius / 3 - showAngle = 0 - } else { - showAngle += d.endAngle - d.startAngle - width = 0 - } - return width + return Math.abs(d.endAngle - d.startAngle) / 3.14 * this.outerRadius } }) .attr('height', (d, i) => { - if (Math.ceil(this.outerRadius / 8) > 30) { - return 90 + if ((d.endAngle - d.startAngle) < 0.25) { + return 0 + } else if ((d.endAngle - d.startAngle) > 1) { + return 1 / 3.14 * this.outerRadius } else { - return Math.ceil(this.outerRadius / 8) * 3 + return Math.abs(d.endAngle - d.startAngle) / 3.14 * this.outerRadius + } + }) + .attr('transform', (d, i) => { + if ((d.endAngle - d.startAngle) < 0.25) { + return '' + } else if ((d.endAngle - d.startAngle) > 6) { + return 'translate(' + (-this.outerRadius / 2) + ',' + (-this.outerRadius / 5) + ')' + } else if ((d.endAngle - d.startAngle) > 3 && (d.endAngle - d.startAngle) < 6) { + return 'translate(' + (arc.centroid(d)[0] - (this.outerRadius - this.outerRadius / 5) / 3) + ',' + (arc.centroid(d)[1] - (1 / 3.14 * this.outerRadius / 2)) + ')' + } else if ((d.endAngle - d.startAngle) > 1 && (d.endAngle - d.startAngle) < 3) { + return 'translate(' + (arc.centroid(d)[0] - (Math.abs(d.endAngle - d.startAngle) / 3.14 * this.outerRadius / 2)) + ',' + (arc.centroid(d)[1] - (1 / 3.14 * this.outerRadius / 2)) + ')' + } else { + return 'translate(' + (arc.centroid(d)[0] - (Math.abs(d.endAngle - d.startAngle) / 3.14 * this.outerRadius / 2)) + ',' + (arc.centroid(d)[1] - (Math.abs(d.endAngle - d.startAngle) / 3.14 * this.outerRadius / 2)) + ')' + } + }) + .style('font-size', (i) => { + if (Math.ceil(this.outerRadius / 18) > 30) { + return 30 + } else { + return Math.ceil(this.outerRadius / 18) } }) .style('line-height', '16px') .html((d) => { - return this.drawText(d) + if ((d.endAngle - d.startAngle) < 0.5 && Math.ceil(this.outerRadius / 18 < 30)) { + return `
- +