【发布时间】:2021-10-09 17:27:56
【问题描述】:
有人可以帮我解决图表颜色的问题吗?
我在 Angular 中使用 d3.js 来创建半饼图。 我想要 3 个部分,我应该有 3 种颜色。 我想要 3 种颜色,每种颜色都有一定的范围。
我的数据为 { a: 50, b: 50, c: 50},颜色为 ['green','yellow','red']。 为此,图表被平均分割,图表颜色的顺序正确(以绿色开始,以红色结束)。这是正确的附加图像。
但是当我提供 { a: 50, b: 30, c: 70} 的值时,我得到的颜色顺序不同。如下所示,它看起来首先呈现了最高值 (70)。
我的代码:
private theData = { a: 50, b: 30, c: 70};
private svg:any;
private svgWidth = 200;
private svgHeight = 125;
private colorRanges:any;
private pie:any;
this.svg = d3
.select('div#pie')
.append('svg')
.attr('width',this.svgWidth)
.attr('height',this.svgHeight)
.attr('id','pie-svg')
.append('g')
.attr('class','ps')
.attr(
'transform',
'translate('+this.svgWidth/2+',115)');
this.colorRanges = d3.scaleOrdinal().range(['green','yellow','red']);
this.pie = d3
.pie()
.startAngle(-90 * (Math.PI / 180))
.endAngle(90 * (Math.PI / 180))
.value((d: any) => d[1]);
this.svg.selectAll('rect')
.data(this.pie(Object.entries(this.theData)))
.join('path')
.attr('d',
d3.arc().innerRadius(75).outerRadius(35))
.attr('fill', (d:any) => this.colorRanges(d.data[0]))
.attr('stroke','none')
.style('stroke-width','1px')
.style('opacity',1);
【问题讨论】:
标签: javascript angular typescript d3.js angular12