Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:08:39+00:00 2026-06-18T09:08:39+00:00

Repeat of a previous post, this time with the original code. I’m creating a

  • 0

Repeat of a previous post, this time with the original code.

I’m creating a SVG candlestick graph but I’m having trouble producing the appropriate height.

The x value is determined by the opening price. In some cases the closing price is greater than the opening price, thus the position needs to be adjusted accordingly.

Because a negative value produces an error, i.e no rectangle at all, I need to take the difference between the opening and closing price and add this value to the x position.

Here is the code I have now. How would I effectively execute this type of adjustment?

I considered a foreach statement, but my php knowledge is limited and I kept running in to errors.

EDITED

X value is incremented. Y value is opening price.

    <?php

$open_price = array(5,10,15,20,25,30);
$closing_price = array (3,15,50,15,14);

$svg = '<svg id="main_graph" xmlns="http://www.w3.org/2000/svg" version="1.1" height="400" width="600" style="border:1px solid black;">';

$bars;

    for ($i=0, $count = count($open_price); $i<$count; $i++)

    {        
      $calculation = ($open_price[$i])-($closing_price[$i]);                    
      $bars .= '<rect x="'. $i * 5 . '" y="' . $open_price[$i] . '" width="20" height="' . $calculation . '" fill="blue"/>';                
    };

echo $graph = $svg . $bars . '</svg>';

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Crazy Bars</title>
    <style type="text/css">

        #main_graph {margin-left: 100px;}

    </style>


</head>
<body>    
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:08:40+00:00Added an answer on June 18, 2026 at 9:08 am

    You probably want the height to be the absolute value of the difference between closing price and opening price. Use php’s abs() function to calculate it.

    $calculation = ($open_price[$i])-($closing_price[$i]); 
    $abscalc = abs($calculation);
    

    Then, if you wanted the bar to show in a downward direction for negative price movement, you’d want to adjust the y value as well.

    $y = $open_price[$i];
    if ($calculation < 0) $y = $closing_price[$i];
    $bars .= '<rect x="' . $i * 5 . '" y="'.$y.'" width="20" height="' . $abscalc . '" fill="blue"/>'; 
    

    This code will keep your x value constant, and cause the bar to show the price range. The bar height will be the absolute value of the price difference, and the bar’s y position will be the lower of the opening or closing price.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(moved from previous post) - sorry if this is seen as a repeat! Hi
I've been working on a small blog form but have been having trouble with
This question might seem a repeat of previous ones. I have read through a
First: This is not a repeat of this question . Same script, but different
This isn't a repeat of a previous question, I have found out the issue
I know this question may be repeat , but i didn't get any of
I have ask this kind of question before, but it seems my previous question
This may be a repeat of the following unanswered question: Help with bitmap lock
How do i repeat this, somehow the clear doesn't make the layout under eachother
Sorry to repeat old questions, but I didn't quite understand the answer. The question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.