메뉴 닫기

Converting Between Dates and Pixels

PointConverter
Steelray Gantt Chart

Introduction

This article teaches you how to write a simple class to convert dates to values on an X-axis and vice versa. This is one of the most fundamental calculations in rendering a Gantt chart for project management software. We use this class in our viewer for Microsoft Project (see image above), but it could be applied in many other ways as well.

Background

It will help to understand the basic math behind the calculations. First, a couple of conventions. The algorithm will work for any kind of units, but we’ll call our units pixels in this article. Second, we’ve picked days as our unit of time measure, but depending on your needs, other units (seconds, days, etc.) would work just as well.
To convert a date to a point on an X axis, we need to know a few things:

  • D: the date to convert
  • S: the date at the start (leftmost point) of the X axis
  • W: width (in pixels) of a day

With these three pieces of information, the X value can be calculated with a simple formula:

X = (D - S) * W

To convert a point to a date, we simply solve the above equation for D:

D = (X / W) + S

Using the code

The class will need to keep track of S and W, the start date and the day width from our formulas above. We also keep a constant to convert milliseconds to days.

private Date start = null;
private double dayWidth = 0;
private double lengthOfDayInMs = 86400000f;

To instantiate and initialize the class, provide a constructor with S and W:

// Constructor
// start - start date
// width - how wide (in units) a day is.
public PointConverter(Date start, float width) {
    this.dayWidth = width;
    this.start = start;
}

The conversion functions use our formulas from above (with some conversion to/from days to make sure the units are uniform).

// dateToPoint - convert Date d to an X value
public float dateToPoint(Date d) {
    // Compute how many days since the start
    long elapsed = d.getTime() - start.getTime();
    double daysPast = ((float)(elapsed / lengthOfDayInMs));
    // return the X value        
    return ((float)(daysPast * dayWidth));
}
// pointToDate - convert an X value to a date
public Date pointToDate(float x) {
    // The formula is the reverse of dateToPoint
    long elapsed = start.getTime() + ((long)((x / dayWidth) * lengthOfDayInMs));
    return new Date(elapsed);
}

This class works fine if the starting date or scale never changed, but in a Gantt chart, those two values change quite frequently. To accomodate that reality, we provide an update method to allow the start date and day width to change:

// update - pass new values in for start and width
public void update(Date start, float width) {
    this.start = start;
    dayWidth = width;
}

