Tooltip behavior on line chart with Date as the x-axis











up vote
0
down vote

favorite












I'm trying to create a line chart with a tooltip where the x-axis is a date.



I would like the line and tooltip to change after it is halfway (or similar) to the next tick on the x-axis.



Mainly I would like the behavior to be the same as this bl.ock: http://bl.ocks.org/wdickerson/64535aff478e8a9fd9d9facccfef8929



You can view the behavior I currently have on my bl.ock: https://bl.ocks.org/JulienAssouline/574a52ee2034bcdc1e56ed926f36dd52



It mostly works but the data only changes after it passes to the September month and it never reaches the October month.



I have tried to adapt my code to the bl.ock. The problem is the bl.ock displayed is using years and I am using a date format which seems to be my main problem.



Here is the main part of the code:



 var tipBox = svg.append("rect")
.attr("width", width)
.attr("height", height)
.attr("opacity", 0)
.on("mousemove", drawTooltip)
.on("mouseout", removeTooltip)

function removeTooltip() {
if (tooltip) tooltip.style('display', 'none');
if (tooltipLine) tooltipLine.attr('stroke', 'none');
}

function drawTooltip(){
const line_hover = xScale.invert(d3.mouse(this)[0]);
// console.log(d3.mouse(this)[0])
console.log(xScale.invert(d3.mouse(this)[0]).getMonth())

console.log(Math.floor(xScale.invert(d3.mouse(this)[0])))

const date_hover = xScale.invert(d3.mouse(this)[0]).getMonth()



// yScale.invert(pos.y)

tooltipLine.attr("stroke", "grey")
.attr("x1", xScale(line_hover))
.attr("x2", xScale(line_hover))
.attr("y1", 0)
.attr("y2", height)
.attr("class", "line_hover")
.style('stroke-width', 1)

tooltip.html(date_hover)
.style("position", "absolute")
.style("background-color", "lightgrey")
.style('display', 'block')
.style('left', d3.event.pageX - 100+ "px")
.style('top', d3.event.pageY - 20+"px")
.selectAll()
.data(dataNest).enter()
.append('div')
.style('color', "black")
.html(function(e){ return e.key + ': ' + e.values.find(function(h){ return (h.Date.getMonth() + 0.5) == (date_hover + 0.5) }).randNumCol})
}


