UNPKG

typescript-closure-tools

Version:

Command-line tools to convert closure-style JSDoc annotations to typescript, and to convert typescript sources to closure externs files

1,542 lines (1,248 loc) 83.8 kB
/// <reference path="d3.d.ts" /> //Example from http://bl.ocks.org/3887235 function testPieChart() { var width = 960, height = 500, radius = Math.min(width, height) / 2; var color = d3.scale.ordinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); var arc = d3.svg.arc() .outerRadius(radius - 10) .innerRadius(0); var pie = d3.layout.pie() .sort(null) .value(function (d) { return d.population; }); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); d3.csv("data.csv", function (error, data) { data.forEach(function (d) { d.population = +d.population; }); var g = svg.selectAll(".arc") .data(pie(data)) .enter().append("g") .attr("class", "arc"); g.append("path") .attr("d", arc) .style("fill", function (d) { return color(d.data.age); }); g.append("text") .attr("transform", function (d) { return "translate(" + arc.centroid(d) + ")"; }) .attr("dy", ".35em") .style("text-anchor", "middle") .text(function (d) { return d.data.age; }); }); } //Example from http://bl.ocks.org/3887051 interface GroupedData { State: string; ages: Array<{ name: string; value: number;}>; } function groupedBarChart() { var margin = { top: 20, right: 20, bottom: 30, left: 40 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x0 = d3.scale.ordinal() .rangeRoundBands([0, width], .1); var x1 = d3.scale.ordinal(); var y = d3.scale.linear() .range([height, 0]); var color = d3.scale.ordinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); var xAxis = d3.svg.axis() .scale(x0) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .tickFormat(d3.format(".2s")); 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 + ")"); d3.csv("data.csv", function (error, data: Array<GroupedData>) { var ageNames = d3.keys(data[0]).filter(function (key) { return key !== "State"; }); data.forEach(function (d) { d.ages = ageNames.map(function (name) { return { name: name, value: +d[name] }; }); }); x0.domain(data.map(function (d) { return d.State; })); x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]); y.domain([0, d3.max(data, function (d) { return d3.max(d.ages, function (d) { return d.value; }); })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Population"); var state = svg.selectAll(".state") .data(data) .enter().append("g") .attr("class", "g") .attr("transform", function (d) { return "translate(" + x0(d.State) + ",0)"; }); state.selectAll("rect") .data(function (d) { return d.ages; }) .enter().append("rect") .attr("width", x1.rangeBand()) .attr("x", function (d) { return x1(d.name); }) .attr("y", function (d) { return y(d.value); }) .attr("height", function (d) { return height - y(d.value); }) .style("fill", function (d) { return color(d.name); }); var legend = svg.selectAll(".legend") .data(ageNames.reverse()) .enter().append("g") .attr("class", "legend") .attr("transform", function (d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width - 18) .attr("width", 18) .attr("height", 18) .style("fill", color); legend.append("text") .attr("x", width - 24) .attr("y", 9) .attr("dy", ".35em") .style("text-anchor", "end") .text(function (d) { return d; }); }); } //Example from http://bl.ocks.org/3886208 function stackedBarChart() { var margin = { top: 20, right: 20, bottom: 30, left: 40 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); var y = d3.scale.linear() .rangeRound([height, 0]); var color = d3.scale.ordinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .tickFormat(d3.format(".2s")); 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 + ")"); d3.csv("data.csv", function (error, data) { color.domain(d3.keys(data[0]).filter(function (key) { return key !== "State"; })); data.forEach(function (d) { var y0 = 0; d.ages = color.domain().map(function (name) { return { name: name, y0: y0, y1: y0 += +d[name] }; }); d.total = d.ages[d.ages.length - 1].y1; }); data.sort(function (a, b) { return b.total - a.total; }); x.domain(data.map(function (d) { return d.State; })); y.domain([0, d3.max(data, function (d) { return d.total; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Population"); var state = svg.selectAll(".state") .data(data) .enter().append("g") .attr("class", "g") .attr("transform", function (d) { return "translate(" + x(d.State) + ",0)"; }); state.selectAll("rect") .data(function (d) { return d.ages; }) .enter().append("rect") .attr("width", x.rangeBand()) .attr("y", function (d) { return y(d.y1); }) .attr("height", function (d) { return y(d.y0) - y(d.y1); }) .style("fill", function (d) { return color(d.name); }); var legend = svg.selectAll(".legend") .data(color.domain().reverse()) .enter().append("g") .attr("class", "legend") .attr("transform", function (d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width - 18) .attr("width", 18) .attr("height", 18) .style("fill", color); legend.append("text") .attr("x", width - 24) .attr("y", 9) .attr("dy", ".35em") .style("text-anchor", "end") .text(function (d) { return d; }); }); } // example from http://bl.ocks.org/3886394 function normalizedBarChart() { var margin = { top: 20, right: 100, bottom: 30, left: 40 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); var y = d3.scale.linear() .rangeRound([height, 0]); var color = d3.scale.ordinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .tickFormat(d3.format(".0%")); 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 + ")"); d3.csv("data.csv", function (error, data) { color.domain(d3.keys(data[0]).filter(function (key) { return key !== "State"; })); data.forEach(function (d) { var y0 = 0; d.ages = color.domain().map(function (name) { return { name: name, y0: y0, y1: y0 += +d[name] }; }); d.ages.forEach(function (d) { d.y0 /= y0; d.y1 /= y0; }); }); data.sort(function (a, b) { return b.ages[0].y1 - a.ages[0].y1; }); x.domain(data.map(function (d) { return d.State; })); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis); var state = svg.selectAll(".state") .data(data) .enter().append("g") .attr("class", "state") .attr("transform", function (d) { return "translate(" + x(d.State) + ",0)"; }); state.selectAll("rect") .data(function (d) { return d.ages; }) .enter().append("rect") .attr("width", x.rangeBand()) .attr("y", function (d) { return y(d.y1); }) .attr("height", function (d) { return y(d.y0) - y(d.y1); }) .style("fill", function (d) { return color(d.name); }); var legend = svg.select(".state:last-child").selectAll(".legend") .data(function (d) { return d.ages; }) .enter().append("g") .attr("class", "legend") .attr("transform", function (d) { return "translate(" + x.rangeBand() / 2 + "," + y((d.y0 + d.y1) / 2) + ")"; }); legend.append("line") .attr("x2", 10); legend.append("text") .attr("x", 13) .attr("dy", ".35em") .text(function (d) { return d.name; }); }); } // example from http://bl.ocks.org/3885705 function sortablebarChart() { var margin = { top: 20, right: 20, bottom: 30, left: 40 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var formatPercent = d3.format(".0%"); var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1, 1); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .tickFormat(formatPercent); 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 + ")"); d3.tsv("data.tsv", function (error, data) { data.forEach(function (d) { d.frequency = +d.frequency; }); x.domain(data.map(function (d) { return d.letter; })); y.domain([0, d3.max(data, function (d) { return d.frequency; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Frequency"); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function (d) { return x(d.letter); }) .attr("width", x.rangeBand()) .attr("y", function (d) { return y(d.frequency); }) .attr("height", function (d) { return height - y(d.frequency); }); d3.select("input").on("change", change); var sortTimeout = setTimeout(function () { d3.select("input").property("checked", true).each(change); }, 2000); function change() { clearTimeout(sortTimeout); var x0 = x.domain(data.sort(this.checked ? function (a, b) { return b.frequency - a.frequency; } : function (a, b) { return d3.ascending(a.letter, b.letter); }) .map(function (d) { return d.letter; })) .copy(); var transition = svg.transition().duration(750), delay = function (d, i) { return i * 50; }; transition.selectAll(".bar") .delay(delay) .attr("x", function (d) { return x0(d.letter); }); transition.select(".x.axis") .call(xAxis) .selectAll("g") .delay(delay); } }); } //example from http://bl.ocks.org/4063318 function callenderView() { var width = 960, height = 136, cellSize = 17; // cell size var day = d3.time.format("%w"), week = d3.time.format("%U"), percent = d3.format(".1%"), format = d3.time.format("%Y-%m-%d"); var color = d3.scale.quantize() .domain([-.05, .05]) .range(d3.range(11).map(function (d) { return "q" + d + "-11"; })); var svg = d3.select("body").selectAll("svg") .data(d3.range(1990, 2011)) .enter().append("svg") .attr("width", width) .attr("height", height) .attr("class", "RdYlGn") .append("g") .attr("transform", "translate(" + ((width - cellSize * 53) / 2) + "," + (height - cellSize * 7 - 1) + ")"); svg.append("text") .attr("transform", "translate(-6," + cellSize * 3.5 + ")rotate(-90)") .style("text-anchor", "middle") .text(function (d) { return d; }); var rect = svg.selectAll(".day") .data(function (d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); }) .enter().append("rect") .attr("class", "day") .attr("width", cellSize) .attr("height", cellSize) .attr("x", function (d) { return parseInt(week(d)) * cellSize; }) .attr("y", function (d) { return parseInt(day(d)) * cellSize; }) .datum(format); rect.append("title") .text(function (d) { return d; }); svg.selectAll(".month") .data(function (d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); }) .enter().append("path") .attr("class", "month") .attr("d", monthPath); d3.csv("dji.csv", function (error, csv) { var data = d3.nest() .key(function (d) { return d.Date; }) .rollup(function (d) { return (d[0].Close - d[0].Open) / d[0].Open; }) .map(csv); rect.filter(function (d) { return d in data; }) .attr("class", function (d) { return "day " + color(data[d]); }) .select("title") .text(function (d) { return d + ": " + percent(data[d]); }); }); function monthPath(t0) { var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0), d0 = +day(t0), w0 = +week(t0), d1 = +day(t1), w1 = +week(t1); return "M" + (w0 + 1) * cellSize + "," + d0 * cellSize + "H" + w0 * cellSize + "V" + 7 * cellSize + "H" + w1 * cellSize + "V" + (d1 + 1) * cellSize + "H" + (w1 + 1) * cellSize + "V" + 0 + "H" + (w0 + 1) * cellSize + "Z"; } d3.select(self.frameElement).style("height", "2910px"); } // example from http://bl.ocks.org/3883245 function lineChart() { var margin = { top: 20, right: 20, bottom: 30, left: 50 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var parseDate = d3.time.format("%d-%b-%y").parse; var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .x(function (d) { return x(d.date); }) .y(function (d) { return y(d.close); }); 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 + ")"); d3.tsv("data.tsv", function (error, data) { data.forEach(function (d) { d.date = parseDate(d.date); d.close = +d.close; }); x.domain(d3.extent(data, function (d) { return d.date; })); y.domain(d3.extent(data, function (d) { return d.close; })); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Price ($)"); svg.append("path") .datum(data) .attr("class", "line") .attr("d", line); }); } //example from http://bl.ocks.org/3884914 function bivariateAreaChart() { var margin = { top: 20, right: 20, bottom: 30, left: 50 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var parseDate = d3.time.format("%Y%m%d").parse; var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var area = d3.svg.area() .x(function (d) { return x(d.date); }) .y0(function (d) { return y(d.low); }) .y1(function (d) { return y(d.high); }); 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 + ")"); d3.tsv("data.tsv", function (error, data) { data.forEach(function (d) { d.date = parseDate(d.date); d.low = +d.low; d.high = +d.high; }); x.domain(d3.extent(data, function (d) { return d.date; })); y.domain([d3.min(data, function (d) { return d.low; }), d3.max(data, function (d) { return d.high; })]); svg.append("path") .datum(data) .attr("class", "area") .attr("d", area); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Temperature (ºF)"); }); } //Example from http://bl.ocks.org/mbostock/1557377 function dragMultiples() { var width = 238, height = 123, radius = 20; var drag = d3.behavior.drag() .origin(Object) .on("drag", dragmove); var svg = d3.select("body").selectAll("svg") .data(d3.range(16).map(function () { return { x: width / 2, y: height / 2 }; })) .enter().append("svg") .attr("width", width) .attr("height", height); svg.append("circle") .attr("r", radius) .attr("cx", function (d) { return d.x; }) .attr("cy", function (d) { return d.y; }) .call(drag); function dragmove(d) { d3.select(this) .attr("cx", d.x = Math.max(radius, Math.min(width - radius, d3.event.x))) .attr("cy", d.y = Math.max(radius, Math.min(height - radius, d3.event.y))); } } //Example from http://bl.ocks.org/mbostock/3892919 function panAndZoom() { var margin = { top: 20, right: 20, bottom: 30, left: 40 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.linear() .domain([-width / 2, width / 2]) .range([0, width]); var y = d3.scale.linear() .domain([-height / 2, height / 2]) .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom") .tickSize(-height); var yAxis = d3.svg.axis() .scale(y) .orient("left") .ticks(5) .tickSize(-width); var zoom = d3.behavior.zoom() .x(x) .y(y) .scaleExtent([1, 10]) .on("zoom", zoomed); 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 + ")") .call(zoom); svg.append("rect") .attr("width", width) .attr("height", height); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis); function zoomed() { svg.select(".x.axis").call(xAxis); svg.select(".y.axis").call(yAxis); } } //Example from http://bl.ocks.org/mbostock/1125997 function chainedTransitions() { var w = 960, h = 500, y = d3.scale.ordinal().domain(d3.range(50)).rangePoints([20, h - 20]), t = Date.now(); var svg = d3.select("body").append("svg:svg") .attr("width", w) .attr("height", h); var circle = svg.selectAll("circle") .data(y.domain()) .enter().append("svg:circle") .attr("r", 16) .attr("cx", 20) .attr("cy", y) .each(slide(20, w - 20)); function slide(x0, x1) { t += 50; return function() { d3.select(this).transition() .duration(t - Date.now()) .attr("cx", x1) .each("end", slide(x1, x0)); }; } } //Example from http://bl.ocks.org/mbostock/4062085 interface PyramidData { people: number; year: number; age: number; } function populationPyramid() { var margin = { top: 20, right: 40, bottom: 30, left: 20 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom, barWidth = Math.floor(width / 19) - 1; var x = d3.scale.linear() .range([barWidth / 2, width - barWidth / 2]); var y = d3.scale.linear() .range([height, 0]); var yAxis = d3.svg.axis() .scale(y) .orient("right") .tickSize(-width) .tickFormat(function (d) { return Math.round(d / 1e6) + "M"; } ); // An SVG element with a bottom-right origin. 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 + ")"); // A sliding container to hold the bars by birthyear. var birthyears = svg.append("g") .attr("class", "birthyears"); // A label for the current year. var title = svg.append("text") .attr("class", "title") .attr("dy", ".71em") .text(2000); d3.csv("population.csv", function (error, data: Array<PyramidData>) { // Convert strings to numbers. data.forEach(function (d) { d.people = +d.people; d.year = +d.year; d.age = +d.age; } ); // Compute the extent of the data set in age and years. var age1 = d3.max(data, function (d) { return d.age; } ), year0 = d3.min(data, function (d) { return d.year; } ), year1 = d3.max(data, function (d) { return d.year; } ), year = year1; // Update the scale domains. x.domain([year1 - age1, year1]); y.domain([0, d3.max(data, function (d) { return d.people; } )]); // Produce a map from year and birthyear to [male, female]. data = d3.nest() .key(function (d) { return d.year; } ) .key(function (d) { return '' + (d.year - d.age); } ) .rollup(function (v) { return v.map(function (d) { return d.people; } ); } ) .map(data); // Add an axis to show the population values. svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + width + ",0)") .call(yAxis) .selectAll("g") .filter(function (value) { return !value; } ) .classed("zero", true); // Add labeled rects for each birthyear (so that no enter or exit is required). var birthyear = birthyears.selectAll(".birthyear") .data(d3.range(year0 - age1, year1 + 1, 5)) .enter().append("g") .attr("class", "birthyear") .attr("transform", function (birthyear) { return "translate(" + x(birthyear) + ",0)"; } ); birthyear.selectAll("rect") .data(function (birthyear) { return data[year][birthyear] || [0, 0]; } ) .enter().append("rect") .attr("x", -barWidth / 2) .attr("width", barWidth) .attr("y", y) .attr("height", function (value) { return height - y(value); } ); // Add labels to show birthyear. birthyear.append("text") .attr("y", height - 4) .text(function (birthyear) { return birthyear; } ); // Add labels to show age (separate; not animated). svg.selectAll(".age") .data(d3.range(0, age1 + 1, 5)) .enter().append("text") .attr("class", "age") .attr("x", function (age) { return x(year - age); } ) .attr("y", height + 4) .attr("dy", ".71em") .text(function (age) { return age; } ); // Allow the arrow keys to change the displayed year. window.focus(); d3.select(window).on("keydown", function () { switch (d3.event.keyCode) { case 37: year = Math.max(year0, year - 10); break; case 39: year = Math.min(year1, year + 10); break; } update(); } ); function update() { if (!(<any>year in data)) return; title.text(year); birthyears.transition() .duration(750) .attr("transform", "translate(" + (x(year1) - x(year)) + ",0)"); birthyear.selectAll("rect") .data(function (birthyear) { return data[year][birthyear] || [0, 0]; } ) .transition() .duration(750) .attr("y", y) .attr("height", function (value) { return height - y(value); } ); } } ); } //Example from http://bl.ocks.org/MoritzStefaner/1377729 function forcedBasedLabelPlacemant() { var w = 960, h = 500; var labelDistance = 0; var vis = d3.select("body").append("svg:svg").attr("width", w).attr("height", h); var nodes = []; var labelAnchors = []; var labelAnchorLinks = []; var links = []; for (var i = 0; i < 30; i++) { var nodeLabel = { label: "node " + i }; nodes.push(nodeLabel); labelAnchors.push({ node: nodeLabel }); labelAnchors.push({ node: nodeLabel }); }; for (var i = 0; i < nodes.length; i++) { for (var j = 0; j < i; j++) { if (Math.random() > .95) links.push({ source: i, target: j, weight: Math.random() }); } labelAnchorLinks.push({ source: i * 2, target: i * 2 + 1, weight: 1 }); }; var force = d3.layout.force().size([w, h]).nodes(nodes).links(links).gravity(1).linkDistance(50).charge(-3000).linkStrength(function (x) { return x.weight * 10 } ); force.start(); var force2 = d3.layout.force().nodes(labelAnchors).links(labelAnchorLinks).gravity(0).linkDistance(0).linkStrength(8).charge(-100).size([w, h]); force2.start(); var link = vis.selectAll("line.link").data(links).enter().append("svg:line").attr("class", "link").style("stroke", "#CCC"); var node = vis.selectAll("g.node").data(force.nodes()).enter().append("svg:g").attr("class", "node"); node.append("svg:circle").attr("r", 5).style("fill", "#555").style("stroke", "#FFF").style("stroke-width", 3); node.call(force.drag); var anchorLink = vis.selectAll("line.anchorLink").data(labelAnchorLinks)//.enter().append("svg:line").attr("class", "anchorLink").style("stroke", "#999"); var anchorNode = vis.selectAll("g.anchorNode").data(force2.nodes()).enter().append("svg:g").attr("class", "anchorNode"); anchorNode.append("svg:circle").attr("r", 0).style("fill", "#FFF"); anchorNode.append("svg:text").text(function (d, i) { return i % 2 == 0 ? "" : d.node.label } ).style("fill", "#555").style("font-family", "Arial").style("font-size", 12); var updateLink = function () { this.attr("x1", function (d) { return d.source.x; } ).attr("y1", function (d) { return d.source.y; } ).attr("x2", function (d) { return d.target.x; } ).attr("y2", function (d) { return d.target.y; } ); } var updateNode = function () { this.attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; } ); } force.on("tick", function () { force2.start(); node.call(updateNode); anchorNode.each(function (d, i) { if (i % 2 == 0) { d.x = d.node.x; d.y = d.node.y; } else { var b = this.childNodes[1].getBBox(); var diffX = d.x - d.node.x; var diffY = d.y - d.node.y; var dist = Math.sqrt(diffX * diffX + diffY * diffY); var shiftX = b.width * (diffX - dist) / (dist * 2); shiftX = Math.max(-b.width, Math.min(0, shiftX)); var shiftY = 5; this.childNodes[1].setAttribute("transform", "translate(" + shiftX + "," + shiftY + ")"); } } ); anchorNode.call(updateNode); link.call(updateLink); anchorLink.call(updateLink); } ); } //Example from http://bl.ocks.org/mbostock/1125997 function forceCollapsable() { var w = 1280, h = 800, node, link, root; var force = d3.layout.force() .on("tick", tick) .charge(function (d) { return d._children ? -d.size / 100 : -30; } ) .linkDistance(function (d) { return d.target._children ? 80 : 30; } ) .size([w, h - 160]); var vis = d3.select("body").append("svg:svg") .attr("width", w) .attr("height", h); d3.json("flare.json", function (json) { root = json; root.fixed = true; root.x = w / 2; root.y = h / 2 - 80; update(); } ); function update() { var nodes = flatten(root), links = d3.layout.tree().links(nodes); // Restart the force layout. force .nodes(nodes) .links(links) .start(); // Update the links… link = vis.selectAll("line.link") .data(links, function (d) { return d.target.id; } ); // Enter any new links. link.enter().insert("svg:line", ".node") .attr("class", "link") .attr("x1", function (d) { return d.source.x; } ) .attr("y1", function (d) { return d.source.y; } ) .attr("x2", function (d) { return d.target.x; } ) .attr("y2", function (d) { return d.target.y; } ); // Exit any old links. link.exit().remove(); // Update the nodes… node = vis.selectAll("circle.node") .data(nodes, function (d) { return d.id; } ) .style("fill", color); node.transition() .attr("r", function (d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; } ); // Enter any new nodes. node.enter().append("svg:circle") .attr("class", "node") .attr("cx", function (d) { return d.x; } ) .attr("cy", function (d) { return d.y; } ) .attr("r", function (d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; } ) .style("fill", color) .on("click", click) .call(force.drag); // Exit any old nodes. node.exit().remove(); } function tick() { link.attr("x1", function (d) { return d.source.x; } ) .attr("y1", function (d) { return d.source.y; } ) .attr("x2", function (d) { return d.target.x; } ) .attr("y2", function (d) { return d.target.y; } ); node.attr("cx", function (d) { return d.x; } ) .attr("cy", function (d) { return d.y; } ); } // Color leaf nodes orange, and packages white or blue. function color(d) { return d._children ? "#3182bd" : d.children ? "#c6dbef" : "#fd8d3c"; } // Toggle children on click. function click(d) { if (d.children) { d._children = d.children; d.children = null; } else { d.children = d._children; d._children = null; } update(); } // Returns a list of all nodes under the root. function flatten(root) { var nodes = [], i = 0; function recurse(node) { if (node.children) node.size = node.children.reduce(function (p, v) { return p + recurse(v); } , 0); if (!node.id) node.id = ++i; nodes.push(node); return node.size; } root.size = recurse(root); return nodes; } } //Example from http://bl.ocks.org/mbostock/3757110 function azimuthalEquidistant() { var width = 960, height = 960; var topojson: any; var projection = d3.geo.azimuthalEquidistant() .scale(150) .translate([width / 2, height / 2]) .clipAngle(180 - 1e-3) .precision(.1); var path = d3.geo.path() .projection(projection); var graticule = d3.geo.graticule(); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); svg.append("defs").append("path") .datum({ type: "Sphere" }) .attr("id", "sphere") .attr("d", path); svg.append("use") .attr("class", "stroke") .attr("xlink:href", "#sphere"); svg.append("use") .attr("class", "fill") .attr("xlink:href", "#sphere"); svg.append("path") .datum(graticule) .attr("class", "graticule") .attr("d", path); d3.json("/mbostock/raw/4090846/world-50m.json", function (error, world) { svg.insert("path", ".graticule") .datum(topojson.feature(world, world.objects.land)) .attr("class", "land") .attr("d", path); svg.insert("path", ".graticule") .datum(topojson.mesh(world, world.objects.countries, function (a, b) { return a !== b; } )) .attr("class", "boundary") .attr("d", path); } ); d3.select(self.frameElement).style("height", height + "px"); } //Example from http://bl.ocks.org/mbostock/4060366 function voronoiTesselation() { var width = 960, height = 500; var vertices = <Array<D3.Geom.Vertice>>d3.range(100).map(function (d) { return [Math.random() * width, Math.random() * height]; } ); var voronoi = d3.geom.voronoi() .clipExtent([[0, 0], [width, height]]); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .attr("class", "PiYG") .on("mousemove", function () { vertices[0] = d3.mouse(this); redraw(); } ); var path = <D3.UpdateSelection>svg.append("g").selectAll("path"); svg.selectAll("circle") .data(vertices.slice(1)) .enter().append("circle") .attr("transform", function (d) { return "translate(" + d + ")"; } ) .attr("r", 2); redraw(); function redraw() { path = path.data(voronoi(vertices).map(function (d) { return "M" + d.join("L") + "Z"; } ), String); path.exit().remove(); path.enter().append("path").attr("class", function (d, i) { return "q" + (i % 9) + "-9"; } ).attr("d", String); path.order(); } } // Example from https://gist.github.com/christophermanning/1734663 function forceDirectedVoronoi() { var w = window.innerWidth > 960 ? 960 : (window.innerWidth || 960), h = window.innerHeight > 500 ? 500 : (window.innerHeight || 500), radius = 5.25, links = [], simulate = true, zoomToAdd = true, color = d3.scale.quantize().domain([10000, 7250]).range(["#dadaeb","#bcbddc","#9e9ac8","#807dba","#6a51a3","#54278f","#3f007d"]) var numVertices = (w*h) / 3000; var vertices = d3.range(numVertices).map(function(i) { var angle = radius * (i+10); return <D3.Layout.GraphNode>{x: angle*Math.cos(angle)+(w/2), y: angle*Math.sin(angle)+(h/2)}; }); var d3_geom_voronoi = d3.geom.voronoi<D3.Layout.GraphNode>() .x(function(d) { return d.x; }) .y(function(d) { return d.y; }) var prevEventScale = 1; var zoom = d3.behavior.zoom().on("zoom", function(d,i) { if (zoomToAdd){ if (d3.event.scale > prevEventScale) { var angle = radius * vertices.length; vertices.push(<D3.Layout.GraphNode>{x: angle*Math.cos(angle)+(w/2), y: angle*Math.sin(angle)+(h/2)}) } else if (vertices.length > 2 && d3.event.scale != prevEventScale) { vertices.pop(); } force.nodes(vertices).start() } else { if (d3.event.scale > prevEventScale) { radius+= .01 } else { radius -= .01 } vertices.forEach(function(d, i) { var angle = radius * (i+10); vertices[i] = <D3.Layout.GraphNode>{x: angle*Math.cos(angle)+(w/2), y: angle*Math.sin(angle)+(h/2)}; }); force.nodes(vertices).start() } prevEventScale = d3.event.scale; }); d3.select(window) .on("keydown", function() { // shift if(d3.event.keyCode == 16) { zoomToAdd = false } // s if(d3.event.keyCode == 83) { simulate = !simulate if(simulate) { force.start() } else { force.stop() } } }) .on("keyup", function() { zoomToAdd = true }) var svg = d3.select("#chart") .append("svg") .attr("width", w) .attr("height", h) .call(zoom) var force = d3.layout.force() .charge(-300) .size([w, h]) .on("tick", update); force.nodes(vertices).start(); var circle = <D3.UpdateSelection>svg.selectAll("circle"); var path = <D3.UpdateSelection>svg.selectAll("path"); var link = <D3.UpdateSelection>svg.selectAll("line"); function update() { path = path.data(d3_geom_voronoi(vertices)); path.enter().append("path") // drag node by dragging cell .call(d3.behavior.drag() .on("drag", function(d, i) { vertices[i] = <D3.Layout.GraphNode>{x: vertices[i].x + d3.event.dx, y: vertices[i].y + d3.event.dy} }) ) .style("fill", function(d, i) { return color(0) }) path.attr("d", function(d) { return "M" + d.join("L") + "Z"; }) .transition().duration(150).style("fill", function(d, i) { return color(d3.geom.polygon(d).area()) }) path.exit().remove(); circle = circle.data(vertices) circle.enter().append("circle") .attr("r", 0) .transition().duration(1000).attr("r", 5); circle.attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }); circle.exit().transition().attr("r", 0).remove(); link = link.data(d3_geom_voronoi.links(vertices)) link.enter().append("line") link.attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }) link.exit().remove() if(!simulate) force.stop() } } //Example from http://bl.ocks.org/mbostock/4341156 function delaunayTesselation() { var width = 960, height = 500; var vertices = <Array<D3.Geom.Vertice>>d3.range(100).map(function (d) { return [Math.random() * width, Math.random() * height]; } ); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .attr("class", "PiYG") .on("mousemove", function () { vertices[0] = d3.mouse(this); redraw(); } ); var path = <D3.UpdateSelection>svg.append("g").selectAll("path"); svg.selectAll("circle") .data(vertices.slice(1)) .enter().append("circle") .attr("transform", function (d) { return "translate(" + d + ")"; } ) .attr("r", 2); redraw(); function redraw() { path = path.data(d3.geom.delaunay(vertices).map(function (d) { return "M" + d.join("L") + "Z"; } ), String); path.exit().remove(); path.enter().append("path").attr("class", function (d, i) { return "q" + (i % 9) + "-9"; } ).attr("d", String); } } //Example from http://bl.ocks.org/mbostock/4343214 function quadtree() { var width = 960, height = 500; var data = d3.range(5000).map(function () { return { x: Math.random() * width, y: Math.random() * width }; } ); var quadtree = d3.geom.quadtree(data, -1, -1, width + 1, height + 1); var brush = d3.svg.brush() .x(d3.scale.identity().domain([0, width])) .y(d3.scale.identity().domain([0, height])) .on("brush", brushed) .extent([[100, 100], [200, 200]]); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); svg.selectAll(".node") .data(nodes(quadtree)) .enter().append("rect") .attr("class", "node") .attr("x", function (d) { return d.x; } ) .attr("y", function (d) { return d.y; } ) .attr("width", function (d) { return d.width; } ) .attr("height", function (d) { return d.height; } ); var point = svg.selectAll(".point") .data(data) .enter().append("circle") .attr("class", "point") .attr("cx", function (d) { return d.x; } ) .attr("cy", function (d) { return d.y; } ) .attr("r", 4); svg.append("g") .attr("class", "brush") .call(brush); brushed(); function brushed() { var extent = brush.extent(); point.each(function (d) { d.scanned = d.selected = false; } ); search(quadtree, extent[0][0], extent[0][1], extent[1][0], extent[1][1]); point.classed("scanned", function (d) { return d.scanned; } ); point.classed("selected", function (d) { return d.selected; } ); } // Collapse the quadtree into an array of rectangles. function nodes(quadtree) { var nodes = []; quadtree.visit(function (node, x1, y1, x2, y2) { nodes.push({ x: x1, y: y1, width: x2 - x1, height: y2 - y1 }); } ); return nodes; } // Find the nodes within the specified rectangle. function search(quadtree, x0, y0, x3, y3) { quadtree.visit(function (node, x1, y1, x2, y2) { var p = node.point; if (p) { p.scanned = true; p.selected = (p.x >= x0) && (p.x < x3) && (p.y >= y0) && (p.y < y3); } return x1 >= x3 || y1 >= y3 || x2 < x0 || y2 < y0; } ); } } //Example from http://bl.ocks.org/mbostock/4341699 function convexHull() { var width = 960, height = 500; var randomX = d3.random.normal(width / 2, 60), randomY = d3.random.normal(height / 2, 60), vertices = d3.range(100).map(function () { return [randomX(), randomY()]; } ); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .on("mousemove", function () { vertices[0] = d3.mouse(this); redraw(); } ) .on("click", function () { vertices.push(d3.mouse(this)); redraw(); } ); svg.append("rect") .attr("width", width) .attr("height", height); var hull = svg.append("path") .attr("class", "hull"); var circle = <D3.UpdateSelection>svg.selectAll("circle"); redraw(); function redraw() { hull.datum(d3.geom.hull(vertices)).attr("d", function (d) { return "M" + d.join("L") + "Z"; } ); circle = circle.data(vertices); circle.enter().append("circle").attr("r", 3); circle.attr("transform", function (d) { return "translate(" + d + ")"; } ); } } // example from http://bl.ocks.org/mbostock/1044242 function hierarchicalEdgeBundling() { var diameter = 960, radius = diameter / 2, innerRadius = radius - 120; var cluster = d3.layout.cluster() .size([360, innerRadius]) .sort(null) .value(function (d) { return d.size; } ); var bundle = d3.layout.bundle(); var line = d3.svg.line.radial() .interpolate("bundle") .tension(.85) .radius(function (d) { return d.y; } ) .angle(function (d) { return d.x / 180 * Math.PI; } ); var svg = d3.select("body").append("svg") .attr("width", diameter) .attr("height", diameter) .append("g") .attr("transform", "translate(" + radius + "," + radius + ")"); d3.json("readme-flare-imports.json", function (error, classes) { var nodes = cluster.nodes(packages.root(classes)), links = packages.imports(nodes); svg.selectAll(".link") .data(bundle(links)) .enter().append("path") .attr("class", "link") .attr("d", line); svg.selectAll(".node") .data(nodes.filter(function (n) { return !n.children; } )) .enter().append("g") .attr("class", "node") .attr("transform", function (d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; } ) .append("text") .attr("dx", function (d) { return d.x < 180 ? 8 : -8; } ) .attr("dy", ".31em") .attr("text-anchor", function (d) { return d.x < 180 ? "start" : "end"; } ) .attr("transform", function (d) { return d.x < 180 ? null : "rotate(180)"; } ) .text(function (d) { return d.key; } ); } ); d3.select(self.frameElement).style("height", diameter + "px"); var packages = { // Lazily construct the package hierarchy from class names. root: function (classes) { var map = {}; function find(name, data?) { var node = map[name], i; if (!node) { node = map[name] = data || { name: name, children: [] }; if (name.length) { node.parent = find(name.substring(0, i = name.lastIndexOf("."))); node.parent.children.push(node); node.key = name.substring(i + 1); } } return node; } classes.forEach(function (d) { find(d.name, d); } ); return map[""]; } , // Return a list of imports for the given array of nodes. imports: function (nodes) { var map = {}, imports = []; // Compute a map from name to node. nodes.forEach(function (d) { map[d.name] = d; } ); // For each import, construct a link from the source to target node. nodes.forEach(function (d) { if (d.imports) d.imports.