scaleBand轴上的数据点和刻度线未对齐 [英] Data points and ticks in the scaleBand axis are not aligned

查看:90
本文介绍了scaleBand轴上的数据点和刻度线未对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据点和scaleBand y轴上的值未对齐.我无法正确对齐它们,当我阅读文档时,看到默认情况下对齐为0.5,这就是为什么我的数据点在轴上的两个点之间绘制的原因.但是我尝试覆盖对齐方式,将对齐方式设置为0,但似乎没有变化.

My data points and the values in the scaleBand y axis are not aligned. I am not able to align them properly, when I read the documentation, saw that by default the alignment is 0.5 and that's why my data points are plotted between the two points in the axis. But I tried to override the alignment my giving the alignment as 0, but there seems to be no change.

以下是我的代码.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>D3 v4 - linechart</title>
<style>
#graph {
    width: 900px;
    height: 500px;
}
.tick line {
    stroke-dasharray: 2 2 ;
    stroke: #ccc;
}


.y path{
    fill: none;
  stroke: none;
}

</style>
</head>

<body>
<div id="graph"></div>    



<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.1.1/d3.min.js"></script>    
<script>
!(function(){
    "use strict"

    var width,height
    var chartWidth, chartHeight
    var margin
    var svg = d3.select("#graph").append("svg")
    var axisLayer = svg.append("g").classed("axisLayer", true)
    var chartLayer = svg.append("g").classed("chartLayer", true)

    var xScale = d3.scaleLinear()
    var yScale = d3.scaleBand()
    var align = 0


    //d3.tsv("data1.tsv", cast,  main)
    d3.json("http://localhost/d32.json",cast)

    //データの方変換
    function cast(data) {
        console.log("got it");
        data.forEach(function(data) {
        console.log(data.Letter);
        data.Letter = data.Letter;
        data.Freq = +data.Freq;
    });
        main(data);
    }

    function main(data) {
        console.log("in main");
        setSize(data)
        drawAxis()
        drawChart(data)    
    }

    function setSize(data) {
        width = document.querySelector("#graph").clientWidth
        height = document.querySelector("#graph").clientHeight

        margin = {top:40, left:100, bottom:40, right:0 }


        chartWidth = width - (margin.left+margin.right+8)
        chartHeight = height - (margin.top+margin.bottom)

        svg.attr("width", width).attr("height", height)

        axisLayer.attr("width", width).attr("height", height)

        chartLayer
            .attr("width", chartWidth)
            .attr("height", chartHeight)
            .attr("transform", "translate("+[margin.left, margin.top]+")")


        xScale.domain([0, d3.max(data, function(d) { return d.Freq; })]).range([0,chartWidth])
        yScale.domain(data.map(function(d) { return d.Letter; })).range([chartHeight, 0]).align(1)

    }

    function drawChart(data) {

    console.log("in drawChart");
        var t = d3.transition()
            .duration(8000)
            .ease(d3.easeLinear)
            .on("start", function(d){ console.log("transiton start") })
            .on("end", function(d){ console.log("transiton end") })


        var lineGen = d3.line()
            .x(function(d) { return xScale(d.Freq) })
            .y(function(d) { return yScale(d.Letter)  })
            .curve(d3.curveStepAfter)

        var line = chartLayer.selectAll(".line")
            .data([data])

        line.enter().append("path").classed("line", true)
            .merge(line)
            .attr("d", lineGen)
            .attr("fill", "none")
            .attr("stroke", "blue")
            .attr("stroke-width","2px")
            .attr("stroke-dasharray", function(d){ return this.getTotalLength() })
            .attr("stroke-dashoffset", function(d){ return this.getTotalLength() })

        chartLayer.selectAll(".line").transition(t)
            .attr("stroke-dashoffset", 0)



    chartLayer.selectAll("circle").classed("circle",true)
    .data(data)
    .enter().append("circle")
    .attr("class", "circle")
    .attr("fill","none")
    .attr("stroke","black")
    .attr("cx", function(d) { return xScale(d.Freq); })
    .attr("cy", function(d) { return yScale(d.Letter); })
    .attr("r", 4);

    chartLayer.selectAll(".logo").transition(t)
        .attr("stroke-dashoffset", 0)


    }

    function drawAxis(){
        var yAxis = d3.axisLeft(yScale)
            .tickSizeInner(-chartWidth)

        axisLayer.append("g")
            .attr("transform", "translate("+[margin.left, margin.top]+")")
            .attr("class", "axis y")
            .call(yAxis);

        var xAxis = d3.axisBottom(xScale)

        axisLayer.append("g")
            .attr("class", "axis x")
            .attr("transform", "translate("+[margin.left, chartHeight+margin.top]+")")
            .call(xAxis);

    }
}());
</script>    
</body>
</html>

输出显示在这里:

推荐答案

在这种情况下,带刻度是错误的工具.主要原因是频段规模具有关联的带宽.

The band scale is the wrong tool in this situation. The main reason is that a band scale has an associated bandwidth.

您可以调整带刻度的paddingInnerpaddingOuter值,以得到预期的结果.但是,最简单的解决方案是使用点刻度. 磅秤:

You can tweak the paddingInner and paddingOuter values of the band scale to give you the expected result. However, the easiest solution is using a point scale instead. Point scales:

...是频带比例的一种变体,其带宽固定为零. (强调我的)

...are a variant of band scales with the bandwidth fixed to zero. (emphasis mine)

因此,应该是:

var yScale = d3.scalePoint()

这篇关于scaleBand轴上的数据点和刻度线未对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