Talk about PHP graphing (iii) _php tutorial

Source: Internet
Author: User

The last time I said to use GD for various geometric shapes, and fill color. Which deliberately put such a more complex situation
Left behind, this is the fill color for freeform and freeform.

Header ("Content-type:image/png");
$im = imagecreate (200, 100);
$col _blk = imagecolorallocate ($im, 0,0,0);
$col _grn = imagecolorallocate ($im, 0,255,0);

$parray = Array (40,10,60,10,70,20,60,50,40,50,30,20);
Defines an array with 12 members being the horizontal ordinate of 6 points.
Imagepolygon ($im, $parray, 6, $col _GRN);
This is the function that draws the freeform, $parray is the array you just defined,
6 represents six points. Note that six points are connected to a hexagon.
You do not have to artificially add a point that is the same as the 1th in order to close the graph.

Imagepng ($im);
Imagedestroy ($im);
?>

You should have thought of the function of the freeform fill color:

Header ("Content-type:image/png");
$im = imagecreate (200, 100);
$col _blk = imagecolorallocate ($im, 0,0,0);
$col _orn = imagecolorallocate ($im, 255,192,0);
$col _yel = imagecolorallocate ($im, 255,255,0);
$col _red = imagecolorallocate ($im, 255,0,0);
$col _grn = imagecolorallocate ($im, 0,255,0);
$col _blu = imagecolorallocate ($im, 0,0,255);

$parray = Array (40,10,60,10,70,20,60,50,40,50,30,20);
Imagefilledpolygon ($im, $parray, 6, $col _GRN);

Imagepng ($im);
Imagedestroy ($im);
?>

Well. Below we can write on the image. However, do not be happy, to write Chinese characters will cost some trouble.
This will be explained gradually later. Let's take a look at how to write a Western character in a nutshell.

Header ("Content-type:image/png");
$im = imagecreate (200, 250);
$col _blk = imagecolorallocate ($im, 0,0,0);
$col _orn = imagecolorallocate ($im, 255,192,0);

$str = "This is a test.";
Imagestring ($im, 1,10,10, $str, $col _orn);
Imagestring ($im, 2,10,30, $str, $col _orn);
Imagestring ($im, 3,10,60, $str, $col _orn);
Imagestring ($im, 4,10,100, $str, $col _orn);
Imagestring ($im, 5,10,150, $str, $col _orn);
It calls Imagestring five times in a row, in different locations,
The string $str is output from small to large glyphs respectively.
imagestring function supports only five glyphs

Imagepng ($im);
Imagedestroy ($im);
?>

Look again:

Header ("Content-type:image/png");
$im = imagecreate (200, 250);
$col _blk = imagecolorallocate ($im, 0,0,0);
$col _orn = imagecolorallocate ($im, 255,192,0);

$str = "This is a test.";
Imagestringup ($im, 1,10,180, $str, $col _orn);
Imagestringup ($im, 2,20,180, $str, $col _orn);
Imagestringup ($im, 3,40,180, $str, $col _orn);
Imagestringup ($im, 4,70,180, $str, $col _orn);
Imagestringup ($im, 5,110,180, $str, $col _orn);
The function name is replaced with Imagestringup, and the usage is unchanged.
is the output vertical text.

Imagepng ($im);
Imagedestroy ($im);
?>

When using the function of the output character, if you can know the width and height of the characters of different fonts in the image,
How convenient it is to arrange the location of the output characters! PHP is available to us: Imagefontwidth () and
Imagefontheight (), whose parameters are simple, only one: the number of the font. For example Imagefontwidth (5)
is to get the 5th word width of each character, Imagefontheight (3) is to get the height of each character of the 3rd word. So
Simple, not an example, wait a bit later in the code is also useful.

Similar to the output string, Imagechar and Imagecharup output a single character for less use and can even
No--no matter the character or the string, you can use Imagestring and Imagestringup.

Below, I used to draw a part of the code of the Stock Candlestick analysis chart, and put the above-mentioned content system
To apply it a bit. Because it involves a database, you can not bring the original code to everyone to take back the test. can only construct some
Data, simulating the stock market that was obtained from the database. Since there may not be many people here who know the stock candlestick, we may not be aware of the candlestick
How should the figure be drawn? However, I can't tell you how the candlestick is going on here, but I'm just going to introduce a series of methods. and other paintings
Well, you can certainly see that you've actually seen such a diagram before.

Header ("Content-type:image/png");

$im = Imagecreate (640,260);
$bkground = Imagecolorallocate ($im, 255,255,255);
$data = Imagecolorallocate ($im, 0,0,0);
$gird = Imagecolorallocate ($im, 200,200,160);
$upline = Imagecolorallocate ($im, 255,0,0);
$dnline = Imagecolorallocate ($im, 0,175,175);
$d 5line = imagecolorallocate ($im, 255,127,0);
$d 20line = imagecolorallocate ($im, 0,0,127);
$d 10line = imagecolorallocate ($im, 255,0,255);
First define the colors used to paint the various objects.

