javascript - Appending multiple options to html in d3 -


i have timeline in d3 using data posts in wordpress formatted in json. items on timeline have tooltips display few things, including start , end times item. both timeline , tooltips working fine except have 2 conditions change what's displayed in tooltip , can 1 work @ time.

the 2 conditions are:

  1. if start date same end date, display start date. otherwise display startdate + " " + enddate.
  2. the date might displayed year, month , year, or day, month , year, depending on input post.

so have 3 separate date formats (all working fine):

var displaydate = d3.time.format("%d %b %y"); var displaymonthyear = d3.time.format("%b %y"); var displayyear = d3.time.format("%y"); 

and can append html tooltip either first condition:

var tip = d3.tip()   .attr('class', 'd3-tip')   .offset(function(d) { if (xtimeline(d.enddate)  > 800) { return [-10, 8] } else { return [-10, -8]  } })   .html(function(d) {     if ((xtimeline(d.enddate) - xtimeline(d.startdate) == 0)) {       return d.title + "<br/><p>" + displaydate(d.startdate) + "</p>" + d.content; }     else {        return d.title + "<br/><p>" + displaydate(d.startdate) + " " + displaydate(d.enddate) + "</p>" + d.content; }   }); 

or second:

var tip = d3.tip()   .attr('class', 'd3-tip')   .offset(function(d) { if (xtimeline(d.enddate)  > 800) { return [-10, 8] } else { return [-10, -8]  } })   .html(function(d) { if (d.dateformat == "year only") {   return d.title + "<br/><p>" + displayyear(d.startdate) + "</p>" + d.content;    }   else if (d.dateformat == "month , year") { return d.title + "<br/><p>" + displaymonthyear(d.startdate) + "</p>" + d.content;    }   else { return d.title + "<br/><p>" + displaydate(d.startdate) + "</p>" + d.content;    }   }); 

but lack skills combine them. i've tried making second 1 var or function, can't seem make work.

i suspect straightforward knowledge of javascript, that's not me @ stage.

any appreciated.

@tgerard: deleted previous answer after re-reading question , realizing had not provided asking for. think following code looking for:

var tip = d3.tip()   .attr('class', 'd3-tip')   .offset(function(d) {       if (xtimeline(d.enddate)  > 800) {         return [-10, 8];       } else {         return [-10, -8];       }   })   .html(function(d) {     var tooltipcontent = "";     if ((xtimeline(d.enddate) - xtimeline(d.startdate) == 0)) {       tooltipcontent = gettooltipcontent(d, true);     } else {       tooltipcontent = gettooltipcontent(d, false);     }     return tooltipcontent;   });  function gettooltipcontent(d, samedates) {   var tooltipcontent = d.title + "<br/><p>";   if (d.dateformat == "year only") {     tooltipcontent +=  (samedates)       ? displayyear(d.startdate)       : displayyear(d.startdate) + " " + displayyear(d.enddate);   } else if (d.dateformat == "month , year") {     tooltipcontent +=  (samedates)       ? displaymonthyear(d.startdate)       : displaymonthyear(d.startdate) + " " + displaymonthyear(d.enddate);   } else {     tooltipcontent +=  (samedates)       ? displaydate(d.startdate)       : displaydate(d.startdate) + " " + displaydate(d.enddate);   }   tooltipcontent += "</p>" + d.content;   return tooltipcontent; } 

within gettooltipcontent function, check 3 conditions date format:

  1. year only
  2. month , year
  3. something else (full date)

then each 1 of conditions, use ternary operator decide append tooltipcontent string. so, if 'samedates' evaluates true, part after '?' gets appended. if 'samedates' evaluates false, part after ':' gets appended.

good luck project! let me know if have questions code above.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -