Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

javascript - Data points and ticks in the scaleBand axis are not aligned

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.

The following is my code.

<!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 output is shown here:

image

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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)

So, it should be:

var yScale = d3.scalePoint()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...