PHP plot (3)

Source: Internet
Author: User

The last time we talked about using GD for various geometric figures and fill colors. This is a complicated situation.
Leave it behind, which is the fill color of any polygon and any polygon.

<?
Header ("Content-type: image/png ");
$ Im = ImageCreate (200,100 );
$ Col_blk = ImageColorAllocate ($ im, 0, 0 );
$ Col_grn = ImageColorAllocate ($ im, 0,255, 0 );

$ Parray = array );
// Define an array. The 12 members are the horizontal and vertical coordinates of the six points.
ImagePolygon ($ im, $ parray, 6, $ col_grn );
// This is the function for drawing arbitrary polygon. $ parray is the defined array,
// 6 indicates six points. Note that six points are connected to a hexagonal structure.
// You do not have to add the same vertex at the end to close the graph.

ImagePNG ($ im );
ImageDestroy ($ im );
?>

You should have thought of the function of filling any polygon color:

<?
Header ("Content-type: image/png ");
$ Im = ImageCreate (200,100 );
$ Col_blk = ImageColorAllocate ($ im, 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, 255 );

$ Parray = array );
ImageFilledPolygon ($ im, $ parray, 6, $ col_grn );

ImagePNG ($ im );
ImageDestroy ($ im );
?>

Well. Now we can write on the image. However, it may take some trouble to write Chinese characters.
This will be explained later. Let's take a look at how to simply write Spanish characters.

<?
Header ("Content-type: image/png ");
$ Im = ImageCreate (200,250 );
$ Col_blk = ImageColorAllocate ($ im, 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, 150, $ str, $ col_orn );
// ImageString is called five times in a row in different locations,
// Output the string $ str in a small to large font.
// The ImageString function supports only five types of fonts (1 ~ 5)

ImagePNG ($ im );
ImageDestroy ($ im );
?>

Let's look at it again:

