Simple Line Chart Using D3 js with Animation, Value points and Tooltip

A Simple Line Chart Using D3 js with Animation, Value points and Tooltip

Inspired from great visualizations of data using d3js.org, a simple line chart using d3js with animation, value points and tooltip.



<!DOCTYPE html>
<html>
<head>
<style>
svg{
 background-color : none;
}
.axis{
 stroke : #0000001;
 fill : silver;
}
#tooltip{
 position : absolute;
 border : 1px solid #000;
 background-color : #ddd;
 padding : 5px;
 padding-left: 15px;
 padding-right : 15px;
 border-radius : 5px;
}


</style>
<script src="https://cdn.jsdelivr.net/d3js/3.4.12/d3.min.js" type="text/javascript"></script>
</head>
<body>
<svg id="my-line-chart" width="600" height="600"></svg>
<div id="tooltip"></div>
<script>
 drawLineChart();
 function drawLineChart(){

  var chartArea=d3.select('#my-line-chart');
  var lineData=[ { x: 0, y: 5},{ x: 20, y: 50 },{ x: 30, y: 45 },{ x: 40, y: 100 },{ x: 50, y: 65 },
    { x: 60, y: 75 },{ x: 70, y: 15 },{ x: 80, y: 85 },{ x: 90, y: 30 },{ x: 100, y: 0 }];

  //setting scale functions
  var xScale=d3.scale.linear().range([0,500]).domain(d3.extent(lineData, function(d){ return d.x;}));
  var yScale=d3.scale.linear().range([500,0]).domain(d3.extent(lineData, function(d){ return d.y;}));
  
  //setting axis functions
  xAxis = d3.svg.axis().scale(xScale).tickSize(2);
  yAxis = d3.svg.axis().scale(yScale).tickSize(2).ticks(20).orient("left");
  
  //drawing axes
  chartArea.append('g').attr('class','x axis').call(xAxis).attr('transform', 'translate(30,510)');
  chartArea.append('g').attr('class','y axis').call(yAxis).attr('transform', 'translate(30,10)');
  
  //line path generator function
  var linePathGenerator = d3.svg.line().x( function(d){
   return xScale(d.x);
  }).y(function(d){
   return yScale(d.y);
  });
  
  //drawing line 
  var linePath=chartArea.append('path').datum(lineData)
  .attr({
   'd' : linePathGenerator,
   'fill': 'none',
   'transform': 'translate(30,10)',
   'stroke': 'rgb(9, 112, 184)',
   'stroke-width' : 3
  });
  
  // animation 
  var pathLength = linePath.node().getTotalLength();
  linePath.attr({
   "stroke-dasharray" : pathLength  + " " + pathLength,
   "stroke-dashoffset" : pathLength
  }).transition().duration(3000).ease("linear")
  .attr("stroke-dashoffset", 0);
   
   //points
  var circlePoints=chartArea.selectAll('circle')
  .data(lineData)
  .enter()
  .append('circle')
  .attr({cx :  function(d){
   return xScale(d.x)+30;
  }, cy : function (d){
   return yScale(d.y)+10;
  }, r :8,
  fill : 'silver' 
  });
  
  // tooltip
  var div= d3.select('#tooltip');
  circlePoints.on("mouseover", function(d) {      
   div.transition()        
    .duration(200)      
                .style("opacity", .9);      
            div.html(d.y)  
               .style("left", (d3.event.pageX) + "px")     
               .style("top", (d3.event.pageY - 28) + "px");    
        }).on("mouseout", function(d) {       
            div.transition()        
                .duration(500)      
                .style("opacity", 0);   
        });
}
</script>
</body>
</html>

