d3实现甘蔗图任务图效果
代码语言:html
所属分类:图表
代码描述:d3实现甘蔗图任务图效果
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> * { margin: 0; padding: 0; } body { background: #fff; font-family: 'Open-Sans',sans-serif; } #container{ margin: 0 auto; position: relative; width:800px; overflow: visible; } .svg { width:800px; height:400px; overflow: visible; position:absolute; } .grid .tick { stroke: lightgrey; opacity: 0.3; shape-rendering: crispEdges; } .grid path { stroke-width: 0; } #tag { color: white; background: #FA283D; width: 150px; position: absolute; display: none; padding:3px 6px; margin-left: -80px; font-size: 11px; } #tag:before { border: solid transparent; content: ' '; height: 0; left: 50%; margin-left: -5px; position: absolute; width: 0; border-width: 10px; border-bottom-color: #FA283D; top: -20px; } </style> </head> <body translate="no"> <div id="container"> <div class="svg"></div> <div id="tag"></div> </div> <script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/d3.v3.js"></script> <script > var w = 800; var h = 400; var svg = d3.selectAll(".svg") //.selectAll("svg") .append("svg") .attr("width", w) .attr("height", h) .attr("class", "svg"); var taskArray = [ { task: "conceptualize", type: "development", startTime: "2013-1-28", //year/month/day endTime: "2013-2-1", details: "This actually didn't take any conceptualization" }, { task: "sketch", type: "development", startTime: "2013-2-1", endTime: "2013-2-6", details: "No sketching either, really" }, { task: "color profiles", type: "development", startTime: "2013-2-6", endTime: "2013-2-9" }, { task: "HTML", type: "coding", startTime: "2013-2-2", endTime: "2013-2-6", details: "all three lines of it" }, { task: "write the JS", type: "coding", startTime: "2013-2-6", endTime: "2013-2-9" }, { task: "advertise", type: "promotion", startTime: "2013-2-9", endTime: "2013-2-12", details: "This counts, right?" }, { task: "spam links", type: "promotion", startTime: "2013-2-12", endTime: "2013-2-14" }, { task: "eat", type: "celebration", startTime: "2013-2-8", endTime: "2013-2-13", details: "All the things" }, { task: "crying", type: "celebration", startTime: "2013-2-13", endTime: "2013-2-16" }, ]; var dateFormat = d3.time.format("%Y-%m-%d"); var timeScale = d3.time.scale() .domain([d3.min(taskArray, function(d) {return dateFormat.parse(d.startTime);}), d3.max(taskArray, function(d) {return dateFormat.parse(d.endTime);})]) .range([0,w-150]); var categories = new Array(); for (var i = 0; i < taskArray.length; i++){ categories.push(taskArray[i].type); } var catsUnfiltered = categories; //for vert labels categories = checkUnique(categories); makeGant(taskArray, w, h); var title = svg.append("text") .text("Gantt Chart Process") .attr("x", w/2) .attr("y", 25) .attr("text-anchor", "middle") .attr("font-size", 18) .attr("fill", "#009FFC"); function makeGant(tasks, pageWidth, pageHeight){ var barHeight = 20; var gap = barHeight + 4; var topPadding = 75; var sidePadding = 75; var colorScale = d3.scale.linear() .domain([0, categories.length]) .range(["#00B9FA", "#F95002"]) .interpolate(d3.interpolateHcl); makeGrid(sidePadding, topPadding, pageWidth, pageHeight); drawRects(tasks, gap, topPadding, sidePadding, barHeight, colorScale, pageWidth, pageHeight); vertLabels(gap, topPadding, sidePadding, barHeight, colorScale); } function drawRects(theArray, theGap, theTopPad, theSidePad, theBarHeight, theColorScale, w, h){ var bigRects = svg.append("g") .selectAll("rect") .data(theArray) .enter() .append("rect") .attr("x", 0) .attr("y", function(d, i){ return i*theGap + theTopPad - 2; }) .attr("width", function(d){ return w-theSidePad/2; }) .attr("height", theGap) .attr("stroke", "none") .attr("fill", function(d){ for (var i = 0; i < categories.length; i++){ if (d.type == categories[i]){ return d3.rgb(theColorScale(i)); } } }) .attr("opacity", 0.2); var rectangles = svg.append('g') .selectAll("rect") .data(theArray) .enter(); var innerRects = rectangles.append("rect") .attr("rx", 3) .attr("ry", 3) .attr("x", function(d){ return timeScale(dateFormat.parse(d.startTime)) + theSidePad; }) .attr("y", function(d, i){ return i*theGap + theTopPad; }) .attr("width", function(d){ return (timeScale(dateFormat.parse(d.endTime))-timeScale(dateFormat.parse(d.startTime))); }) .attr("height", theBarHeight) .attr("stroke", "none") .attr("fill", function(d){ for (var i = 0; i < categories.length; i++){ if (d.type == categories[i]){ return d3.rgb(theColorScale(i)); } } }) var rectText = rectangles.append("text") .text(function(d){ return d.task; }) .attr("x", function(d){ .........完整代码请登录后点击上方下载按钮下载查看
网友评论0