File indexing completed on 2024-12-22 05:34:40

0001 !(function (d3) {
0002 
0003   $("#d3linelableinline").empty();
0004   // new memeber project stati
0005   var parseTime = d3.timeParse("%Y-%m-%d");
0006   var svgLine = d3.select("#d3linelableinline")
0007       .append("svg")
0008       .attr("width", 1200)
0009       .attr("height", 600);
0010 
0011   var marginLine = {top: 30, right: 50, bottom: 30, left: 30},
0012       widthLine = +svgLine.attr("width") - marginLine.left - marginLine.right,
0013       heightLine = +svgLine.attr("height") - marginLine.top - marginLine.bottom,
0014       labelPadding = 3;
0015 
0016   var g = svgLine.append("g")
0017       .attr("transform", "translate(" + marginLine.left + "," + marginLine.top + ")");
0018 
0019 
0020       d3.json("/backend/index/getnewmembersprojects", function(error, data) {        
0021         if (error) throw error;
0022         data = data.results;
0023         
0024         // format the data
0025         data.forEach(function(d) {
0026             d.date = parseTime(d.date);
0027             d.members = +d.members;
0028             d.projects = +d.projects;
0029         });
0030 
0031         data.columns=['date','members','projects'];      
0032         var series = data.columns.slice(1).map(function(key) {
0033           return data.map(function(d) {
0034             return {
0035               key: key,
0036               date: d.date,
0037               value: d[key]
0038             };
0039           });
0040         });
0041 
0042 
0043         var xLine = d3.scaleTime()
0044             .domain([data[0].date, data[data.length - 1].date])
0045             .range([0, widthLine]);
0046 
0047         var yLine = d3.scaleLinear()
0048             .domain([0, d3.max(series, function(s) { return d3.max(s, function(d) { return d.value; }); })])
0049             .range([heightLine, 0]);
0050 
0051         var zLine = d3.scaleOrdinal(d3.schemeCategory10);
0052 
0053         g.append("g")
0054             .attr("class", "axis axis--x")
0055             .attr("transform", "translate(0," + heightLine + ")")
0056             .call(d3.axisBottom(xLine));
0057 
0058         var serie = g.selectAll(".serie")
0059             .data(series)
0060           .enter().append("g")
0061             .attr("class", "serie");
0062 
0063         serie.append("path")
0064             .attr("class", "line")
0065             .style("stroke", function(d) { return zLine(d[0].key); })
0066             .attr("d", d3.line()
0067                 .x(function(d) { return xLine(d.date); })
0068                 .y(function(d) { return yLine(d.value); }));
0069 
0070         var label = serie.selectAll(".label")
0071             .data(function(d) { return d; })
0072           .enter().append("g")
0073             .attr("class", "label")
0074             .attr("transform", function(d, i) { return "translate(" + xLine(d.date) + "," + yLine(d.value) + ")"; });
0075             
0076             label.append("text")
0077                  .attr("dy", ".35em")
0078                  .text(function(d) { return d.value; })
0079                .filter(function(d, i) { return i === data.length - 1; })
0080               
0081 
0082        const newText = label.selectAll('text');
0083        const bbox = newText.node().getBBox();
0084 
0085        label.append('rect', 'text')
0086            .datum(() => bbox)
0087            .attr('x', d => (d.x - labelPadding))
0088            .attr('y', d => (d.y - labelPadding))
0089            .attr('width', d => (d.width + (2 * labelPadding)))
0090            .attr('height', d => (d.height + (2 * labelPadding)));
0091 
0092            label.append("text")
0093                 .attr("dy", ".35em")
0094                 .text(function(d) { return d.value; })
0095               .filter(function(d, i) { return i === data.length - 1; })
0096               .append("tspan")
0097                 .attr("class", "label-key")
0098                 .text(function(d) { return " " + d.key; });
0099 
0100       });
0101 
0102 
0103 })(d3);