js 語法總結
阿新 • • 發佈:2018-11-11
js 是使用比較靈活的語言,語法 書寫比較靈活,在js中function具有非常大的靈活性,function程式碼本身不僅允許被執行,還允許對值域範圍內的程式碼進行擴充套件,支援在函式程式碼執行後再返回可繼續執行的函式,同時可採用類似物件操作的方法對位函式增加屬性或方法,參見示例:
function circularHeatChart() { var margin = {top: 20, right: 20, bottom: 20, left: 20}, innerRadius = 50, numSegments = 24, segmentHeight = 20, domain = null, range = ["white", "red"], accessor = function(d) {return d;}, radialLabels = segmentLabels = []; function chart(selection) { //==================在d3中選擇器使用each進行選擇器的遍歷,對set、map物件使用forEach進行成員資料遍歷=== selection.each(function(data) { var svg = d3.select(this); var offset = innerRadius + Math.ceil(data.length / numSegments) * segmentHeight; g = svg.append("g") .classed("circular-heat", true) .attr("transform", "translate(" + parseInt(margin.left + offset) + "," + parseInt(margin.top + offset) + ")"); var autoDomain = false; //=========返回陣列中的最大值、最小值,使用accessor的目的是為了具有擴充套件性===== if (domain === null) { domain = d3.extent(data, accessor); autoDomain = true; } //====================可以用線性比例尺返回特定範圍內的顏色值=========================== var color = d3.scale.linear().domain(domain).range(range); if(autoDomain) domain = null; //=============這個圖還不是很靈活,圖形360度繪製的弧度數量=numSegment,當繪製的資料數量大於numSegment,逐層向外擴充套件繪製 g.selectAll("path").data(data) .enter().append("path") .attr("d", d3.svg.arc().innerRadius(ir).outerRadius(or).startAngle(sa).endAngle(ea)) .attr("fill", function(d) {return color(accessor(d));}); // Unique id so that the text path defs are unique - is there a better way to do this? var id = d3.selectAll(".circular-heat")[0].length; //Radial labels var lsa = 0.01; //Label start angle var labels = svg.append("g") .classed("labels", true) .classed("radial", true) .attr("transform", "translate(" + parseInt(margin.left + offset) + "," + parseInt(margin.top + offset) + ")"); labels.selectAll("def") .data(radialLabels).enter() .append("def") .append("path") .attr("id", function(d, i) {return "radial-label-path-"+id+"-"+i;}) .attr("d", function(d, i) { var r = innerRadius + ((i + 0.2) * segmentHeight); return "m" + r * Math.sin(lsa) + " -" + r * Math.cos(lsa) + " a" + r + " " + r + " 0 1 1 -1 0"; }); labels.selectAll("text") .data(radialLabels).enter() .append("text") .append("textPath") .attr("xlink:href", function(d, i) {return "#radial-label-path-"+id+"-"+i;}) .style("font-size", 0.6 * segmentHeight + 'px') .text(function(d) {return d;}); //Segment labels var segmentLabelOffset = 2; var r = innerRadius + Math.ceil(data.length / numSegments) * segmentHeight + segmentLabelOffset; labels = svg.append("g") .classed("labels", true) .classed("segment", true) .attr("transform", "translate(" + parseInt(margin.left + offset) + "," + parseInt(margin.top + offset) + ")"); labels.append("def") .append("path") .attr("id", "segment-label-path-"+id) .attr("d", "m0 -" + r + " a" + r + " " + r + " 0 1 1 -1 0"); labels.selectAll("text") .data(segmentLabels).enter() .append("text") .append("textPath") .attr("xlink:href", "#segment-label-path-"+id) .attr("startOffset", function(d, i) {return i * 100 / numSegments + "%";}) .text(function(d) {return d;}); }); } /* Arc functions */ ir = function(d, i) { return innerRadius + Math.floor(i/numSegments) * segmentHeight; } or = function(d, i) { return innerRadius + segmentHeight + Math.floor(i/numSegments) * segmentHeight; } sa = function(d, i) { return (i * 2 * Math.PI) / numSegments; } ea = function(d, i) { return ((i + 1) * 2 * Math.PI) / numSegments; } /* Configuration getters/setters */ chart.margin = function(_) { if (!arguments.length) return margin; margin = _; return chart; }; chart.innerRadius = function(_) { if (!arguments.length) return innerRadius; innerRadius = _; return chart; }; chart.numSegments = function(_) { if (!arguments.length) return numSegments; numSegments = _; return chart; }; chart.accessor = function(_) { if (!arguments.length) return accessor; accessor = _; return chart; }; return chart; }
可使用變數賦值的方式呼叫函式circularHeatChart,呼叫完成後該變數既擁有了circularHeatChart的返回函式,可通過call等方式執行該變數,完成函式呼叫,示例程式碼:
var chart = circularHeatChart() .segmentHeight(20) .innerRadius(20) .numSegments(24) .radialLabels(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]) .segmentLabels(["Midnight", "1am", "2am", "3am", "4am", "5am", "6am", "7am", "8am", "9am", "10am", "11am", "Midday", "1pm", "2pm", "3pm", "4pm", "5pm", "6pm", "7pm", "8pm", "9pm", "10pm", "11pm"]) .margin({top: 20, right: 0, bottom: 20, left: 280}); d3.select('#energychart') .selectAll('svg') .data([energyData]) .enter() .append('svg') .call(chart);
當然對函式物件chart的呼叫執行也可以採用這樣的方式:
chart(d3.select('#energychart')
.selectAll('svg')
.data([energyData])
.enter()
.append('svg'))