<?
// Header ("Content-type: image/png ");
$ Im = ImageCreate (200,250 );
$ Col_blk = ImageColorAllocate ($ im, 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 changed to ImageStringUp, and the usage remains unchanged.
// Output vertical text.

ImagePNG ($ im );
ImageDestroy ($ im );
?>

While using the function of output characters, if you can know the width and height of different fonts in the image,
How convenient it is to arrange the output characters! PHP provides: ImageFontWidth () and
ImageFontHeight () has only one parameter: the number of the font. For example, ImageFontWidth (5)
Is to get the width of each character of the 5 character, ImageFontHeight (3) is to get the height of each character of the 3 character. So
This is simple. I will not give it an example. It will be used later in the code.

Similar to the output string, ImageChar and ImageCharUp output a single character, which is rarely used or even can
No-you can use ImageString and ImageStringUp regardless of the character or string!

Next, I will use part of the code that I have used to draw a stock K-line analysis diagram to introduce the content system mentioned above.
Application. Because it involves databases, you cannot take the original code back for testing. Only some
Data to simulate the stock market obtained from the database. Since there may not be many people who understand the stock keline
How to draw a graph. However, I cannot tell you what the keline is, but I just want to introduce such a series of methods. Other paintings
After that, you can see that you have seen such a picture before.

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

$ Im = ImageCreate (640,260 );
$ Bkground = ImageColorAllocate ($ im, 255,255,255 );
$ Data = ImageColorAllocate ($ im, 0, 0 );
$ Gird = ImageColorAllocate ($ im, 200,200,160 );
$ Upline = ImageColorAllocate ($ im, 255, 0, 0 );
$ Dnline = ImageColorAllocate ($ im, 0,175,175 );
$ D5line = ImageColorAllocate ($ im, 255,127, 0 );
$ D20line = ImageColorAllocate ($ im, 127 );
$ D10line = ImageColorAllocate ($ im, 255 );
// Define the colors used to draw various objects first.

For ($ I = 20; $ I <=220; $ I + = 25)
ImageLine ($ im, 60, $ I, 560, $ I, $ gird );
For ($ j = 60; $ j <= 560; $ j + = 25)
ImageLine ($ im, $ j, 20, $ j, 220, $ gird );
// Calculate the position and grid width in advance, and draw lines through the for loop, saving you a lot of trouble.

$ Zzg = 10.55;
$ Zzd = 7.63;
USD lzg = 10350;
// Assume the stock market data,
// $ Zzg is the highest price for the period to be analyzed, for example, 10.55 yuan.
// $ Zzd is the lowest price for the period to be analyzed. Assume It is 7.63 RMB.
// $ Lzg is the highest transaction volume during the period to be analyzed. Assume It is 10350 hands.
// This is an important data used to calculate the "scale" of the coordinate grid.
$ Bl = $ zzg-$ zzd;
// The difference between the highest price and the lowest price. According to the ratio between it and the total height of the grid,
// You can obtain the position of the actual price in the grid.

For ($ I = 1; $ I <= 7; $ I ++)
{
$ Y = $ I * 25-10;
// Calculate the appropriate height (ordinate) of the scale based on the position of the grid lines ).
$ Str = Number_Format ($ zzg-($ i-1)/6 * $ bl, 2 ,".",",");
// Calculate the price of each dial and format the string.
$ X = 55-ImageFontWidth (2) * StrLen ($ str );
// Calculate the appropriate abscissa Based on the width of the string to be occupied.
ImageString ($ im, 2, $ x, $ y, $ str, $ data );
// Write this 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 volumes, it is not worthwhile to use the cycle.
// Use a loop if the number is large.


// Because a K-line chart needs to draw countless small K-line columns, write a small K-line column as a function.
Function kline ($ img, $ kp, $ zg, $ zd, $ sp, $ cjl, $ ii)
// Parameter: $ img image; $ kp $ zg $ zd $ sp indicates opening, highest, lowest, and closing;
// $ Cjl transaction volume; $ ii counter, indicating the serial number of the K-line column.
{
Global $ bl, $ zzd, $ lzg;
// Declare the $ bl, $ zzd, and $ lzg variables used in this function as global variables.
$ H = 150; // The height of the k bar area is 150.
$ Hh = 200; // the total height of the K-line column and transaction volume column is 200.

If ($ sp <$ kp)
$ Linecolor = ImageColorAllocate ($ img, 0,175,175 );
// If the closing price is lower than the opening price, it is a overcast line, use blue
Else
$ Linecolor = ImageColorAllocate ($ img, 255, 0, 0 );
// Otherwise, it is a yellow line, in red.

$ X = 58 + $ ii * 4;
// Calculate the abscissa Based on the serial number of the K-line column.
$ Y1 = 20 + $ h-($ kp-$ zzd)/$ bl * $ h;
// Calculate the corresponding ordinate based on the open sequence.
$ Y2 = 20 + $ h-($ sp-$ zzd)/$ bl * $ h;
// Calculate the corresponding ordinate Based on the closing price.
$ Y3 = 20 + $ h-($ zg-$ zzd)/$ bl * $ h;
// Calculate the corresponding ordinate value based on the highest price.
$ Y4 = 20 + $ h-($ zd-$ zzd)/$ bl * $ h;
// Calculate the corresponding ordinate value based on the lowest price.
$ Y5 = 20 + $ hh-$ cjl/$ lzg * ($ hh-$ h );
// Calculate the corresponding ordinate Based on the transaction volume.

If ($ y1 <= $ y2) ImageFilledRectangle ($ img, $ X-1, $ y1, $ x + 1, $ y2, $ linecolor );
Else ImageFilledRectangle ($ img, $ X-1, $ y2, $ x + 1, $ y1, $ linecolor );
// Subtract 1 from the X-axis to 1, and the span is 3. That is, a small filled rectangle with a painting width of 3.
// The height and ordinate values are determined by the opening and closing prices.
// The test shows that this function must be written before the upper left vertex coordinate in the lower right vertex coordinate,
// Instead of automatically determining the upper left and lower right of the two points.

ImageFilledRectangle ($ img, $ X-1, $ y5, $ x + 1,220, $ linecolor );
// Draws the transaction volume Column Based on the transaction volume.
ImageLine ($ img, $ x, $ y3, $ x, $ y4, $ linecolor );
// Draw a Shadow Line at the highest and lowest prices.

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.