|
|
1
Gerardo Furtado
8 年前
这是我的建议,基于朱在他的书中的方法
基本上,我们用一个对象创建一个函数。。。
function marcon() {
var instance = {};
}
…并分别设置每个方法:
instance.top = function(d) {
if (!arguments.length) return top;
top = d;
return instance;
};
最后,使用调用渲染部分
render()
marcon().top(10)
.left(10)
//etc...
.render();
这种方法的优点是,根据您的要求,它允许链接。例如,您可以这样创建SVG:
var mySvg = marcon();
mySvg.top(20)
.left(10)
.right(10)
.bottom(10)
.height(200)
.width(200)
.element("body")
.render();
function marcon() {
var instance = {};
var top = 10,
bottom = 0,
left = 0,
right = 0,
width = 900,
height = 600,
element = "body",
innerWidth, innerHeight, svg;
instance.top = function(d) {
if (!arguments.length) return top;
top = d;
return instance;
};
instance.left = function(d) {
if (!arguments.length) return left;
left = d;
return instance;
};
instance.right = function(d) {
if (!arguments.length) return right;
right = d;
return instance;
};
instance.bottom = function(d) {
if (!arguments.length) return bottom;
bottom = d;
return instance;
};
instance.width = function(d) {
if (!arguments.length) return width;
width = d;
return instance;
};
instance.height = function(d) {
if (!arguments.length) return height;
height = d;
return instance;
};
instance.element = function(d) {
if (!arguments.length) return element;
element = d;
return instance;
};
instance.innerWidth = function() {
return innerWidth;
};
instance.innerHeight = function() {
return innerHeight;
};
instance.svg = function() {
return svg;
};
instance.render = function() {
innerWidth = width - left - right;
innerHeight = height - top - bottom;
svg = d3.select(element)
.append("svg")
.attr("width", innerWidth + left + right)
.attr("height", innerHeight + top + bottom)
.append("g")
.attr("transform", "translate(" + left + ", " + top + ")");
}
return instance;
}
var mySvg = marcon();
mySvg.top(20)
.left(10)
.right(10)
.bottom(20)
.height(200)
.width(200)
.element(".testDiv")
.render();
var rect = mySvg.svg()
.append("rect")
.attr("width", mySvg.innerWidth())
.attr("height", mySvg.innerHeight())
.style("fill", "teal")
svg {
background-color: tan;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<div class="testDiv"></div>
此外,它允许默认设置。如果不设置setter,则默认为指定的值。在这里,如果我们不设置宽度,它默认为900:
function marcon() {
var instance = {};
var top = 10,
bottom = 0,
left = 0,
right = 0,
width = 900,
height = 600,
element = "body",
innerWidth, innerHeight, svg;
instance.top = function(d) {
if (!arguments.length) return top;
top = d;
return instance;
};
instance.left = function(d) {
if (!arguments.length) return left;
left = d;
return instance;
};
instance.right = function(d) {
if (!arguments.length) return right;
right = d;
return instance;
};
instance.bottom = function(d) {
if (!arguments.length) return bottom;
bottom = d;
return instance;
};
instance.width = function(d) {
if (!arguments.length) return width;
width = d;
return instance;
};
instance.height = function(d) {
if (!arguments.length) return height;
height = d;
return instance;
};
instance.element = function(d) {
if (!arguments.length) return element;
element = d;
return instance;
};
instance.innerWidth = function() {
return innerWidth;
};
instance.innerHeight = function() {
return innerHeight;
};
instance.svg = function() {
return svg;
};
instance.render = function() {
innerWidth = width - left - right;
innerHeight = height - top - bottom;
svg = d3.select(element)
.append("svg")
.attr("width", innerWidth + left + right)
.attr("height", innerHeight + top + bottom)
.append("g")
.attr("transform", "translate(" + left + ", " + top + ")");
}
return instance;
}
var mySvg = marcon();
mySvg.top(20)
.left(10)
.right(10)
.bottom(20)
.height(200)
.element("body")
.render();
var rect = mySvg.svg()
.append("rect")
.attr("width", mySvg.innerWidth())
.attr("height", mySvg.innerHeight())
.style("fill", "teal")
svg{
}
<<div class=“testDiv”></div>
最后,您可以使用getter,例如:
marcon().top();
这给了你价值。这是一个演示,请查看控制台:
function marcon() {
var instance = {};
var top = 10,
bottom = 0,
left = 0,
right = 0,
width = 900,
height = 600,
element = "body",
innerWidth, innerHeight, svg;
instance.top = function(d) {
if (!arguments.length) return top;
top = d;
return instance;
};
instance.left = function(d) {
if (!arguments.length) return left;
left = d;
return instance;
};
instance.right = function(d) {
if (!arguments.length) return right;
right = d;
return instance;
};
instance.bottom = function(d) {
if (!arguments.length) return bottom;
bottom = d;
return instance;
};
instance.width = function(d) {
if (!arguments.length) return width;
width = d;
return instance;
};
instance.height = function(d) {
if (!arguments.length) return height;
height = d;
return instance;
};
instance.element = function(d) {
if (!arguments.length) return element;
element = d;
return instance;
};
instance.innerWidth = function() {
return innerWidth;
};
instance.innerHeight = function() {
return innerHeight;
};
instance.svg = function() {
return svg;
};
instance.render = function() {
innerWidth = width - left - right;
innerHeight = height - top - bottom;
svg = d3.select(element)
.append("svg")
.attr("width", innerWidth + left + right)
.attr("height", innerHeight + top + bottom)
.append("g")
.attr("transform", "translate(" + left + ", " + top + ")");
}
return instance;
}
var mySvg = marcon();
mySvg.top(20)
.left(10)
.right(10)
.bottom(20)
.height(200)
.width(200)
.element("body")
.render();
var rect = mySvg.svg()
.append("rect")
.attr("width", mySvg.innerWidth())
.attr("height", mySvg.innerHeight())
.style("fill", "teal");
console.log("The height is " + mySvg.height())
svg{
背景色:棕褐色;
}
<<div class=“testDiv”></div>
|