How to use the same scale on the y as the x but smaller because of the size difference?

北城余情 提交于 2019-12-25 06:19:11

问题


I have the following code snippet:

var data = [{
  "Part": 956,
  "Pos": "P2",
  "Side": "A",
  "Number": 1,
  "PIN/PAD": "A1",
  "xdim": 0.022,
  "ydim": 0.08,
  "centrex": 1.775685039,
  "centrey": 0.1945,
  "Rotated": "TRUE"
}];
var svgContainer = d3.selectAll('#svgContainer');

var margin = {
    top: 20,
    right: 20,
    bottom: 30,
    left: 40
  },
  width = 1240 - margin.left - margin.right,
  height = 740 / 1.5 - margin.top - margin.bottom;


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

var y = d3.scale.linear()
  .range([height, 0]);
//.range([width,0]);
//.range([0, width-200]);
var color = d3.scale.category10();

var xAxis = d3.svg.axis()
  .scale(x)
  .orient("bottom")
  .ticks(15);

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

var viewer = d3.select("#svg").append('g')
  //.attr("transform", "translate(" + ((width/2) - (width/4)) + "," + 35 + ")")
  .attr('id', 'viewerSVG');



var svg = viewer.append("svg").attr('id', 'viewerPins')
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

data.forEach(function(d) // data is the JSON
  {
    d.Length = +d.centrey;
    d.Width = +d.centrex;
    d.xdim = +d.xdim;
    d.ydim = +d.ydim;
  });

// x.domain(d3.extent(data, function(d) { return d.Width; })).nice();
//y.domain(d3.extent(data, function(d) { return d.Length; })).nice();

var maxW = d3.max(data, function(d) {
  return d.Width;
});
var maxL = d3.max(data, function(d) {
  return d.Length;
});

x.domain([0, maxW]).nice();
y.domain([0, maxW]).nice();
//y.domain([0, maxW]).nice();



// axis

var axisMovement = 0; //skipped this for simplicity

svg.append("g")
  .attr("class", "x axis")
  .attr('transform', 'translate(0,' + height + ')')
  // did transform w/o axisMovement thingie for simplicity
  .call(xAxis)
  .append("text")
  .attr("class", "label")
  .attr("x", width - 200)
  .attr("y", -6)
  .style("text-anchor", "end")
  .text("Width (cm)");

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis)
  .append("text")
  .attr("class", "label")
  .attr("transform", "rotate(-90)")
  .attr("y", 6)
  .attr("dy", ".71em")
  .style("text-anchor", "end")
  .text("Length (cm)")
interface css .canvas {
  fill: #FEFFFE;
}
#canvasChild.canvas {
  fill: white;
}
.visible {
  visibility: visible;
  opacity: 1;
}
.hidden {
  visibility: hidden;
  pointer-events: none;
}
.textNormal {
  text-anchor: middle;
  color: steelblue;
  position: relative;
  font: 14px Calibri;
}
body {
  font: 10px sans-serif;
}
.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}
.dot {
  stroke: #000;
}
.pad956,
.pad958 {
  opacity: 0.8;
  stroke: #000;
}
#pinDataText {
  text-anchor: middle;
  color: steelblue;
  position: relative;
  font: 14px Calibri;
}
.pad956Label,
.pad958Label {
  text-anchor: middle;
  color: steelblue;
  position: relative;
  font: 8px Calibri;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.0.4/d3.min.js"></script>
<body>
  <div id="title"></div>
  <div id="svg"></div>
</body>

And an alternative jsfiddle:

http://jsfiddle.net/laurieskelly/q0ubpmwj/

Both the x and the y use the same domain, but they're not to scale. This is because the height is different to the width, i know, but how do i edit the y axis to use the same proportions as the x axis but just on a smaller scale.

For example, i want the x to go from 0-1.8, but the y, to say around, 0 - 1.2. But i want the y scale to be the same as the x.

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

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

回答1:


Choose the right side and scale only to it.

var min_dimension = width < height ? width : height;    
var x = d3.scale.linear().range([0, min_dimension]);    
var y = d3.scale.linear().range([min_dimension, 0]);

Here, i've updated fiddle: http://jsfiddle.net/fen1kz/q0ubpmwj/1/

Edit: Ah, since you draw y-line from top, you should adjust only from height:

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



回答2:


Finally figured it out :

var yAxisDomain = maxW/(width/height);

y.domain([0, yAxisDomain]).nice(); 

I needed to get the difference in scale of width and height and then divide the axis width by that to get the correct proportions :)

Updated fiddle :

http://jsfiddle.net/q0ubpmwj/2/



来源:https://stackoverflow.com/questions/31202802/how-to-use-the-same-scale-on-the-y-as-the-x-but-smaller-because-of-the-size-diff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!