从d3js图中删除某些刻度

时间:2015-11-25 20:07:35

标签: javascript d3.js

我有一个用d3js创建的绘图,请参阅下面的代码。我希望绘图与Matlab绘图更匹配,例如,参见enter image description here

在我的情节中,左上角,右上角和右下角有一些伸出的嘀嗒声。如何删除这些?

|           |
---------------  <-- these
|           |
|           |
|           |
---------------  <-- these

var margin  = {top: 20, right: 20, bottom: 50, left: 50},
    width   = 250 - margin.left - margin.right,
    height  = 250 - margin.top  - margin.bottom,
    padding = 50;

var x = d3.scale.linear()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(5)
	.innerTickSize(-6)
	.outerTickSize(-6)
	.tickPadding(7);

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(5)
	.innerTickSize(-6)
	.outerTickSize(-6)
	.tickPadding(7);

var svg = d3.select("body").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 + ")");    

var data = [];

for (var k = 0; k < 11; k++) {
    data.push({x: k, y: k});
}

var line = d3.svg.line()
    .x(function(d) { return x(d.x); })
    .y(function(d) { return y(d.y); })
    .interpolate("linear");

    // Define x domain
    x.domain([0, 10]);

    // Define y domain
    y.domain([0, 10]);

    // Add x axis
    svg.append("g")
        .attr("class","x axis")
        .attr("transform","translate(0," + height + ")")
        .call(xAxis);

    // Add y axis 
    svg.append("g")
        .attr("class","y axis")
        .call(yAxis);

    /* append additional X axis */
    svg.append("g")
    	.attr("class", "axis")
    	.attr("transform", "translate(" + [0, 0] + ")")
    	.call(xAxis.innerTickSize(6).tickPadding(-20).tickFormat(""));

	/* append additional y axis */
    svg.append("g")
        .attr("class","y axis")
        .attr("transform", "translate(" + [width, 0] + ")")
        .call(yAxis.innerTickSize(6).tickPadding(-20).tickFormat(""));

    // Add x grid
    svg.append("g")         
        .attr("class","grid")
        .attr("transform","translate(0," + height + ")")
        .call(xAxis
            .tickSize(-height,-height,0)
            .tickFormat("")
        );

    // Add y grid
    svg.append("g")         
        .attr("class","grid")
        .call(yAxis
            .tickSize(-width,-width,0)
            .tickFormat("")
        );

    // Add x axis label  
    svg.append("text")
        .attr("transform", "translate(" + (width / 2) + "," + (height + margin.bottom) + ")")
        .style("font-size","15")
        .style("text-anchor", "middle")
        .text("x axis");

    // Add y axis label
    svg.append("text")
        .attr("transform", "rotate(-90)")
        .attr("y",0 - margin.left)
        .attr("x",0 - (height / 2))
        .attr("dy", "1em")
        .style("font-size","15")
        .style("text-anchor", "middle")
        .text("y axis");

    svg.append("path")
        .attr("class", "line")
        .attr("d",line(data));
body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
    fill: none;
    stroke: #000;
    shape-rendering: crispEdges;
}

.line {
    fill: none;
    stroke: steelblue;
    stroke-width: 1.5px;
}

.grid .tick {
    stroke: lightgrey;
    opacity: 0.7;
}

.grid path {
    stroke-width: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

0 个答案:

没有答案