D3如何在0以外的其他位置启动y轴

时间:2016-11-14 16:35:13

标签: javascript css d3.js axis-labels

我有像这样的双轴图

enter image description here

2 Y轴没有最佳缩放比例 - 右手Y我想从1.5开始并以.1的4个滴答结束 -Left hand Y我想从50开始并在1

结束60个刻度

如何做到这一点是我的代码和数据:

<!DOCTYPE html>
<meta charset="utf-8">

<head>
<title>Self Identification Percentages</title>
<style>
.dataDisplay {
  font-size:1em;
}


body { font: 12px Arial;}

path { 
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}



</style>

 <script src="./jquery.min.js" type="text/javascript"></script>
 <script src="./jquery-ui.min.js" type="text/javascript"></script>
 <script src="./jquery.corner.js"></script>
</head>

<body>

<script src="./d3/d3.v3.min.js"></script>








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

<script>


// Set the dimensions of the canvas / graph
var margin = {top: 0, right: 20, bottom: 20, left: 50},
    width = 350 - margin.left - margin.right,
    height = 350 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%Y-%m-%d").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 1]);
var yr = d3.scale.linear().range([height, 1]);


// Define the axes
var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y).orient("left").ticks(10);
var yAxisRight = d3.svg.axis().scale(y).orient("right").ticks(10); 

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.pctgDiff); });

// Adds the svg canvas
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 + ")");

// Get the data


 var dataFile = "abo.csv";
 d3.csv(dataFile, function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.pctgDiff = d.pctgDiff;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, d3.max(data, function(d) { return d.pctgDiff; })]);

    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data))
        .attr("stroke", "#7CA2C8");


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


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


  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
      .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", -40)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
          .style("font-size", "12px")
          .style("font-weight", "900")
          .style("font", "sans-serif")
      .text("All Other Change");


 });





 dataFile = "dis.csv";
 d3.csv(dataFile, function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.pctgDiff = d.pctgDiff;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, d3.max(data, function(d) { return d.pctgDiff; })]);

    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data))
        .attr("stroke", "#ECAD6F");
 });



 dataFile = "min.csv";
 d3.csv(dataFile, function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.pctgDiff = d.pctgDiff;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, d3.max(data, function(d) { return d.pctgDiff; })]);

    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data))
        .attr("stroke", "#C3D1DC");
 });



 dataFile = "fem.csv";
 d3.csv(dataFile, function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.pctgDiff = d.pctgDiff;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, d3.max(data, function(d) { return d.pctgDiff; })]);

    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data))
        .attr("stroke", "#7CC8A2");

   svg.append("g")             
        .attr("class", "y axis")    
        .attr("transform", "translate(" + width + " ,0)")   
        .style("fill", "#7CC8A2")       
        .call(yAxisRight);
  svg.append("g")
      .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 310)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
          .style("font-size", "12px")
          .style("font-weight", "900")
          .style("font", "sans-serif")
      .style("fill", "#7CC8A2") 
      .text("Female Change");
 });

</script>



</body>
</html>

我试过摆弄

var y = d3.scale.linear()。range([height,1]); var yr = d3.scale.linear()。range([height,1]);

例如将0更改为1但它仍然从双方的原点开始

这里也是我的数据:

abo.csv:

date,pctgDiff
2014-03-31,1.850000
2015-03-31,2.400000
2016-03-31,2.700000
2016-11-10,3.280000

dis.csv:

date,pctgDiff
2014-03-31,1.020000
2015-03-31,1.040000
2016-03-31,1.170000
2016-11-10,1.320000

min.csv

date,pctgDiff
2014-03-31,5.010000
2015-03-31,6.100000
2016-03-31,6.370000

2016-11-10,6.070000

fem.csv

date,pctgDiff
2014-03-31,53.540000
2015-03-31,53.940000
2016-03-31,54.510000
2016-11-10,54.830000

2 个答案:

答案 0 :(得分:2)

Y轴构建方法使用开始值和结束值的数组。在你的情况下,domain方法得到0作为起点。

  

y.domain([0,d3.max(data,function(d){return d.pctgDiff;})]);

您可以使用自定义值代替0或仅使用  d3.extent(array[, accessor])帮助方法获取当前数据的[minimum, maximum]数组。

  

d3.extent(array [,accessor])

     

使用natural返回给定数组中的最小值和最大值   订购。如果数组为空,则返回[undefined,undefined]。一个   可以指定可选的访问器功能,相当于   在计算范围之前调用array.map(accessor)。

示例:

y.domain(d3.extent(data,function(d){return d.pctgDiff;}));

答案 1 :(得分:0)

你错误的范围和域名。范围对应于像素范围,范围与您的规模应该处理的范围有关。