Results 1 to 8 of 8

Thread: Creating a diagonal line (as on a graph) in HTML?

  1. #1
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default Creating a diagonal line (as on a graph) in HTML?

    I'm doing pretty well making a scatterplot graphs (just one with X-Y coordinate points) in HTML for a current data analysis project. And I'm generating statistics, etc. (All based in PHP and HTML for presentation; no images.)

    Is there any easy way to make a diagonal line in HTML to connect two points and create a line graph? Assuming I can deal with the math (figuring out length, angle and coordinates for both endpoints, based on relative locations on the page).

    I don't want to use a canvas or images because that would make it too complicated and defeat the point in some sense, and it also might make the page sluggish if I end up with a lot of lines.


    My current idea is maybe to use an <hr> and rotate it. Would this work out?


    I'm also not particularly attached to cross-browser compatibility-- if it works for me in Firefox (or perhaps another browser instead) then I'm happy. This is for personal use, though I wouldn't mind working up to something more stable later.
    Last edited by djr33; 10-11-2012 at 05:44 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #2
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    You can rotate an <hr> tag using the css transform property, I've set up a jsfiddle for you here demonstrating what I mean.
    Edit:
    The jsfiddle is blank for some reason, so here is the code:
    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    hr {
    transform:rotate(5deg);
    -ms-transform:rotate(5deg); 
    -moz-transform:rotate(5deg); 
    -webkit-transform:rotate(5deg);
    -o-transform:rotate(5deg);
    }
    </style>
    </head>
    <body>
    
    <hr></hr>
    
    </body>
    </html>

    Edit:
    Ok, fixed now, see here for the fiddle
    Last edited by bernie1227; 10-10-2012 at 05:23 AM.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  3. The Following User Says Thank You to bernie1227 For This Useful Post:

    djr33 (10-10-2012)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Great, thanks! I thought something like that might work, but last night I was too exhausted after writing the rest of my code to figure it out. Thanks for filling in the various cross-browser CSS codes as well.

    For anyone else looking for a similar answer, here's everything worked out in a little more detail:

    1. Calculate start points and end points, however that works on your page. (Hopefully that's just x and y values for each, within one region on the page-- not as complex as my page is right now, haha-- but I'll still get it there, just with a few more steps.)
    2. Convert the ordered pair of points to an angle for the line (fun trigonometry! rise over run and all that...).
    3. Calculate the distance from point1 to point2-- pythagorean equation (again, rise and run for the sides and calculate the hypotenuse).
    3. Dynamically set the values for the HTML and CSS below:
    Code:
    hr
    {
    /*Replace 5 below with whatever angle you calculated*/
    transform:rotate(5deg);
    -ms-transform:rotate(5deg);
    -moz-transform:rotate(5deg);
    -webkit-transform:rotate(5deg);
    -o-transform:rotate(5deg);
    }
    
    --------------
    
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
        <!--Replace width of 50 with length; top of 80 with height-y; and left of 20 with x-->
        <hr style="width:50px;position:absolute;top:80px;left:20px;">
    
    </body>
    </html>
    And now for some trigonometry to get it setup on my page.


    EDIT: There's a problem. I'll need to test it further. The rotation is going to be based on the center of the object, I expect. Therefore, it will be necessary to slide it up half of the rise (or down if rise is negative) and over (half length - half of the run) to move it to be attached to the starting point at one end-- I'll play with this later.
    Last edited by djr33; 10-10-2012 at 05:05 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

  6. The Following User Says Thank You to molendijk For This Useful Post:

    djr33 (10-11-2012)

  7. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Thanks Arie, but that looks like the same code posted by Bernie above. Am I missing an important difference? I might be-- it has been a long day.


    Oh, I see. It works for earlier versions of IE? Hm. That's useful. For this particularly project I don't care about IE at all, but that would be useful for future versions of my script if I end up generalizing/releasing it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #6
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    the only difference appears to be this line of code:
    Code:
    behavior:url(-ms-transform.htc);
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  9. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Right. Which (I think) makes IE7 work.... right?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #8
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Yes, hence the name of the link.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

Similar Threads

  1. Replies: 7
    Last Post: 08-30-2010, 01:33 PM
  2. MySQL to line graph
    By singar in forum PHP
    Replies: 1
    Last Post: 01-04-2010, 06:17 PM
  3. Resolved Creating a heading with an expanding line to the left
    By jamidodger in forum CSS
    Replies: 11
    Last Post: 11-08-2009, 05:31 PM
  4. Creating a Dotted Css Table without Top line!
    By hicricket in forum Looking for such a script or service
    Replies: 2
    Last Post: 12-24-2007, 03:39 PM
  5. enhancements to Balamurugan Line Graph and Pie Chart scripts?
    By itp in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 04-21-2006, 02:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •