PHP Stock Candlestick Chart Generation Code _php tutorial

Source: Internet
Author: User
Tags dashed line
This program implements PHP histogram//Its design idea is: first to use Imagecreate () to generate a blank shape, its program is implemented as follows://need two different colors, positive number of a negative one, an array of data, $path to save the image of the address

This program to implement PHP histogram, the development of stock site friends often encounter in the generation of K-line images, now we take a look at the use of PHP based on the array of data generated by the K-line image.
Its design idea is: first to use Imagecreate () to generate a blank shape, its program is implemented as follows:
Requires two different colors, a positive number, a negative one, an array of data, $path to save the image's address

$data = Array ("1174.58", "1865.41", "1961.24", "-1174.58", "-1865.41", "1961.24", "1174.58", "-1865.41", "1961.24", "- 1174.58 ","-1865.41 "," 1961.24 "," 1174.58 ","-1865.41 "," 1961.24 ","-1174.58 ","-1865.41 "," 1961.24 ","-1174.58 ","- 1865.41 "," 1961.24 "," 1174.58 "," 1865.41 "," 1961.24 "," 1174.58 ","-1865.41 "," 1961.24 ","-1174.58 ","-1865.41 "," 1961.24 ");
$colors = Array (Array (255,0,0), Array (0,255,0));
$path = "Gg15_600050.gif";
$title = "China Unicom (600050) 30th Capital Access";
$date = Array ("2010-05-20 00:00:00", "2010-05-20 00:00:00", "2010-05-20 00:00:00", "2010-05-20 00:00:00", "2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 "," 2010-05-20 00:00:00 ");
$hai = Array ( 7203,6037,1931,9605,7405,8855,228,7771,8671,8791,1290,426,3265,4298,4809,3350,8204,8682,5523,7569,5853,593,3124,9062,3329 , 3236,2252,8756,2796,5164,2599,2630,1470,9779,8805,1512,1517,5635,760,1380,152,6035,2905,3163,9443,9432,5031,2838,6802,93 30,1878,1333,4297,4180,3037,4527,9043,7287,3248,3448,3405,7204,2203,3974,4399,7333,4541,7033,4292,6128,9955,8603,297,6992 , 3282,9612,4483,8379,4016,6038,4163,3789,6896,8710,1502,1579,1712,3705,3619,5150,3007,1853,2776,3056,569,9031,6092,8600,8 941,6366,5829,433,8011,9637,9106,1761,2422,9873,8186,1136,1763,3050,285,1962,775,9900,7371,2691,9051,6596,6858,6726,1527 , 3970,910,2621,7598,7899,6546,3678,2202,9411,6129,8352,4830,4484,6776,243,323,3946,751,7508,8173,3776,5071,3847,3318,4901 , 2102,7516,5251,3299,8859,2114,8711,9800,7707,5898,3489,580,5763,2470,4126,6269,5191,182,412,3043,3208,2438,6889,1732,197 9,7692,3445,8214,8620,9446,4624,3615,5496,6152,213,3715,9623,1215,4757,9443,3366,2988,8043,6874,3742,4974,4812,6839,3016 , 5625,4656,5315,8303,8346,2315,3230,1969,1688,7381,6463,1030,8856,4691,6042,7652,8502,3839,7844,9149,4029,9465,2897,7688,3791,1458,6981,484,12 90,4261,1222,9503,4881,7663,1399,2260,8066,6405,8442,722,9297,177,8426,2103,2078,3521,2280,8570,4663,6227,5784,7438,654,3 036,9965,872,8306,3537,899,5888,1319,2472,9218,4853,7849,9433,1196,4880,1597,9258,7465,8138,817,1637,4201,1939,8914,1114 , 2232,8251,4538,7378,7438,4489,993,5269,944,2718,6633,4378,8431,155,5061,9347,1125,8865,4629,9965,4706,6263,8742,971,301 );
Draw (600,300, $data, $colors, $path, $title, $date);
function Draw ($w, $h, $data, $colors, $path, $title, $date)
{
Create a new blank shape
Arrows 50 pixels, content 400*300
$width = $w;
$height = $h;
Get the coordinate point in the lower left corner, 50 pixels to the left and 50 pixels at the bottom for writing the ruler
$l _b_x = 50;
$l _b_y = $height/2;

$chartfont = 2; Chart font
$font = ' C:/WINDOWS/FONTS/SIMSUN.TTC ';//fonts that support Chinese characters
$chartfontheight = Imagefontheight ($chartfont);//The size of the chart font

Get the most data
$max = ABS (max ($data));
$max = $max >abs (min ($data))? $max: Abs (min ($data));
$image = Imagecreate ($width, $height);

Fill background is white
$color _white = imagecolorallocate ($image, 0xFF, 0xFF, 0xff);//White
$color _diwen = imagecolorallocate ($image, 0xd8,0xd8,0xd8);//Dark grey
Imagefill ($image, 0,0, $color _white);
Write logo
Imagettftext ($image, 20,0, $l _b_x+20,45, $color _diwen, $font, ' South China Financial Network ');
Draw coordinates
Coordinate color is black, draw coordinates first, then draw arrows
$black _color = imagecolorallocate ($image, 0x00,0x00,0x00);
Imageline ($image, $l _b_x, $l _b_y, $l _b_x,10, $black _color);
Imageline ($image, $l _b_x, $l _b_y, $width -10, $l _b_y, $black _color);
Arrow
Imageline ($image, $l _b_x,10, $l _b_x-3,14, $black _color);
Imageline ($image, $l _b_x,10, $l _b_x+3,14, $black _color);
Imageline ($image, $width -10, $l _b_y, $width -13, $l _b_y-3, $black _color);
Imageline ($image, $width -10, $l _b_y, $width -13, $l _b_y+3, $black _color);
Shading, Gray, 20 shading; dashed line
$pit = Floor (($l _b_y-50)/10);//height of each segment
$pit _x = Floor (($width-$l _b_x-30)/count ($data));//width of each segment
Unit of calculation
$x _max = $max;
$flag _danwei = 0;
while ($x _max> 1000)
{
$x _max/= 100;
$flag _danwei++;
}
Switch ($flag _danwei)
{
Case 0: $danwei = ' (yuan) ';
Case 0: $danwei = ' (million) ';
Case 1: $danwei = ' (million) ';
Case 2: $danwei = ' (billion) ';
Case 3: $danwei = ' (tens of billions) ';
}
for ($i =1; $i <11; $i + +)
{
Imageline ($image, $l _b_x+1, $l _b_y-$i * $pit, $width -15, $l _b_y-$i * $pit, $color _diwen);
The value from the coordinate
$v _v = Number_format (($max/(10*pow ($flag _danwei)) * $i), 2);
$v _v = substr ($v _v,0,5);
Imagestring ($image,
$chartfont,
$l _b_x-40,
$l _b_y-$i * $pit-10,
$v _v,
$black _color);
}
Origin Point coordinates
Imagestring ($image,
$chartfont,
$l _b_x-20,
$l _b_y-10,
0,
$black _color);
Write kanji, units and titles
Imagettftext ($image, 10,0, $l _b_x-45,20, $black _color, $font, $danwei);
Imagettftext ($image, 11,0, $width/2-70, $l _b_y+20, $black _color, $font, $title);

Draw the cylinder, rectangle, and color
$color _big = imagecolorallocate ($image, $colors [0][0], $colors [0][1], $colors [0][2]);
$color _small = imagecolorallocate ($image, $colors [1][0], $colors [1][1], $colors [[]];
for ($j =0; $j <>
{
$d _h = Floor (ABS ($data [$j]/$max *10* $pit);//Column height
$current _date = substr ($date [$j],5,5);
$current _date. = substr ($date [$j],8,2);
Imagerectangle ($image,
$l _b_x+ $j * $pit _x+1,
$l _b_y,
$l _b_x+ ($j + 1) * $pit _x-2,
$l _b_y-$d _h,
$black _color);
Coloring a rectangle
$current _color = $data [$j]>0? $color _big: $color _small;
Imagefilltoborder ($image,
$l _b_x+ $j * $pit _x+1 + floor ($pit _X/2),
$l _b_y-floor ($d _h/2),
$black _color,
$current _color);
Write Date
/*imagestring ($image,
$chartfont,
$l _b_x+ $j * $pit _x,
$l _b_y+2,
$current _date,
$black _color); * *
Imagettftext ($image, 8,90, $l _b_x+ $j * $pit _x+10, $l _b_y-$d _h, $black _color, $font, $current _date);
}
if ($path! = "")
Imagegif ($image, $path);
else Imagegif ($image);
Imagedestroy ($image);
}//drawing function Ends
Output the resulting picture
/*

Header ("Content-type:image/gif");
$my _colors[] = array (0xff,0x00,0x00);
$my _colors[] = array (0x00,0xff,0x00);
Draw (600,400,array (1066565650,-40345340,50343433440,1834340,50343230,-8023454230,600,7232323300,-836), $my _ colors,0, ' textbook Great Aston '); * *
Draw (600,400,array (1066,-4040,5040,1840,5030,-8230,600,7232,-836), $my _colors,0, ' textbook Great Aston ');
?>

This site original tutorial, reproduced note from the http://www.bKjia.c0m/phper/php.html or else must investigate

http://www.bkjia.com/PHPjc/633000.html www.bkjia.com true http://www.bkjia.com/PHPjc/633000.html techarticle This program implements PHP histogram//Its design idea is: first to use Imagecreate () to generate a blank graph, its program implementation is as follows://need two different colors, positive one ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.