首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分组条形图d3js中的文本标签错误

分组条形图d3js中的文本标签错误
EN

Stack Overflow用户
提问于 2021-04-22 09:04:25
回答 1查看 17关注 0票数 0

我是d3js的新手,我不知道为什么酒吧里所有的标签都错了。

我的代码和捕获如下所示,然后您可以看到所有标签都与我的数据不同。

有人知道我的文字标签区发生了什么吗?

//设置图形的尺寸和边距

代码语言:javascript
复制
var margin = { top: 10, right: 30, bottom: 40, left: 50 },
    width = 700 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

const dataUrl = "https://raw.githubusercontent.com/yushinglui/IV/main/time_distance_status_v2.csv"

//fetch the data
d3.csv(dataUrl)
    .then((data) => {

        // append the svg object to the body of the page
        var svg = d3.select("#graph-2")
            .append("svg")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
            .append("g")
            .attr("transform",
                "translate(" + margin.left + "," + margin.top + ")")



        // List of subgroups = header of the csv files = soil condition here
        var subgroups = data.columns.slice(1)

        // List of groups = species here = value of the first column called group -> I show them on the X axis
        var groups = d3.map(data, function (d) { return (d.startTime) })

        // Add X axis
        var x = d3.scaleBand()
            .domain(groups)
            .range([0, width])
            .padding([0.2])
        svg.append("g")
            .attr("transform", "translate(0," + height + ")")
            .call(d3.axisBottom(x).tickSize(0));

        // Add Y axis
        var y = d3.scaleLinear()
            .domain([0, 20])
            .range([height, 0]);
        svg.append("g")
            .call(d3.axisLeft(y));

        // Another scale for subgroup position?
        var xSubgroup = d3.scaleBand()
            .domain(subgroups)
            .range([0, x.bandwidth()])
            .padding([0.05])

        // color palette = one color per subgroup
        var color = d3.scaleOrdinal()
            .domain(subgroups)
            .range(['#98abc5', '#8a89a6'])



        // Show the bars
        svg.append("g")
            .selectAll("g")

            // Enter in data = loop group per group
            .data(data)
            .enter()
            .append("g")
            .attr("transform", function (d) { return "translate(" + x(d.startTime) + ",0)"; })

            .selectAll("rect")
            .data(function (d) { return subgroups.map(function (key) { return { key: key, value: d[key] }; }); })
            .enter()
            .append("rect")
            .attr("x", function (d) { return xSubgroup(d.key); })
            .attr("y", function (d) { return y(d.value); })
            .attr("width", xSubgroup.bandwidth())
            .attr("height", function (d) { return height - y(d.value); })
            .attr("fill", function (d) { return color(d.key); })

            // mouseover and mouseout animation
            .on("mouseover", function (d) {
                d3.select(this).style("fill", d3.rgb(color(d.key)).darker(2))
            })
            .on("mouseout", function (d) {
                d3.select(this).style("fill", function (d) { return color(d.key); })
            })





        //axis labels
        svg.append('text')
            .attr('x', - (height / 2))
            .attr('y', width - 650)
            .attr('transform', 'rotate(-90)')
            .attr('text-anchor', 'middle')
            .style("font-size", "17px")
            .text('Average Distance');

        svg.append('text')
            .attr('x', 300)
            .attr('y', width - 240)
            .attr('transform', 'rotate()')
            .attr('text-anchor', 'middle')
            .style("font-size", "17px")
            .text('Start Time');

        // legend
        svg.append("circle").attr("cx", 200).attr("cy", 20).attr("r", 6).style("fill", "#98abc5")
        svg.append("circle").attr("cx", 300).attr("cy", 20).attr("r", 6).style("fill", "#8a89a6")
        svg.append("text").attr("x", 220).attr("y", 20).text("Present").style("font-size", "15px").attr("alignment-baseline", "middle")
        svg.append("text").attr("x", 320).attr("y", 20).text("Absent").style("font-size", "15px").attr("alignment-baseline", "middle")


        //text labels on bars -- all labels wrong!!

        svg.append("g")
            .selectAll("g")

            // Enter in data = loop group per group
            .data(data)
            .enter()
            .append("g")
            .attr("transform", function (d) { return "translate(" + x(d.startTime) + ",0)"; })

            .selectAll("text")
            .data(function (d) { return subgroups.map(function (key) { return { key: key, value: d[key] }; }); })
            .enter()
            .append("text")
            .text(function (d) { return y(d.value); })

            .attr("font-family", "sans-serif")
            .attr("font-size", "12px")
            .attr("fill", "black")
            .attr("text-anchor", "middle")

            .attr("x", function (d) { return xSubgroup(d.key); })
            .attr("y", function (d) { return y(d.value) + 10; })

    });

我的参考网站:

http://plnkr.co/edit/9lAiAXwet1bCOYL58lWN?p=preview&preview

https://bl.ocks.org/bricedev/0d95074b6d83a77dc3ad

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-22 11:13:52

您的问题是,当您追加文本时,您无意中调用了y函数,该函数用于获取插入文本的y位置。你得到的数字实际上是y-location值,看起来完全是随机的。

代码语言:javascript
复制
.text(function (d) { return y(d.value); }) // here is the issue

将其更改为

代码语言:javascript
复制
.text(function (d) { return d.value; })

它应该是有效的!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67205400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档