php


Difficulty with If statement within for loop to produce proper height value for SVG graph


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>
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.

Related Links

Recurly subscription non-prorated
How can I get parameters to automatically appear within brackets?
How to prevent display comments for specific post types on Wordpress comments list
Laravel 5.3 Twig rcrowe/twigbridge 0.9.4 extends function not working properly
can not insert hebrew in mysql db version 5.1.56 community with php version 5.2.1
Speeding the process of MySql insert
Rectifying excel sheet in PHP
Codeigniter : Trying to get property of non-object of row()
Tymon JWT using database provider instead of eloquent
Share templates between React/Vue.js and PHP + HAML
Prestashop how can i get the posted input value under order page?
Classes, Namespacing, bin/console commands
Add Woocommerce Product Addon Fields to EventOn Tickets
echo next array value from sequence
Json response array inside array for specific string
How to match and parse specific info in tags with varying form

Categories

HOME
weblogic12c
long-integer
applescript
initialization
magento-2.0
yql
dji-sdk
facebook-android-sdk
sentry
phpmqtt
specifications
siesta
xamarin-zebble
multiple-tables
sipp
vuforia
raima
ipa
ruby-daemons
python-imageio
tortoisehg
acrobat
audit
envoy
publishing
advanced-filter
autoencoder
jslint
google-now
windows-store
plane
resampling
get-event-store
angular-datatables
flipkart
vungle-ads
music21
sony-future-lab-n
punctuation
sequence-diagram
android-mediarecorder
mongodb-aggregation
msxml
http4s
portal
google-identity-toolkit
bigdecimal
java-2d
s-function
xcode8-beta4
yii2-model
android-syncadapter
noclassdeffounderror
django-redis
verisign
certificate-authority
variable-length-array
decidable
hmisc
ngcordova
html-escape-characters
tkx
nhibernate-criteria
adwords-apiv201402
back
mobile-country-code
screwturn
usb-flash-drive
illuminate-container
insertonsubmit
apportable
chartfx
aero
mongo-jackson-mapper
session-0-isolation
clrprofiler
wndproc
word-frequency
first-class
struts2-s2hibernate
dropshadow
office-automation
bucket
rakudo
acceleration
chronic

Resources

Encrypt Message