首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在d3甘特图中绘制一条表示当前日期的垂直线

在d3甘特图中绘制一条表示当前日期的垂直线
EN

Stack Overflow用户
提问于 2014-10-17 14:02:56
回答 1查看 17.8K关注 0票数 8

嗨,这对你们来说可能很简单,我只需要在我的d3甘特图中画一条代表当前日期的垂直线。我已经计算出了y的值,我只是在x轴的值上遇到了问题,因为我在x轴上使用了time.scale。我将粘贴绘制甘特图的代码,绘制垂直线的部分位于最底部

代码语言:javascript
复制
initTimeDomain(tasks);
initAxis();

var numFormat = d3.format(",.0f");
var dateFormat = d3.time.format("%Y-%b-%d");
var parseDate = d3.time.format("%Y-%b-%d").parse;

var svg = d3.select("#gantt_chart")
.append("svg")
.attr("class", "chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
    .attr("class", "gantt-chart")
.attr("width", width + margin.left + margin.right)
.attr("height", (height + margin.top + margin.bottom) / tasks[tasks.length - 1].endDate)
.attr("transform", "translate(" + margin.left + ", " + margin.top + ")");


 //this is the x-axis
 svg.append("g")
 .attr("class", "x axis")
 .attr("transform", "translate(0, " + (height - margin.top - margin.bottom) + ")")
 .transition()
 .call(xAxis)
 .selectAll("text")
    .style("text-anchor","end")
    //.attr("dx", 35)
    //.attr("dy", 5);
    .attr("dx", "-.8em")
    .attr("dy", -10)
    .attr("transform", function(d){return "rotate(-90)"});


 //this is the y-axis
 svg.append("g").attr("class", "y axis").transition().call(yAxis);




//this is the actual gantt
svg.selectAll(".chart")
 .data(tasks, keyFunction).enter()
 .append("rect")
 .attr("rx", 0)
  .attr("ry", 0)
 .attr("class", function(d){ 
 if(d.status > 70)
 { 
    return "bar-failed";
 }
 else if (d.status >= 51 && d.status <= 70){
    return "bar-killed";
 }
 else{
    return "bar-running";
 }
 }) 
 .attr("y", 0)
 .attr("transform", rectTransform)
 .attr("height", function(d) { return y.rangeBand(); })
 .attr("width", function(d) { 
     return (x(d.endDate) - x(d.startDate)); 
     })
.on("mouseover", function(d){
        div.transition()
            .duration(200)
            .style("opacity", .9);
        div.html("HandlerID: " + d.taskName  + "<br>" +  "startDate: " + dateFormat(d.startDate) + "<br/>" + 
                "endDate: " + dateFormat(d.endDate) + "<br/>" + "% Insertions: " + d3.round(d.status,2) + "%" + "<br/>" +
                "Insertions: " + numFormat(d.insertions) )                       
            .style("left", (d3.event.pageX) + "px")
            .style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout",function(d){
        div.transition()
            .duration(500)
            .style("opacity", 0);
});


var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = yyyy+'-'+mm+'-'+dd;
today = parseDate(today);
//document.write(today);

svg.append("line")
.attr("x1", today)
.attr("y1", 0)
.attr("x2", today)
.attr("y2", height - margin.top - margin.bottom)
.style("stroke-width", 2)
.style("stroke", "red")
.style("fill", "none");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-17 14:26:33

今天拿到日期就行了。不需要获取其日期、月份和年份,因为它将返回一个字符串。您所要做的就是将日期放在您的x变量中,以便与其属性域一起使用

代码语言:javascript
复制
var today = new Date();
var dd = today.getDate();    //<<===== no need
var mm = today.getMonth()+1; //January is 0!   //<<===== no need
var yyyy = today.getFullYear();  //<<===== no need


svg.append("line")
.attr("x1", x(today))  //<<== change your code here
.attr("y1", 0)
.attr("x2", x(today))  //<<== and here
.attr("y2", height - margin.top - margin.bottom)
.style("stroke-width", 2)
.style("stroke", "red")
.style("fill", "none");
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26418777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档