如何在 y 上使用与 x 相同的比例但由于尺寸差异而更小?
How to use the same scale on the y as the x but smaller because of the size difference?
我有以下代码片段:
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>
还有一个替代的 jsfiddle:
http://jsfiddle.net/laurieskelly/q0ubpmwj/
x 和 y 使用相同的域,但它们不按比例缩放。这是因为高度与宽度不同,我知道,但是我如何编辑 y 轴以使用与 x 轴相同的比例,但比例较小。
例如,我希望 x 为 0-1.8,而 y 为 0-1.2。但我希望 y 比例与 x 相同。
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
选择右侧并仅对其进行缩放。
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]);
在这里,我更新了fiddle:
http://jsfiddle.net/fen1kz/q0ubpmwj/1/
编辑:啊,因为你从上面画y线,你应该只从高度调整:
var x = d3.scale.linear().range([0, height]);
var y = d3.scale.linear().range([height, 0]);
终于想通了:
var yAxisDomain = maxW/(width/height);
y.domain([0, yAxisDomain]).nice();
我需要得到宽度和高度的比例差异,然后将轴宽度除以得到正确的比例:)
已更新 fiddle:
我有以下代码片段:
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>
还有一个替代的 jsfiddle:
http://jsfiddle.net/laurieskelly/q0ubpmwj/
x 和 y 使用相同的域,但它们不按比例缩放。这是因为高度与宽度不同,我知道,但是我如何编辑 y 轴以使用与 x 轴相同的比例,但比例较小。
例如,我希望 x 为 0-1.8,而 y 为 0-1.2。但我希望 y 比例与 x 相同。
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
选择右侧并仅对其进行缩放。
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]);
在这里,我更新了fiddle: http://jsfiddle.net/fen1kz/q0ubpmwj/1/
编辑:啊,因为你从上面画y线,你应该只从高度调整:
var x = d3.scale.linear().range([0, height]);
var y = d3.scale.linear().range([height, 0]);
终于想通了:
var yAxisDomain = maxW/(width/height);
y.domain([0, yAxisDomain]).nice();
我需要得到宽度和高度的比例差异,然后将轴宽度除以得到正确的比例:)
已更新 fiddle: