PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Jul 23, 2017 9:35 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: Fri Dec 14, 2012 12:57 pm 
Offline
Forum Contributor

Joined: Mon Dec 28, 2009 8:48 pm
Posts: 102
Hi there,
I can draw bar and line graph by php using gd library. But cant find a way to draw a area chart like below (here the area of violet color):
Image

Can anybody guide me how to draw a area shape by php?


Top
 Profile  
 
PostPosted: Fri Dec 14, 2012 2:47 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13417
Location: New York, NY, US
I think you would probably want to use imagefilledpolygon() (http://php.net/manual/en/function.image ... olygon.php). Fill the array with the bottom points, top points and end points.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sat Dec 15, 2012 1:15 am 
Offline
Forum Contributor

Joined: Mon Dec 28, 2009 8:48 pm
Posts: 102
Thanks Christopher for your reply and suggestion. Yes, that may be a way using GD library. But there is a problem also. The array points will be derived after the calculation of the moving average (in the graph, the two solid curves in between the area shape) and the candlesticks (the red and green bars throughout the area). Actually in the picture, the upper and lower border line of the shape are constructed by +20 and -20 of the respective moving averages.

As a result the upper and lower band's array points will come after calculating the moving average (solid two curves). So I have to draw the curves and bars first, then have to draw the polygons in sequence. But, if I draw polygon after drawing the curves, it will overlap the two solid curves and the candlestick bars and those will not be seen after completion of imagecreate. And If I store the x,y coordinates of curves and candlestick bars by variables for drawing after polygons, there will be a huge memory consumption as there will be lots of x,y coordinate points for two curves and specially for candlestick bars (for each bar, there are 8 x,y coordinate values).

My query is, is there any function by which I can draw the polygon after drawing two curves but the polygon will remain behind the curves and bars? In that situation if 'Z' order can be accomplished for polygon, it can do the thing what I expecting.

Also, there is no option for creating outline border of the area shape by imagefilledpolygon, is there?

Regards


Top
 Profile  
 
PostPosted: Sat Dec 15, 2012 11:20 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13417
Location: New York, NY, US
infomamun wrote:
And If I store the x,y coordinates of curves and candlestick bars by variables for drawing after polygons, there will be a huge memory consumption as there will be lots of x,y coordinate points for two curves and specially for candlestick bars (for each bar, there are 8 x,y coordinate values).
I can't imagine that there would be a "huge memory consumption" storing maybe 100 arrays of 8 x,y pairs. You're building images in memory and worring about an array of integers?!? PHP programs fetch 100+ database records that are bigger than that without thinking twice. And not sure why you'd need to store all the values of the bars on the first pass anyway -- they are all the same width. Sounds like Premature Optimization. I'd pre-process the data and build the image in layers back to front. Then see if there is a real memory problem.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sun Dec 16, 2012 12:49 pm 
Offline
Forum Contributor

Joined: Mon Dec 28, 2009 8:48 pm
Posts: 102
Christopher wrote:
And not sure why you'd need to store all the values of the bars on the first pass anyway -- they are all the same width. Sounds like Premature Optimization.

for each bar, there are four x,y coordinates (x1,x2,y1,y2) and you might have noticed that there are two threads/sticks attached with each candlestick bar, for those you need more 2x2=4 coordinates. Although bars are of same width but not of same height. So there will be total 8 coordinates for each bar. Additionally if you want border around each bar, there will be atleast 4 coordinates more.

Christopher wrote:
You're building images in memory and worring about an array of integers?!?

Yes I am worried, because I have a shared hosting in which almost all pages are dynamic. It scraps pages from a stock market website and shows in a simpler format. Thus it creates a heavy load on shared server. Few days ago, I was bound to restrict some of my pages by login system as my site was suspending for using over-resources (only 15% CPU usage). So I try to use as much simpler code as I can. I even don't use more than two/three include files in one page to reduce http requests per page.

My intention was to ask you whether there was any way to draw the bars and curves first and then polygon in sequence by using some types of mask/transparency for this reason.

Wishing a help still from you.


Top
 Profile  
 
PostPosted: Sun Dec 16, 2012 4:36 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13417
Location: New York, NY, US
infomamun wrote:
for each bar, there are four x,y coordinates (x1,x2,y1,y2) and you might have noticed that there are two threads/sticks attached with each candlestick bar, for those you need more 2x2=4 coordinates. Although bars are of same width but not of same height. So there will be total 8 coordinates for each bar. Additionally if you want border around each bar, there will be atleast 4 coordinates more.

I understand that you need to calculate those coordinates when you draw the bars, but that is a later pass. I assume you are starting with a high/low or open/close values for each day. On the first pass you draw the grid. Then you want to draw the purple area. You don't need the coordinates for the bars yet -- just the the top and bottom of the purple area for that day. Once you draw the purple area, then calculate the coordinates for the bars and draw the bars.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Mon Dec 17, 2012 3:37 am 
Offline
Forum Contributor

Joined: Mon Dec 28, 2009 8:48 pm
Posts: 102
Actually purple area is not directly related with bars, but with moving average (the two solid curves inside purple area). Moving Averages are calculated in each minute and upper and lower bottom of purple area are calculated by the following formula:
Upper point = Moving Average+20% of Moving Average
Lower point = Moving Average-20% of Moving Average
The whole trade hour per day is about 4hrs, that means there are 4x60=240 minutes and in each minute I have to calculate moving averages and upper and lower point of purple areas. So, if I can draw moving averages and purple area at the same time, it can reduces task and memory consumption.

By the by, is it possible to create z-order for purple area or can create masking?


Top
 Profile  
 
PostPosted: Mon Dec 17, 2012 7:28 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13417
Location: New York, NY, US
infomamun wrote:
Actually purple area is not directly related with bars, but with moving average (the two solid curves inside purple area). Moving Averages are calculated in each minute and upper and lower bottom of purple area are calculated by the following formula:
Upper point = Moving Average+20% of Moving Average
Lower point = Moving Average-20% of Moving Average
The whole trade hour per day is about 4hrs, that means there are 4x60=240 minutes and in each minute I have to calculate moving averages and upper and lower point of purple areas. So, if I can draw moving averages and purple area at the same time, it can reduces task and memory consumption.
My point was that you could calculate just what is needed on each pass -- as you were concerned about creating arrays for some reason.

infomamun wrote:
By the by, is it possible to create z-order for purple area or can create masking?
Not that I know of.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Dec 19, 2012 11:10 am 
Offline
Forum Contributor

Joined: Mon Dec 28, 2009 8:48 pm
Posts: 102
Hello Christopher,
Would you help in this regard if I provide you a php library that can draw area chart? There is a php class 'chart.php' which draw this area chart. But I am dull in understanding php class. Would you tell me by which method (imagefilledrectangle/imagefilledpolygon etc) this library is drawing area chart?

I have attached the library with this reply and the web address of this library is:
http://quimby.gnus.org/circus/chart/chart-manual.php

Please help me finding the method if you have time.

Regards


Attachments:
chart-0.8.tar.gz [38.32 KiB]
Downloaded 1078 times
Top
 Profile  
 
PostPosted: Wed Dec 19, 2012 3:08 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13417
Location: New York, NY, US
Please provide the code that draws the graph in the first post. And and an example data set for the number of days on that chart. Thanks.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Thu Dec 20, 2012 12:20 am 
Offline
Forum Contributor

Joined: Mon Dec 28, 2009 8:48 pm
Posts: 102
Dear Chritopher
Thanks for your reply. I don't have the code of first post, because that picture is from another site. I am trying to code for that type of chart. I have my own code which pulls data from my mysql table (which creates chart only for moving average). If you want, I can send you the code by email or by PM (if possible).
But in the previous post, the attached post contain data and necessary codes for area chart. After unzip the attached file, you may find data array from data.php and area chart code from example12.php and the class file which can produce area chart from chart.php.

You may also view demo of area chart from the link below:
http://quimby.gnus.org/circus/chart/chart-manual.php

Thanks


Top
 Profile  
 
PostPosted: Thu Dec 20, 2012 10:17 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13417
Location: New York, NY, US
I can look at example12.php, but if you can post you own example it would be better. They both you and I, and others, can work through the problem.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Thu Dec 20, 2012 1:08 pm 
Offline
Forum Contributor

Joined: Mon Dec 28, 2009 8:48 pm
Posts: 102
Hi Christopher,
I PM my code to you. Please check your private message (PM). Also, please don't quote the code here in this topic. Just discuss any matter indirectly here. If you want to ask in details for any part of my code, it will be my pleasure if you PM me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group