52 comments:

  1. Nice post you have done here. please check out this site Hotmail contact telephone number UK

    ReplyDelete
  2. I would like to thank you for the efforts you have made in writing this article.
    baixar facebook
    baixar whatsapp
    unblocked games

    ReplyDelete
  3. Thank you for that information you article
    i like play games games for kids friv online and play game friv games and games 2 girls ! have fun!

    ReplyDelete
  4. This article is really fantastic and thanks for sharing the valuable post.
    Signature:
    download free descargar whatsapp and descargar whatsapp gratis , baixar whatsapp gratis

    ReplyDelete
  5. Total Video Converter for Mac Total Video Converter for Mac is your best video converter for Mac to convert all videos to iPad, iPhone... Best all-in-one Video Converter for Mac.

    ReplyDelete
  6. I was working and suddenly I visits your site frequently and recommended it to me to read also. The writing style is superior and the content is relevant. Thanks for the insight you provide the readers!
    Signature:
    facebook entrar iniciar sesion gratis - Sitio Oficial iniciado sesión en Facebook lengua española. facebook entrar rápido, facebook iniciar sesion en tu cuenta de entrar facebook

    ReplyDelete
  7. Thanks for sharing this quality information with us. I really enjoyed reading.
    Signature:
    Versión en facebook en español descargar a los países hablan Español: facebook entrar direto agora , facebook en español descargar , facebook entrar direto agora

    ReplyDelete
  8. I would like more information about this, because it is very nice., Thanks for sharing.
    Signature:
    The place to play all unblocked games online. Here you can find every blocked games such as: unblockedgames , unblocked games happy , unblocked games 77 , gmod

    ReplyDelete
  9. The article you have shared here very awesome. I really like and appreciated your work. I read deeply your article, the points you have mentioned in this article are useful
    kids games online , friv 2 , jogos do friv , juegosjuegos.com , juegos de matar zombbies
    , juegos de un show mas

    ReplyDelete
  10. Thanks for sharing this quality information with us. I really enjoyed reading. I think I need it.
    Games for girls|
    Kizi 1|
    Kizi 2

    ReplyDelete
  11. Thanks for sharing the information. It is very useful for my future. keep sharing
    Kizi 123|
    Friv 234|
    Friv

    ReplyDelete
  12. Thanks for your great article friend, i get new information, new ideas to do somethings, i hope you will share again, i keep waiting for next post, thanks.
    Friv 4
    Kizi 3

    ReplyDelete
  13. I found a lot of information here to create this actually best for all newbie here. Thank you for this information.
    Signature:
    i like play games happy wheels online and play happy wheels 2 games and Download retrica online includes more than eighty different filters with many different styles and include retrica indir , and zombie tsunami is the ideal game for anyone who loves the running game genre

    ReplyDelete
  14. You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
    Thanks you for sharing!
    Yepi
    Kizi
    Friv
    Friv Games

    ReplyDelete
  15. You are stressed and tired, you need entertainment. Refer to our website. hope you get the most comfortable.
    Kizi 4

    ReplyDelete
  16. you'd have time to look these kids active. Please visit our website and let us play the game interesting.
    Thanks for sharing !
    Friv 10
    Kizi 10
    Yepi 2

    ReplyDelete
  17. This is something special! never seen this before! thank you
    Friv

    ReplyDelete
  18. thank you for your interest in welcome visit
    Friv 4

    ReplyDelete
  19. You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
    Thanks for sharing !
    Friv 5
    Kizi 1
    Yepi 3

    ReplyDelete
  20. New attractive game . I will play it every day
    Friv|
    Friv 2

    ReplyDelete
  21. When you're tired, you want to relax after a stressful working hours, you need to have time to take care of the kids active.
    Please visit our website and play exciting flash games.Thanks you for sharing!
    Friv 4

    ReplyDelete
  22. Play the Best Free Games! We've picked out the racing games, cooking games, candy crush, games shooting, fashion games, ...
    Thanks for sharing !
    Kizi 200
    Friv 1
    Y88
    Y9

    ReplyDelete
  23. You want to relax after a stressful working hours. Refer to our website. Hope you get the most comfort.
    Thanks for sharing !
    GirlsGoGames
    Kizi 100
    Frvi

    ReplyDelete
  24. You need to have time to take care of the kids active. Please visit our website and play exciting flash games.
    Thanks you for sharing!
    Games2Girls
    Kizi 1000
    Frin
    Friv 3

    ReplyDelete
  25. You want to relax after a stressful working hours. Refer to our website. Hope you get the most comfort.
    Thanks for sharing !Y8 arcade

    ReplyDelete
  26. Life after hours of work stress and fatigue is the space I really love. I can play games, take photos or simply surfing facebook, chatting with friends. It made me feel so comfortable and pleasan
    facebook baixar , whatsapp baixar , square quick

    ReplyDelete
  27. Just remember your stab at this business, should you decide to end it, is not a failure but a training ground. You will take with you what has worked and learned from what has not in order to make the next business idea a success!
    baixar facebook gratis , baixar whatsapp gratis , return man , subway surfers game , download hola launcher , pokemon go game , minecraft

    ReplyDelete
  28. The blog or and best that is extremely useful to keep I can share the ideas. Age Of War 2
    Big Farm | Slitherio | Tank Trouble
    Of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    Happy Wheels | Goodgeme Empire | Slither.io

    ReplyDelete
  29. Great post! I am actually getting ready to across this information, is very helpful my friend
    Cookie Clicker
    Cookie Clicker play
    Cookie Clicker game
    Cookie Clicker online

    ReplyDelete
  30. How often do you play games on free time or after hours of work stress? I usually come to the game as a way to balance life is full of pressure. And I would recommend to everyone that I loved the game

    strike force heroes 2, strike force heroes 2 game, strike force heroes 3 , strike force heroes 2

    ReplyDelete
  31. I used to play games after long day of work tired or bored. And I would recommend to everyone a very interesting game and attractive for both color and sound
    run 2 , run 2 game, run 2 unblocked

    ReplyDelete
  32. How often do you play games on free time or after hours of work stress? I usually come to the game as a way to balance life is full of pressure. And I would recommend to everyone that I loved the game
    strike force heroes 2, strike force heroes 2 game, strike force heroes 3 , strike force heroes 2 game

    ReplyDelete
  33. Thanks for sharing this good stuff! Keep up the great work, we look forward to reading more from you in the future!
    Friv 100

    ReplyDelete
  34. I wanted to thank you for this excellent read!! I definitely loved every little bit of it.Cheers for the info!!!! & This is the perfect blog for anyone who wants to know about this topic
    return man 2 game , return man 2 ,return man

    ReplyDelete
  35. In your article, points caught my attention the most is how your prose, to give me a deep impression. Wish you would write more. good luck!
    subway surfers game , subway surfer , subway surfers

    ReplyDelete
  36. Sarkari Recruitment is one of the biggest Indian Job Site so here you will getPPSC Jobs 2017 so

    ReplyDelete
  37. How often do you play games on free time or after hours of work stress? I usually come to the game as a way to
    manabadi ts ssc supplementary results 2017
    balance life is full of pressure. And I would recommend to everyone that I loved the game
    strike force heroes 2, strike force heroes 2 game, strike force heroes 3 , strike force heroes 2 game

    ReplyDelete
  38. Life becomes more interesting and wonderful when you share your memorable moments with friends and family through unique photographs. You can create your own unique style impressed with image editing software. And after hours of work stress you can also

    tank trouble, tank trouble 2, tank trouble games, tank trouble

    ReplyDelete
  39. Life becomes more interesting and wonderful when you share your memorable moments with friends and family through unique photographs. You can create your own unique style impressed with image editing software. And after hours of work stress you can also
    dr driving, dr driving baixar ,download dr driving, dr driving

    ReplyDelete
  40. You need to have time to take care of the active. It in fact was a amusement account it. Look advanced to far added agreeable from you.
    banana kong ,
    banana kong baixar ,
    baixar banana kong ,
    download banana kong ,
    banana kong

    ReplyDelete
  41. You need to have time to take care of the active. It in fact was a amusement account it. Look advanced to far added agreeable from you.

    entrar hotmail agora , hotmail entrar, entrar hotmail , entrar no hotmail

    ReplyDelete
  42. Mostly people have all the same things when they are writing academic task or any other writing, especially light music most people like during the writing.

    subway surf , baixar subway surf, subway surf download , download subway surf

    ReplyDelete
  43. Mostly people have all the same things when they are writing academic task or any other writing, especially light music most people like during the writing.
    dream league soccer download , dream league soccer apk , download dream league soccer , dream league soccer

    ReplyDelete
  44. Work anybody but take some time to relax every day work efficiency will be higher than that. Invite you to visit our website

    Friv 1
    GirlsGoGames
    Friv 4 School

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...