2017-07-31 21 views
0

d3.jsを使用してグラフを作成しています。私はグラフにいくつかのグリッド線を追加しました。結果はhereです。だから私は軸を識別するためにグリッドを右に動かす必要がありますy私はx軸の値を固定したい(13000、13100、13200、13300、13400 、ここでは13500、13600、13700、13800、13900、14000)は、全体のコードです:d3.jsを使用してグラフを右側に移動するにはどうすればよいですか?

<!DOCTYPE html> 


<style> /* set the CSS */ 


.grid line { 
    stroke: aquamarine; 
    stroke-opacity: 0.7; 
    shape-rendering: crispEdges; 
} 

.grid path { 
    stroke-width: 1; 
} 

</style> 
<style> 
body { 
    background-color: #262f39; 
} 
</style> 
<svg id="svg1" width="1000" height="500"></svg> 
<script src="https://d3js.org/d3.v4.min.js"></script> 
<script> 

// set the dimensions and margins of the graph 
var svg1 = d3.select("#svg1"), 
    margin = {top: 1, right: 0, bottom: 90, left: 50}, 
    width = 950 - margin.left - margin.right, 
    height = 500 - margin.top - margin.bottom, 
    g1 = svg1.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 


var x = d3.scaleLinear() 
.rangeRound([0, width]); 

var y = d3.scaleLinear() 
.rangeRound([height, 0]); 

// gridlines in x axis function 
function make_x_gridlines() { 
    return d3.axisBottom(x) 
     .ticks(5) 
} 

// gridlines in y axis function 
function make_y_gridlines() { 
    return d3.axisLeft(y) 
     .ticks(5) 
} 

var line = d3.line() 
.x(function(d) { return x(d.frequency); }) 
.y(function(d) { return y(d.output); }); 




d3.csv("Measurements.csv", function(d) { 
    d.frequency = +d.frequency; 
    d.output = +d.output; 
    return d; 
}, function(error, data) { 
    if (error) throw error; 

x.domain(d3.extent(data, function(d) { return d.frequency; })); 
y.domain(d3.extent(data, function(d) { return d.output; })); 

    g1.append("g") 
     .attr("transform", "translate(0," + height + ")") 
     .call(d3.axisBottom(x)) 
     .append("text") 
     .attr("fill", "#000") 
     .attr("y", 11) 
     .attr("dx", "0.71em") 
     .attr("text-anchor", "end") 
     .text("Frequency"); 


    g1.append("g") 
     .call(d3.axisLeft(y)) 
    .append("text") 
     .attr("fill", "#000") 
     .attr("transform", "rotate(-90)") 
     .attr("y", 15) 
     .attr("dy", "0.71em") 
     .attr("text-anchor", "end") 
     .text("Mixer Output"); 

    g1.append("path") 
     .datum(data) 
     .attr("fill", "none") 
     .attr("stroke", "aquamarine") 
     .attr("stroke-linejoin", "round") 
     .attr("stroke-linecap", "round") 
     .attr("stroke-width", 4) 
     .attr("d", line); 

     // add the X gridlines 
    svg1.append("g") 
     .attr("class", "grid") 
     .attr("transform", "translate(0," + height + ")") 
     .call(make_x_gridlines() 
      .tickSize(-height) 
      .tickFormat("") 
    ) 

    // add the Y gridlines 
svg1.append("g") 
    .attr("class", "grid") 
    .call(make_y_gridlines() 
     .tickSize(-width) 
     .tickFormat("") 
    ) 


}); 

</script> 
<svg id="svg2" width="1000" height="500"></svg> 
<script> 

// set the dimensions and margins of the graph 
var svg2 = d3.select("#svg2"), 
    margin = {top: 1, right: 0, bottom: 90, left: 50}, 
    width = 950 - margin.left - margin.right, 
    height = 500 - margin.top - margin.bottom, 
    g2 = svg2.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 


var x2 = d3.scaleLinear() 
    .rangeRound([0, width]); 

var y2 = d3.scaleLinear() 
    .rangeRound([height, 0]); 

    // gridlines in x axis function 
    function make_x_gridlines() { 
     return d3.axisBottom(x2) 
      .ticks(10) 
    } 

    // gridlines in y axis function 
function make_y_gridlines() { 
    return d3.axisLeft(y2) 
     .ticks(5) 
} 

var line = d3.line() 
    .x(function(d) { return x(d.frequency); }) 
    .y(function(d) { return y(d.output); }); 




d3.csv("m11.csv", function(d) { 
    d.frequency = +d.frequency; 
    d.output = +d.output; 
    return d; 
}, function(error, data) { 
    if (error) throw error; 

x.domain(d3.extent(data, function(d) { return d.frequency; })); 
y.domain(d3.extent(data, function(d) { return d.output; })); 

    g2.append("g") 
    .attr("transform", "translate(0," + height + ")") 
    .call(d3.axisBottom(x)) 
    .append("text") 
    .attr("fill", "#000") 
    .attr("y", 11) 
    .attr("dx", "0.71em") 
    .attr("text-anchor", "end") 
    .text("Frequency"); 


g2.append("g") 
    .call(d3.axisLeft(y)) 
    .append("text") 
    .attr("fill", "#000") 
    .attr("transform", "rotate(-90)") 
    .attr("y", 9) 
    .attr("dy", "0.71em") 
    .attr("text-anchor", "end") 
    .text("Delay"); 

g2.append("path") 
    .datum(data) 
    .attr("fill", "none") 
    .attr("stroke", "aquamarine") 
    .attr("stroke-linejoin", "round") 
    .attr("stroke-linecap", "round") 
    .attr("stroke-width", 4) 
    .attr("d", line); 

    // add the X gridlines 
svg2.append("g") 
    .attr("class", "grid") 
    .attr("transform", "translate(0," + height + ")") 
    .call(make_x_gridlines() 
     .tickSize(-height) 
     .tickFormat("") 
    ) 

    // add the Y gridlines 
svg2.append("g") 
    .attr("class", "grid") 
    .call(make_y_gridlines() 
     .tickSize(-width) 
     .tickFormat("") 
    ) 


}); 

</script> 
+0

コード全体を追加することはできますか? –

+0

[これらの](https://meta.stackoverflow.com/a/291370/1783163)に従うと、簡単なルールがあなたの質問を大きく改善する可能性があります。私はそれらに従うことを提案する。私は今あなたの質問を部分的に修正しましたが、いつもあなたと一緒にいることはできません:-) – peterh

答えて

0

あなたのグラフを変換するために、いくつかのマージンを追加するためにAGの要素を使用していますが、直接グリッドを付加しているようですsvg。

代わりにグリッドをg要素に追加してみてください。

関連する問題