On Github xebia-france / d3js-workshop-pres
Created by Julien Buret / @julien_buret
<svg width="300" height="100"> <rect fill="white" stroke="black"></rect> <text x="10" y="20">Hello world in SVG</text> </svg>
<style> .intro-rect { fill: gray; opacity: 0.3; stroke: black; stroke-width: 4; } .intro-text { font-size: 34; fill: white, stroke: black } </style>
var svg = d3.select("#d3-slide1").append("svg"); svg.attr("width", 200).attr("height", 200); svg.append("circle") .attr("cx", 50) .attr("cy", 50) .attr("r", 30) .attr("fill","red") .attr("stroke","black")
var svg = d3.select("#d3-slide1").append("svg"); svg.attr("width", 200).attr("height", 200); svg.append("rect") .attr("x", 75) .attr("y", 10) .attr("width", 50) .attr("height", 100) .attr("fill","blue") .attr("stroke","black")
var lineData = [ { "x": -5, "y": 25}, { "x": -4, "y": 16}, { "x": -3, "y": 9}, { "x": -2, "y": 4}, { "x": -1, "y": 1}, { "x": 0, "y": 0}, { "x": 1, "y": 1}, { "x": 2, "y": 4}, { "x": 3, "y": 9}, { "x": 4, "y": 16}, { "x": 5, "y": 25}]; var lineFunction = d3.svg.line() .x(function(d) { return (d.x * 20) + 150; }) .y(function(d) { return -(d.y * 5) + 180; }) .interpolate("basis"); var lineGraph = svg.append("path") .attr("d", lineFunction(lineData)) .attr("stroke", "blue") .attr("stroke-width", 3) .attr("fill", "none");
// Select body var body = d3.select("body") // Select an element by id var byId = d3.select("#id_my_element")
// Select all rect d3.selectAll("rect")
// Select svg element var svg = d3.select("svg") // Append a rectangle var rect = svg.append("rect") rect.attr("x", 10) rect.attr("y", 10) rect.attr("width", 30) rect.attr("height", 30)
d3.selectAll("section") .append("text")
var oneDim = [1,2,3,5] var twoDim = [[1,1],[2,5],[3,6]]
var complexData = [ {x: 100, y: 80, count: 12}, {x: 120, y: 60, count: 20}, {x: 140, y: 40, count: 8}, {x: 120, y: 120, count: 17} ]
var complexData = [ {x: 100, y: 80, count: 12}, {x: 120, y: 60, count: 20}, {x: 140, y: 40, count: 8}, {x: 120, y: 120, count: 17} ] svg.selectAll("circle") .data(complexData) .enter().append("circ") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", function(d){return d.count})
var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var svg = d3.select("#my_svg").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 + ")");
var data = [[1393940673, 14], [1393940683, 27], ...
var y = d3.scale.linear().range([height,0]); y.domain([0,d3.max(data, function (d) {return d[1]})]); var axey = d3.svg.axis().scale(y).orient('left'); svg.append('g').attr('class', 'axis').call(axey);
var x = d3.time.scale().range([0,width]); x.domain(d3.extent(data, function (d) { return new Date(d[0] * 1000); })); var axex = d3.svg.axis().scale(x).orient('bottom'); var xaxis = svg.append('g').attr('class', 'axis').attr('transform', 'translate(0,' + height + ')').call(axex); xaxis.selectAll('text').style('text-anchor', 'end').attr('dx', '-.8em').attr('dy', '.15em').attr('transform', function () { return 'rotate(-60)'; });
.axis_d3_h1_axes { font: 16px sans-serif; } .axis_d3_h1_axes path, .axis_d3_h1_axes line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis_d3_h1_axes path { display: none; } .line_d3_h1_axes { fill: none; stroke: steelblue; stroke-width: 2px; }
var line = d3.svg.line() .x(function (d) {return x(d[0] * 1000);}) .y(function (d) {return y(d[1]);}); svg.append('path').datum(data).attr('class', 'line_d3_h1_axes').attr('d', line);
var x = d3.scale.ordinal().rangeRoundBands([0,width], 0.1); x.domain(distributionData.map(function (d) {return d[0];})); var axex = d3.svg.axis().scale(x).orient('bottom'); var xaxis = svg.append('g').attr('class', 'axis').attr('transform', 'translate(0,' + height + ')').call(axex); xaxis.selectAll('text').style('text-anchor', 'end').attr('dx', '-.8em').attr('dy', '.15em').attr('transform', function () { return 'rotate(-60)'; });
var x = d3.scale.ordinal().rangeRoundBands([0,width], 0.1); x.domain(distributionData.map(function (d) {return d[0];})); var axex = d3.svg.axis().scale(x).orient('bottom'); var xaxis = svg.append('g').attr('class', 'axis').attr('transform', 'translate(0,' + height + ')').call(axex); xaxis.selectAll('text').style('text-anchor', 'end').attr('dx', '-.8em').attr('dy', '.15em').attr('transform', function () { return 'rotate(-60)'; });
var line = d3.svg.line().interpolate('basis').x(function (d) { return x(d.key); }).y(function (d) { return y1(d.totalcount / distributionDataFull.total); }); svg.datum(distributionData); svg.append('path').attr('class', 'line').attr('d', line).style('stroke', 'orange') .style('fill', 'none').style('stroke-width', '2px');
var ygrid = d3.svg.axis().scale(y1).orient('left'); svg.append('g').attr('class', 'grid').call(ygrid.tickSize(-width, 0, 0).tickFormat(''));