Php stock K-line chart generation code _ PHP Tutorial

Source: Internet
Author: User
Code for generating the php stock K-line chart. This program implements the php bar chart. its design philosophy is: first, use imagecreate () to generate a blank image. The program implementation is as follows: two different colors are required, A positive number of this program implements php to draw a bar chart // its design philosophy is: first, use imagecreate () to generate a blank image; its program implementation is as follows: // two different colors are required. one is a negative number, one is a data array, and $ path is the address for saving the image.

// This program is used to draw a column chart in php. friends who develop stock websites often encounter a K-line image. now let's take a look at a K-line image generated using php based on array data.
// Its design philosophy is: first, use imagecreate () to generate a blank image. The program implementation is as follows:
// Two different colors are required. one is a negative number, one is a data array, and $ path is the address for saving the image.

$ 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 (0,255, 0), array (, 0 ));
$ Path = "gg15_600050.gif ";
$ Title = "China Unicom (600050) 30-day capital access ";
$ Date = array ("00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00 ", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00 ", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00 ", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:00:00 ");
$ Hai = array (1290,426,326, 5853,593,312, large, 2599,2630, large, large, small,, 5829,433,801, clerk, 9051,6596, 6858,6726, 1527,3970, clerk, 2202,9411, clerk, clerk, 6776,243,323,394, clerk, 8859, clerk, clerk, clerk, clerk, 2470,4126, clerk, 182,412,304 3,3208, 2438,6889, clerk, 6152,213,371, clerk, 6874,3742, 4974,4812, 6839,3016, 5625,4656, 6981,484,129, 8442,722,929, california, 9965,872,830, 7438, California, large, small, medium, small, 993,5269, 944,2718, 6633,4378, 8431,155,506, 8742,971,301 );
Draw (600,300, $ data, $ colors, $ path, $ title, $ date );
Function draw ($ w, $ h, $ data, $ colors, $ path, $ title, $ date)
{
// Create a new blank image
// The Arrow is 50 pixels and the content is 400x300.
$ Width = $ w;
$ Height = $ h;
// Get the coordinate point in the lower left corner. 50 pixels on the left and 50 pixels on the bottom are used to write the ruler.
$ L_ B _x = 50;
$ L_ B _y = $ height/2;

$ Chartfont = 2; // Chart font
$ Font = 'C:/windows/fonts/simsun. ttc '; // Chinese font supported
$ Chartfontheight = imagefontheight ($ chartfont); // The font size of the chart.

// Obtain the maximum data
$ Max = abs (max ($ data ));
$ Max = $ max> abs (min ($ data ))? $ Max: abs (min ($ data ));
$ Image = imagecreate ($ width, $ height );

// Fill the background in White
$ Color_white = imagecolorallocate ($ image, 0xff, 0xff, 0xff); // White
$ Color_diwen = imagecolorallocate ($ image, 0xd8, 0xd8, 0xd8); // dark gray
Imagefill ($ image, 0, 0, $ color_white );
// Write the logo
Imagettftext ($ image, 20, 0, $ l_ B _x + 20, 45, $ color_diwen, $ font, 'huanan Finance & Economics Network ');
// Draw coordinates
// The coordinate color is black. draw coordinates first and 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 );
// Draw the shading, Gray; draw 20 shading; dotted line
$ Pit = floor ($ l_ B _y-50)/10); // The height of each section
$ Pit_x = floor ($ width-$ l_ B _x-30)/count ($ data); // The width of each segment
// Calculation unit
$ X_max = $ max;
$ Flag_danwei = 0;
While ($ x_max> 1000)
{
$ X_max/= 100;
$ Flag_danwei ++;
}
Switch ($ flag_danwei)
{
// Case 0: $ danwei = '(yuan)'; break;
Case 0: $ danwei = '(tens of thousands)'; break;
Case 1: $ danwei = '(million)'; break;
Case 2: $ danwei = '()'; break;
Case 3: $ danwei = '(10 billion)'; break;
}
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 of the slave coordinate
$ V_v = number_format ($ max/(10 * pow (100, $ 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 coordinate
Imagestring ($ image,
$ Chartfont,
$ L_ B _x-20,
$ L_ B _y-10,
0,
$ Black_color );
// Write Chinese characters, 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 a column, 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 [1] [2]);
For ($ j = 0; $ j {
$ D_h = floor (abs ($ data [$ j]/$ max * 10 * $ pit); // The height of the column
$ 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 );
// Color the 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 );
} // The painting function ends.
// Output the generated image
/*

Header ("content-type: image/gif ");
$ My_colors [] = array (0xff, 0x00,0x00 );
$ My_colors [] = array (0x00, 0xff, 0x00 );
Draw (600,400, array (1066565650,-40345340,503433440, 1834340,50343230,-8023454230,600,723 2323300,-836), $ my_colors, 0, 'textbook Aston on ');*/
// Draw (600,400, array (1066,-404040,5040, 1840,5030,-8230,600,723 2,-836), $ my_colors, 0, 'textbook Aston on ');
?>

This site Original tutorial, reprinted indicated from http://www.bKjia. c0m/phper/php.html otherwise study

Trim () is used to generate a blank image. The program implementation is as follows: // two different colors are required, one digit...

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.