226 Comments

  1. one g

    I know this if off topic but I’m looking into starting my own blog and was curious what all is required to get
    setup? I’m assuming having a blog like yours would cost a pretty penny?

    I’m not very internet smart so I’m not 100% certain. Any recommendations
    or advice would be greatly appreciated. Appreciate it

  2. http://tinyurl.com/

    Great goods from you, man. I’ve understand your stuff previous to and
    you are just too wonderful. I really like what you’ve acquired here, really like what you’re
    stating and the way in which you say it. You make it entertaining and
    you still care for to keep it sensible. I can not wait to read far more from you.
    This is really a great site.

  3. September Beaufait

    I just want to tell you that I’m new to blogs and definitely savored you’re web page. More than likely I’m going to bookmark your blog . You surely have good well written articles. Thank you for revealing your blog site.

  4. John Deere Technical Manuals

    There are some interesting points with time in this article but I do not know if I see every one of them center to heart. There is some validity but I most certainly will take hold opinion until I check into it further. Very good post , thanks and now we want a lot more! Included with FeedBurner as well

  5. go to these guys

    After research a few of the blog posts on your website now, as well as I absolutely like your means of blog writing. I bookmarked it to my bookmark website list and will certainly be examining back soon. Pls take a look at my web site also and let me recognize what you think.

  6. cbd for sale in lawton ok

    After research a few of the post on your web site currently, as well as I genuinely like your means of blogging. I bookmarked it to my book mark site list and will be examining back soon. Pls take a look at my website also and let me understand what you believe.

  7. John Deere Technical Manuals

    I have been surfing on-line more than three hours lately, but I never discovered any interesting article like yours. It is lovely value sufficient for me. In my opinion, if all webmasters and bloggers made just right content material as you did, the net can be a lot more useful than ever before.

  8. dose of cbd chews for dogs with anxiety

    The following time I read a blog, I hope that it does not dissatisfy me as high as this. I mean, I understand it was my option to check out, yet I really assumed youd have something interesting to say. All I hear is a lot of whining regarding something that you might deal with if you werent as well active looking for attention.

  9. benefits of nectar bee cbd tincture

    Can I simply state what an alleviation to locate a person that really knows what theyre speaking about on the net. You most definitely understand how to bring a concern to light and make it important. More people require to read this and also recognize this side of the story. I cant believe youre not extra preferred due to the fact that you certainly have the present.

  10. Continue Reading

    After research a few of the post on your web site currently, and I really like your means of blogging. I bookmarked it to my book mark web site checklist and will be checking back quickly. Pls look into my web site as well and let me know what you think.

  11. great post to read

    I was really pleased to discover this web-site. I wanted to thanks for your time for this wonderful read!! I most definitely enjoying every little of it as well as I have you bookmarked to have a look at new stuff you post.

  12. read more

    An outstanding share! I have just forwarded this onto a friend who had been conducting a little research on this. And he actually ordered me breakfast due to the fact that I stumbled upon it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanx for spending the time to discuss this topic here on your blog.

  13. mandiriqq

    I’m impressed, I must say. Rarely do I come across a blog that’s both educative and interesting, and let me tell you, you have hit the nail on the head. The problem is an issue that too few men and women are speaking intelligently about. I am very happy I found this in my search for something concerning this.

  14. vipmandiriqq

    Having read this I believed it was rather enlightening. I appreciate you finding the time and energy to put this article together. I once again find myself personally spending a lot of time both reading and leaving comments. But so what, it was still worthwhile!

  15. pokerpelangi

    I’m very happy to uncover this web site. I want to to thank you for your time just for this fantastic read!! I definitely appreciated every little bit of it and I have you book marked to look at new things in your blog.

  16. dewajudiqq

    After looking into a number of the articles on your site, I seriously appreciate your technique of writing a blog. I added it to my bookmark site list and will be checking back soon. Take a look at my web site as well and let me know how you feel.

  17. togel wap hk

    I absolutely love your website.. Pleasant colors & theme. Did you create this website yourself? Please reply back as I’m planning to create my very own blog and would love to find out where you got this from or exactly what the theme is called. Thank you!

  18. dingdong togel

    I was more than happy to uncover this great site. I want to to thank you for your time for this particularly wonderful read!! I definitely appreciated every part of it and i also have you book-marked to look at new information on your blog.

  19. Sbobet777 Indonesia

    Having read this I thought it was rather enlightening. I appreciate you finding the time and energy to put this information together. I once again find myself spending a significant amount of time both reading and posting comments. But so what, it was still worth it!

  20. agen bet88

    This is the right web site for anybody who really wants to find out about this topic. You understand a whole lot its almost hard to argue with you (not that I personally will need to…HaHa). You certainly put a new spin on a subject that has been discussed for ages. Excellent stuff, just great!

  21. Judi online

    You are so interesting! I don’t suppose I have read through a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. many thanks for starting this up. This site is something that is required on the web, someone with a bit of originality!

  22. totobet hk

    Howdy, There’s no doubt that your web site could possibly be having internet browser compatibility issues. Whenever I take a look at your website in Safari, it looks fine however when opening in I.E., it’s got some overlapping issues. I just wanted to give you a quick heads up! Besides that, excellent blog!

  23. togel online indonesia

    You’re so awesome! I do not believe I’ve read something like that before. So great to find another person with original thoughts on this subject matter. Really.. thank you for starting this up. This website is one thing that’s needed on the internet, someone with some originality!

  24. trang lo de thethaobet

    I blog quite often and I really thank you for your information. This great article has really peaked my interest. I will take a note of your blog and keep checking for new information about once a week. I subscribed to your RSS feed too.

  25. joker123 download

    You’re so awesome! I do not suppose I have read through anything like this before. So nice to find someone with a few genuine thoughts on this subject. Really.. thank you for starting this up. This site is something that is needed on the internet, someone with a bit of originality!

  26. daftar joker123

    Next time I read a blog, Hopefully it won’t disappoint me just as much as this one. After all, I know it was my choice to read through, however I genuinely thought you’d have something interesting to say. All I hear is a bunch of complaining about something you could possibly fix if you weren’t too busy seeking attention.

  27. Agen Domino99 Online

    I’m impressed, I have to admit. Seldom do I come across a blog that’s both educative and interesting, and let me tell you, you’ve hit the nail on the head. The problem is something that too few men and women are speaking intelligently about. Now i’m very happy that I stumbled across this in my hunt for something relating to this.

  28. paragon poker

    I seriously love your blog.. Great colors & theme. Did you develop this amazing site yourself? Please reply back as I’m attempting to create my very own blog and want to know where you got this from or exactly what the theme is named. Thanks!

  29. mybestpoker

    I’d like to thank you for the efforts you have put in penning this site. I really hope to view the same high-grade content by you later on as well. In fact, your creative writing abilities has motivated me to get my very own blog now 😉

  30. my response

    I was really happy to discover this web-site. I wished to thanks for your time for this remarkable read!! I definitely enjoying every little of it and I have you bookmarked to check out brand-new things you post.

  31. rumus lightroom selebgram

    Right here is the right website for anyone who wants to understand this topic. You realize so much its almost tough to argue with you (not that I personally would want to…HaHa). You definitely put a new spin on a topic which has been written about for many years. Excellent stuff, just great!

  32. cover lagu

    Having read this I believed it was very informative. I appreciate you finding the time and energy to put this short article together. I once again find myself personally spending a significant amount of time both reading and leaving comments. But so what, it was still worth it!

  33. dingdong

    Having read this I believed it was really informative. I appreciate you spending some time and energy to put this information together. I once again find myself spending a significant amount of time both reading and posting comments. But so what, it was still worthwhile!

  34. Katalog Harga Promo

    An outstanding share! I’ve just forwarded this onto a coworker who had been conducting a little research on this. And he in fact ordered me dinner because I stumbled upon it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanks for spending the time to talk about this subject here on your internet site.

  35. rs primaya

    Good post. I learn something totally new and challenging on sites I stumbleupon everyday. It will always be useful to read through content from other writers and practice a little something from their web sites.

  36. situs poker

    Hi, I do think this is an excellent web site. I stumbledupon it 😉 I may come back once again since i have bookmarked it. Money and freedom is the greatest way to change, may you be rich and continue to help others.

  37. s1288

    I was very happy to discover this great site. I want to to thank you for your time due to this wonderful read!! I definitely really liked every little bit of it and i also have you bookmarked to see new information on your blog.

  38. s128 live

    Aw, this was an extremely nice post. Finding the time and actual effort to generate a superb article… but what can I say… I put things off a whole lot and never seem to get anything done.

  39. Agen Judi Bola

    Having read this I believed it was very enlightening. I appreciate you taking the time and energy to put this short article together. I once again find myself spending a lot of time both reading and commenting. But so what, it was still worthwhile!

  40. check

    When I originally commented I clicked the -Notify me when brand-new remarks are included- checkbox and now each time a comment is included I get 4 e-mails with the exact same comment. Exists any way you can eliminate me from that service? Many thanks!

  41. Poker Play Mobile

    You’re so interesting! I do not believe I’ve read through a single thing like that before. So nice to discover somebody with genuine thoughts on this issue. Really.. thanks for starting this up. This website is one thing that is needed on the web, someone with a little originality!

  42. sbobet88 casino

    After going over a number of the blog articles on your web site, I truly like your way of blogging. I saved as a favorite it to my bookmark webpage list and will be checking back in the near future. Please visit my web site too and let me know what you think.

  43. Sbobet777 Indonesia

    The very next time I read a blog, Hopefully it doesn’t fail me just as much as this one. I mean, I know it was my choice to read, however I truly thought you’d have something useful to talk about. All I hear is a bunch of whining about something you could fix if you were not too busy searching for attention.

  44. s1288

    Hi, I do believe this is an excellent website. I stumbledupon it 😉 I’m going to return yet again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people.

  45. agen bet88

    I’m impressed, I must say. Rarely do I encounter a blog that’s both educative and engaging, and let me tell you, you have hit the nail on the head. The problem is an issue that too few folks are speaking intelligently about. I am very happy that I found this during my hunt for something regarding this.

  46. sabung ayam online

    You are so interesting! I don’t suppose I’ve truly read something like that before. So nice to discover somebody with a few unique thoughts on this subject matter. Seriously.. many thanks for starting this up. This site is something that is needed on the web, someone with some originality!

  47. s128

    When I originally commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I recieve four emails with the same comment. There has to be a means you can remove me from that service? Thank you!

  48. s128

    When I originally left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and now whenever a comment is added I get 4 emails with the same comment. Is there a way you can remove me from that service? Thank you!

  49. Judi Bandarq

    I must thank you for the efforts you’ve put in writing this site. I am hoping to see the same high-grade content by you later on as well. In fact, your creative writing abilities has encouraged me to get my very own site now 😉

  50. Situs Judi Slot

    I would like to thank you for the efforts you’ve put in writing this blog. I am hoping to view the same high-grade content from you in the future as well. In fact, your creative writing abilities has encouraged me to get my own website now 😉

  51. Situs Judi Qiuqiu

    Oh my goodness! Incredible article dude! Thank you so much, However I am having problems with your RSS. I don’t understand why I am unable to subscribe to it. Is there anyone else having identical RSS problems? Anyone that knows the solution will you kindly respond? Thanks!!

  52. PKV Games

    I’m amazed, I must say. Rarely do I come across a blog that’s both equally educative and entertaining, and let me tell you, you’ve hit the nail on the head. The problem is something too few folks are speaking intelligently about. I’m very happy I found this during my search for something concerning this.

  53. senang domino

    Right here is the perfect site for anyone who hopes to find out about this topic. You realize a whole lot its almost tough to argue with you (not that I really would want to…HaHa). You certainly put a fresh spin on a subject that’s been written about for years. Wonderful stuff, just wonderful!

  54. king4d

    Can I simply just say what a comfort to discover an individual who truly knows what they are talking about over the internet. You definitely understand how to bring a problem to light and make it important. More and more people ought to check this out and understand this side of your story. It’s surprising you are not more popular given that you most certainly possess the gift.

  55. login totobet terbaru

    After I initially commented I seem to have clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I receive four emails with the same comment. Is there an easy method you are able to remove me from that service? Thank you!

  56. daftar totobet online

    I’m very happy to find this page. I need to to thank you for your time for this particularly fantastic read!! I definitely really liked every part of it and i also have you saved to fav to check out new things in your web site.

  57. judi qq online terpercaya

    After I initially left a comment I appear to have clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I recieve four emails with the exact same comment. Perhaps there is a means you are able to remove me from that service? Kudos!

  58. casino online

    Can I just say what a relief to uncover someone that really understands what they’re discussing online. You definitely know how to bring a problem to light and make it important. More people really need to look at this and understand this side of your story. I was surprised that you are not more popular because you definitely possess the gift.

  59. Kursus Bahasa Inggris

    May I simply just say what a comfort to find an individual who really understands what they’re talking about over the internet. You certainly realize how to bring a problem to light and make it important. More people really need to read this and understand this side of your story. I was surprised that you’re not more popular since you most certainly have the gift.

  60. site web

    After research study a few of the article on your web site now, as well as I absolutely like your way of blogging. I bookmarked it to my book mark site listing and also will certainly be examining back soon. Pls have a look at my website also and let me understand what you think.

  61. togel online terbaru

    Hello there! This article could not be written much better! Looking through this article reminds me of my previous roommate! He continually kept talking about this. I’ll send this information to him. Fairly certain he will have a great read. I appreciate you for sharing!

  62. dindong casino

    Howdy! This post could not be written much better! Looking through this article reminds me of my previous roommate! He constantly kept preaching about this. I am going to send this information to him. Fairly certain he will have a very good read. I appreciate you for sharing!

  63. s12888

    Next time I read a blog, Hopefully it does not disappoint me just as much as this one. After all, I know it was my choice to read, however I truly thought you’d have something useful to talk about. All I hear is a bunch of whining about something you could fix if you weren’t too busy seeking attention.

  64. kamera4d

    Hello there, I believe your blog might be having internet browser compatibility issues. When I look at your web site in Safari, it looks fine however when opening in IE, it has some overlapping issues. I merely wanted to provide you with a quick heads up! Besides that, wonderful blog!

  65. s128

    After going over a handful of the blog posts on your website, I really like your technique of blogging. I book marked it to my bookmark webpage list and will be checking back soon. Please visit my website as well and tell me your opinion.

  66. s128

    A motivating discussion is worth comment. I think that you need to publish more about this topic, it may not be a taboo subject but generally people don’t speak about these topics. To the next! All the best!!

  67. joker 123

    I’m excited to find this page. I want to to thank you for ones time for this wonderful read!! I definitely loved every bit of it and i also have you bookmarked to check out new information on your website.

  68. slotonline

    You are so awesome! I don’t suppose I have read through a single thing like this before. So great to find somebody with original thoughts on this issue. Seriously.. thanks for starting this up. This web site is something that’s needed on the internet, someone with a bit of originality!

  69. idnpoker88

    I blog quite often and I really appreciate your information. The article has truly peaked my interest. I’m going to bookmark your website and keep checking for new information about once per week. I subscribed to your RSS feed as well.

  70. judionline.com

    Having read this I believed it was extremely informative. I appreciate you spending some time and energy to put this informative article together. I once again find myself spending a lot of time both reading and leaving comments. But so what, it was still worthwhile!

  71. joker123 slot

    I truly love your website.. Great colors & theme. Did you create this amazing site yourself? Please reply back as I’m wanting to create my very own blog and would like to know where you got this from or what the theme is named. Many thanks!

  72. joker123

    Howdy! This post couldn’t be written much better! Looking at this post reminds me of my previous roommate! He always kept preaching about this. I most certainly will send this article to him. Fairly certain he’s going to have a good read. Thanks for sharing!

  73. slot online joker123

    Having read this I thought it was rather informative. I appreciate you spending some time and effort to put this content together. I once again find myself spending a significant amount of time both reading and commenting. But so what, it was still worth it!

  74. joker123 slot

    May I simply just say what a relief to discover somebody who actually understands what they’re talking about over the internet. You actually understand how to bring a problem to light and make it important. More and more people must look at this and understand this side of your story. It’s surprising you are not more popular since you certainly possess the gift.

  75. joker123

    Oh my goodness! Impressive article dude! Thank you, However I am going through difficulties with your RSS. I don’t understand the reason why I can’t join it. Is there anybody else having identical RSS problems? Anyone who knows the solution will you kindly respond? Thanks!!

  76. judi qq

    Nice post. I learn something new and challenging on blogs I stumbleupon on a daily basis. It’s always interesting to read through articles from other writers and practice something from their sites.

  77. Aplikasidewasa.com

    An intriguing discussion is definitely worth comment. There’s no doubt that that you should publish more about this subject matter, it might not be a taboo matter but usually folks don’t speak about these subjects. To the next! Many thanks!!

  78. Sahabatqq

    Right here is the perfect site for anyone who wishes to find out about this topic. You know a whole lot its almost tough to argue with you (not that I really would want to…HaHa). You certainly put a fresh spin on a subject which has been written about for ages. Excellent stuff, just excellent!

  79. levidio.musafirdigital

    I have to thank you for the efforts you’ve put in penning this blog. I really hope to check out the same high-grade content from you later on as well. In truth, your creative writing abilities has inspired me to get my own blog now 😉

  80. https://ardusat.org/

    Howdy! I could have sworn I’ve visited this blog before but after browsing through some of the articles I realized it’s new to me. Anyhow, I’m definitely delighted I stumbled upon it and I’ll be book-marking it and checking back regularly!

  81. dominoqqmu.com

    Howdy! I could have sworn I’ve visited this site before but after browsing through some of the articles I realized it’s new to me. Regardless, I’m definitely delighted I discovered it and I’ll be bookmarking it and checking back frequently!

  82. agen joker388

    Oh my goodness! Awesome article dude! Thank you so much, However I am going through troubles with your RSS. I don’t know why I can’t subscribe to it. Is there anybody having similar RSS problems? Anybody who knows the solution will you kindly respond? Thanks!!

  83. joker888

    Good post. I learn something totally new and challenging on blogs I stumbleupon every day. It will always be helpful to read through content from other authors and use a little something from other websites.

  84. agen joker 123

    Hi there! This blog post couldn’t be written any better! Looking through this post reminds me of my previous roommate! He always kept talking about this. I will send this article to him. Pretty sure he’s going to have a very good read. Many thanks for sharing!

  85. related site

    There are absolutely a great deal of details like that to think about. That is a terrific indicate bring up. I provide the thoughts above as general ideas yet plainly there are concerns like the one you bring up where the most vital thing will certainly be operating in straightforward good faith. I don?t understand if best methods have actually emerged around things like that, yet I am sure that your task is plainly determined as a level playing field. Both children as well as girls really feel the impact of simply a moment?s pleasure, for the remainder of their lives.

  86. sbobet

    Hello there! I could have sworn I’ve visited this website before but after browsing through some of the articles I realized it’s new to me. Anyhow, I’m certainly delighted I stumbled upon it and I’ll be bookmarking it and checking back regularly!

  87. login jokerwin123

    Hi there, I do think your site might be having web browser compatibility problems. Whenever I look at your website in Safari, it looks fine however, when opening in IE, it has some overlapping issues. I simply wanted to give you a quick heads up! Other than that, fantastic site!

  88. sbobet88 daftar

    After I initially commented I appear to have clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I get four emails with the same comment. Is there a way you can remove me from that service? Thanks!

  89. http://107.152.32.187/

    I would like to thank you for the efforts you have put in writing this blog. I really hope to see the same high-grade content from you later on as well. In fact, your creative writing abilities has inspired me to get my own, personal website now 😉

  90. read more here

    There are absolutely a great deal of information like that to consider. That is an excellent point to raise. I provide the thoughts over as basic motivation yet plainly there are questions like the one you raise where one of the most vital point will be working in honest good faith. I don?t understand if finest practices have actually arised around things like that, however I make certain that your job is plainly identified as a level playing field. Both young boys and also women really feel the impact of simply a moment?s satisfaction, for the remainder of their lives.

댓글 남기기

이메일은 공개되지 않습니다.