gpt4 book ai didi

javascript - D3.js 绕中点旋转轴标签

转载 作者:行者123 更新时间:2023-11-29 19:06:16 24 4
gpt4 key购买 nike

我现在正在使用 D3.js,尽管我发现了非常相似的情况,但我实际上无法将它们放在一起并继续前进。

我有类似雷达图的东西,我想附加到我创建的每个轴(轴的数量不固定可以是 4,但也可以是 40)文本,我已经有了,但围绕中心旋转文本当它们达到 180 度(实际上是 0 度)时,立即指向并旋转它们。

结果应如下所示:

enter image description here

我现在拥有的是:

enter image description here

我只知道如何在arc内达到这个目的,这也很好地显示了 here ,但我并没有真正弄清楚我的情况。

这是我的代码片段,我在其中附加了这些文本条件:

//Write criterias
axis.append("text")
.attr("class","labels")
.attr("font-size","12px")
.attr("font-family","Montserrat")
.attr("text-anchor","middle")
.attr("fill","white")
.attr("x",function (d, i) {
return radius * Math.cos(angleSlice * i - Math.PI/2)*options.circles.labelFactor;
})
.attr("y",function (d, i) {
return radius * Math.sin(angleSlice * i - Math.PI/2)*options.circles.labelFactor;
})
.text(function (d) {
return d;
});

编辑:

这是我的 fiddle :https://jsfiddle.net/fsb47ndf/

感谢您的建议

最佳答案

有些人发现旋转 SVG 元素很困难,因为 transform 属性的 rotate 函数使元素围绕原点 (0,0) 旋转,而不是围绕原点 (0,0) 旋转它的中心:

If optional parameters and are not supplied, the rotate is about the origin of the current user coordinate system (source)

因此,一个简单的选择是删除文本的 xy 属性,并使用 transform 定位它们。这样,我们就可以轻松计算旋转:

.attr("transform", function(d, i) {
var rotate = angleSlice * i > Math.PI / 2 ?
(angleSlice * i * 180 / Math.PI) - 270 :
(angleSlice * i * 180 / Math.PI) - 90;
return "translate(" + radius * Math.cos(angleSlice * i - Math.PI / 2) * options.circles.labelFactor +
"," + radius * Math.sin(angleSlice * i - Math.PI / 2) * options.circles.labelFactor +
") rotate(" + rotate + ")"
})

这是您的代码:

data = [{
name: 'DATA1',
value: 22,
}, {
name: 'DATA2',
value: 50,
}, {
name: 'DATA3',
value: 0,
}, {
name: 'DATA4',
value: 24,
}, {
name: 'DATA5',
value: 22,
}, {
name: 'DATA6',
value: 30,
}, {
name: 'DATA7',
value: 20,
}, {
name: 'DATA8',
value: 41,
}, {
name: 'DATA9',
value: 31,
}, {
name: 'DATA10',
value: 30,
}, {
name: 'DATA11',
value: 30,
}, {
name: 'DATA12',
value: 30,
}, {
name: 'DATA13',
value: 30,
}, {
name: 'DATA14',
value: 30,
}, ];



var options = {

width: 600,
height: 600,

margins: {
top: 100,
right: 100,
bottom: 100,
left: 100
},

circles: {
levels: 6,
maxValue: 100,
labelFactor: 1.15,
opacity: 0.2,
},

};


var allAxis = (data.map(function(i, j) {
return i.name
})),
total = allAxis.length,
radius = Math.min(options.width / 2, options.height / 2),
angleSlice = Math.PI * 2 / total,
Format = d3.format('');

var rScale = d3.scale.linear()
.domain([0, options.circles.maxValue])
.range([50, radius]);

var svg = d3.select("body").append("svg")
.attr("width", options.width + options.margins.left + options.margins.right)
.attr("height", options.height + options.margins.top + options.margins.bottom);

var g = svg.append("g")
.attr("transform", "translate(" + (options.width / 2 + options.margins.left) + "," + (options.height / 2 + options.margins.top) + ")");

var axisGrid = g.append("g")
.attr("class", "axisWraper");

var axis = axisGrid.selectAll(".axis")
.data(allAxis)
.enter()
.append("g")
.attr("class", "axis")

//append them lines
axis.append("line")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", function(d, i) {
var tempX2 = radius * Math.cos(angleSlice * i - Math.PI / 2);
return tempX2;
})
.attr("y2", function(d, i) {
var tempY = radius * Math.sin(angleSlice * i - Math.PI / 2);
return tempY;
})
.attr("class", "line")
.attr("stroke", "black")
.attr("fill", "none");

//Draw background circles
axisGrid.selectAll(".levels")
.data([6, 5, 4, 3, 2, 1])
.enter()
.append("circle")
.attr("class", "gridCircle")
.attr("r", function(d, i) {
return parseInt(radius / options.circles.levels * d, 10);
})
.attr("stroke", "black")
.attr("fill-opacity", options.circles.opacity);

//Write data
axis.append("text")
.attr("class", "labels")
.attr("font-size", "12px")
.attr("font-family", "Montserrat")
.attr("text-anchor", "middle")
.attr("fill", "black")
.attr("transform", function(d, i) {
var rotate = angleSlice * i > Math.PI ? (angleSlice * i * 180 / Math.PI) - 270 : (angleSlice * i * 180 / Math.PI) - 90;
return "translate(" + radius * Math.cos(angleSlice * i - Math.PI / 2) * options.circles.labelFactor + "," + radius * Math.sin(angleSlice * i - Math.PI / 2) * options.circles.labelFactor + ") rotate(" + rotate + ")"
})
.text(function(d) {
return d;
});
<script src="https://d3js.org/d3.v3.min.js"></script>

关于javascript - D3.js 绕中点旋转轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42581308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com