You can again view all of the code on my bl.ock: https://bl.ocks.org/JulienAssouline/574a52ee2034bcdc1e56ed926f36dd52










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to create a line chart with a tooltip where the x-axis is a date.



    I would like the line and tooltip to change after it is halfway (or similar) to the next tick on the x-axis.



    Mainly I would like the behavior to be the same as this bl.ock: http://bl.ocks.org/wdickerson/64535aff478e8a9fd9d9facccfef8929



    You can view the behavior I currently have on my bl.ock: https://bl.ocks.org/JulienAssouline/574a52ee2034bcdc1e56ed926f36dd52



    It mostly works but the data only changes after it passes to the September month and it never reaches the October month.



    I have tried to adapt my code to the bl.ock. The problem is the bl.ock displayed is using years and I am using a date format which seems to be my main problem.



    Here is the main part of the code:



     var tipBox = svg.append("rect")
    .attr("width", width)
    .attr("height", height)
    .attr("opacity", 0)
    .on("mousemove", drawTooltip)
    .on("mouseout", removeTooltip)

    function removeTooltip() {
    if (tooltip) tooltip.style('display', 'none');
    if (tooltipLine) tooltipLine.attr('stroke', 'none');
    }

    function drawTooltip(){
    const line_hover = xScale.invert(d3.mouse(this)[0]);
    // console.log(d3.mouse(this)[0])
    console.log(xScale.invert(d3.mouse(this)[0]).getMonth())

    console.log(Math.floor(xScale.invert(d3.mouse(this)[0])))

    const date_hover = xScale.invert(d3.mouse(this)[0]).getMonth()



    // yScale.invert(pos.y)

    tooltipLine.attr("stroke", "grey")
    .attr("x1", xScale(line_hover))
    .attr("x2", xScale(line_hover))
    .attr("y1", 0)
    .attr("y2", height)
    .attr("class", "line_hover")
    .style('stroke-width', 1)

    tooltip.html(date_hover)
    .style("position", "absolute")
    .style("background-color", "lightgrey")
    .style('display', 'block')
    .style('left', d3.event.pageX - 100+ "px")
    .style('top', d3.event.pageY - 20+"px")
    .selectAll()
    .data(dataNest).enter()
    .append('div')
    .style('color', "black")
    .html(function(e){ return e.key + ': ' + e.values.find(function(h){ return (h.Date.getMonth() + 0.5) == (date_hover + 0.5) }).randNumCol})
    }


    You can again view all of the code on my bl.ock: https://bl.ocks.org/JulienAssouline/574a52ee2034bcdc1e56ed926f36dd52










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to create a line chart with a tooltip where the x-axis is a date.



      I would like the line and tooltip to change after it is halfway (or similar) to the next tick on the x-axis.



      Mainly I would like the behavior to be the same as this bl.ock: http://bl.ocks.org/wdickerson/64535aff478e8a9fd9d9facccfef8929



      You can view the behavior I currently have on my bl.ock: https://bl.ocks.org/JulienAssouline/574a52ee2034bcdc1e56ed926f36dd52



      It mostly works but the data only changes after it passes to the September month and it never reaches the October month.



      I have tried to adapt my code to the bl.ock. The problem is the bl.ock displayed is using years and I am using a date format which seems to be my main problem.



      Here is the main part of the code:



       var tipBox = svg.append("rect")
      .attr("width", width)
      .attr("height", height)
      .attr("opacity", 0)
      .on("mousemove", drawTooltip)
      .on("mouseout", removeTooltip)

      function removeTooltip() {
      if (tooltip) tooltip.style('display', 'none');
      if (tooltipLine) tooltipLine.attr('stroke', 'none');
      }

      function drawTooltip(){
      const line_hover = xScale.invert(d3.mouse(this)[0]);
      // console.log(d3.mouse(this)[0])
      console.log(xScale.invert(d3.mouse(this)[0]).getMonth())

      console.log(Math.floor(xScale.invert(d3.mouse(this)[0])))

      const date_hover = xScale.invert(d3.mouse(this)[0]).getMonth()



      // yScale.invert(pos.y)

      tooltipLine.attr("stroke", "grey")
      .attr("x1", xScale(line_hover))
      .attr("x2", xScale(line_hover))
      .attr("y1", 0)
      .attr("y2", height)
      .attr("class", "line_hover")
      .style('stroke-width', 1)

      tooltip.html(date_hover)
      .style("position", "absolute")
      .style("background-color", "lightgrey")
      .style('display', 'block')
      .style('left', d3.event.pageX - 100+ "px")
      .style('top', d3.event.pageY - 20+"px")
      .selectAll()
      .data(dataNest).enter()
      .append('div')
      .style('color', "black")
      .html(function(e){ return e.key + ': ' + e.values.find(function(h){ return (h.Date.getMonth() + 0.5) == (date_hover + 0.5) }).randNumCol})
      }


      You can again view all of the code on my bl.ock: https://bl.ocks.org/JulienAssouline/574a52ee2034bcdc1e56ed926f36dd52










      share|improve this question













      I'm trying to create a line chart with a tooltip where the x-axis is a date.



      I would like the line and tooltip to change after it is halfway (or similar) to the next tick on the x-axis.



      Mainly I would like the behavior to be the same as this bl.ock: http://bl.ocks.org/wdickerson/64535aff478e8a9fd9d9facccfef8929



      You can view the behavior I currently have on my bl.ock: https://bl.ocks.org/JulienAssouline/574a52ee2034bcdc1e56ed926f36dd52



      It mostly works but the data only changes after it passes to the September month and it never reaches the October month.



      I have tried to adapt my code to the bl.ock. The problem is the bl.ock displayed is using years and I am using a date format which seems to be my main problem.



      Here is the main part of the code:



       var tipBox = svg.append("rect")
      .attr("width", width)
      .attr("height", height)
      .attr("opacity", 0)
      .on("mousemove", drawTooltip)
      .on("mouseout", removeTooltip)

      function removeTooltip() {
      if (tooltip) tooltip.style('display', 'none');
      if (tooltipLine) tooltipLine.attr('stroke', 'none');
      }

      function drawTooltip(){
      const line_hover = xScale.invert(d3.mouse(this)[0]);
      // console.log(d3.mouse(this)[0])
      console.log(xScale.invert(d3.mouse(this)[0]).getMonth())

      console.log(Math.floor(xScale.invert(d3.mouse(this)[0])))

      const date_hover = xScale.invert(d3.mouse(this)[0]).getMonth()



      // yScale.invert(pos.y)

      tooltipLine.attr("stroke", "grey")
      .attr("x1", xScale(line_hover))
      .attr("x2", xScale(line_hover))
      .attr("y1", 0)
      .attr("y2", height)
      .attr("class", "line_hover")
      .style('stroke-width', 1)

      tooltip.html(date_hover)
      .style("position", "absolute")
      .style("background-color", "lightgrey")
      .style('display', 'block')
      .style('left', d3.event.pageX - 100+ "px")
      .style('top', d3.event.pageY - 20+"px")
      .selectAll()
      .data(dataNest).enter()
      .append('div')
      .style('color', "black")
      .html(function(e){ return e.key + ': ' + e.values.find(function(h){ return (h.Date.getMonth() + 0.5) == (date_hover + 0.5) }).randNumCol})
      }


      You can again view all of the code on my bl.ock: https://bl.ocks.org/JulienAssouline/574a52ee2034bcdc1e56ed926f36dd52







      javascript d3.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 1:07









      Julien

      325416




      325416
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          GetMonth will always give the month. Get the date and display based on the date. Not ideal, but works.
          Example here






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254824%2ftooltip-behavior-on-line-chart-with-date-as-the-x-axis%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            GetMonth will always give the month. Get the date and display based on the date. Not ideal, but works.
            Example here






            share|improve this answer

























              up vote
              1
              down vote



              accepted










              GetMonth will always give the month. Get the date and display based on the date. Not ideal, but works.
              Example here






              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                GetMonth will always give the month. Get the date and display based on the date. Not ideal, but works.
                Example here






                share|improve this answer












                GetMonth will always give the month. Get the date and display based on the date. Not ideal, but works.
                Example here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 6:05









                Umesh Maharshi

                1,040410




                1,040410






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254824%2ftooltip-behavior-on-line-chart-with-date-as-the-x-axis%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)