for ($i =20; $i <=220; $i +=25)
Imageline ($im, $i, 560, $i, $gird);
for ($j =60; $j <=560; $j +=25)
Imageline ($im, $j, $j, $gird);
It is much easier to calculate the position and width of the grid beforehand and draw the line with a for loop.

$ZZG = 10.55;
$zzd = 7.63;
$LZG = 10350;
Hypothetical stock market data,
$ZZG is the highest price that needs to be analyzed for this period of time, assuming it is 10.55 yuan.
$ZZD is the minimum price to be analyzed for this period of time, assuming that it is 7.63 yuan.
$LZG is the highest volume that needs to be analyzed for this period of time, assuming 10350 lots.
This is an important data for calculating the "scale" of the coordinate grid.
$BL = $zzg-$zzd;
The difference between the high price and the lowest price. According to the ratio between it and the total height of the grid,
You can draw an actual price in the grid where it is located.

for ($i =1; $i <=7; $i + +)
{
$y = $i *25-10;
Calculates the appropriate height (ordinate) of the callout scale based on the position of the grid line.
$str =number_format ($ZZG-($i-1)/6* $BL, 2, ".", ",");
Calculates the price for each tick mark, and formats the string.
$x =55-imagefontwidth (2) *strlen ($STR);
The appropriate horizontal axis is calculated based on the width that the string will occupy.
Imagestring ($im, 2, $x, $y, $str, $data);
Write the string.
}

$str =number_format ($LZG, 0, ".", ",");
Imagestring ($im, 2,564,164, $str, $data);
$str =number_format ($lzg/2,0, ".", ",");
Imagestring ($im, 2,564,189, $str, $data);
Since there are only two scales for writing volume, it is not advantageous to write in a loop.
If the quantity is more, also should use the circulation.


//Because a candlestick chart is drawn with countless bars, a small candlestick is drawn as a function
Functions Kline ($img, $KP, $zg, $zd, $sp, $CJL, $ii)
//Parameters: $img image; $KP $ ZG $zd $sp is open, high, low, close,
//$CJL volume, $ii counter, indicating the number of candlestick bars.
{
Global $bl, $zzd, $LZG;
//Declare the $BL used in the function, $ZZD, $LZG Three variables are global variables.
$h =150;//Candlestick column area height is 150.
$hh =200;//Candlestick column area, volume column area total height is 200.

if ($sp < $KP)
$linecolor = Imagecolorallocate ($img, 0,175,175);
//If the closing price is lower than open, is the Yin line, with cyan
Else
$ LineColor = Imagecolorallocate ($img, 255,0,0);
//Otherwise the Yang line, in red.

$x =58+ $ii;
//calculates the horizontal axis according to the candlestick sequence number.
$y 1=20+ $h-($KP-$ZZD)/$BL * $H;
//calculates the corresponding ordinate according to the open price.
$y 2=20+ $h-($SP-$ZZD)/$BL * $H;
//calculates the corresponding ordinate according to the closing price.
$y 3=20+ $h-($ZG-$ZZD)/$BL * $H;
//calculates the corresponding ordinate according to the high price.
$y 4=20+ $h-($ZD-$ZZD)/$BL * $H;
//calculates the corresponding ordinate according to the lowest price.
$y 5=20+ $hh-$CJL/$lzg * ($HH-$h);
//calculates the corresponding ordinate according to the volume.

if ($y 1<= $y 2) imagefilledrectangle ($img, $x-1, $y 1, $x +1, $y 2, $linecolor);
else Imagefilledrectangle ($img, $x-1, $y 2, $x +1, $y 1, $linecolor);
The horizontal axis minus 1 To add 1, the span is 3. A small filled rectangle with a width of 3 is drawn.
Height and ordinate are determined by the opening and closing price.
It has been tested that the function must be preceded by the upper-left point coordinate before the lower-right coordinate.
Instead of automatically judging two points to the left, the right bottom.

Imagefilledrectangle ($img, $x-1, $y 5, $x +1,220, $linecolor);
Draw volume cylinders based on volume.
Imageline ($img, $x, $y 3, $x, $y 4, $linecolor);
Draws the upper and lower lines according to the highest and lowest prices.<>< p=""><>

http://www.bkjia.com/PHPjc/508314.html www.bkjia.com true http://www.bkjia.com/PHPjc/508314.html techarticle The last time I said to use GD for various geometric shapes, and fill color. It is deliberately to leave such a more complex situation behind, which is the fill color of freeform and freeform. ...

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