Structure design and application example of PHP drawing object

Source: Internet
Author: User
Object | design | Application example This article mainly expounds how to realize the structure design of drawing object in PHP.

In PHP, the small and medium data structures can be implemented by simple classes, which are defined by simple data and operations. However, with the complication of data structure, the simple class cannot satisfy the requirement of the whole data structure, so the structure of the class needs to be used, and the member in the class is also composed of the class. This is the object-oriented design method to achieve data encapsulation, information hiding and code reuse, and another way is the inheritance of class.
This article mainly discusses the structure of classes.

Take the drawing as an example, let's look at a simple example of geometry.
The geometry consists of a series of points, lines, planes, and so on, which are known as the most basic constituent elements by some geometrical axioms. Therefore, we use the point as the basic object of design, thus describing the line and surface objects. Used to illustrate how the class structure is implemented.

Analysis
Assuming that the point is in a plane, after the coordinate system has been determined (that is, given the origin and the axis and its positive direction), we can use the coordinate position to describe the point, as shown in the figure:

The line is determined by two points, so a line segment can be determined by starting and terminating two points. As shown in figure:

Face--(may be an example) rectangle, consisting of two parallel pairs of edges, and adjacent sides of the angle of 90 degrees. When the drawing is found, the rectangle can be uniquely determined by two points diagonally. As shown in figure:

Based on the above analysis, we began to design: Point class, line class, Surface class. The data members in the line and face class contain the point class, which is the structure of the class, which makes up a complex class of simple classes.
The design is as follows:

From another perspective, if you do not use the structure design method of a class, a class with no hierarchy might be like this, take line as an example:

A simple class where the idea is that a member variable represents the coordinates (X1,Y1) of the two points that determine the segment (X2,Y2).

Comparing the two designs, Line_bad brings the result of maintenance and change inconvenience because there is no hierarchy. The first need to maintain more than four member variables, if you need to set the color of the midpoint of the segment, Line_bad need to increase the member function, if you need to change the line to three-dimensional space, Line_bad will need to increase the member variable, and then modify all the member functions. With line class to avoid such trouble, set the color only call point method can be completed, and change to three-dimensional space, just add point to the properties (z coordinates) on it. The line class itself almost does not have to make any changes (and PHP drawing function related), save a lot of maintenance modification Strength Bar:).



Now that the foundation of the drawing is available, let's give a practical example.

In many statistics often need to use the legend to display all kinds of statistical results, we use the above design of the drawing class to achieve the display of the bar.

Analysis:
Columnar graphs are mainly composed of axes and columns. The axes can be displayed by the line class, and the column can be displayed by the rectangle class.

Display axes as follows:


function Drawaxes ()
{
$vertspan = 200;

$p 1=new Point ($vertspan +2.5);
$p 2=new Point (10,2.5);

$VerticalAxis =new Line ($p 1, $p 2);
$VerticalAxis->draw ();

$p 1=new Point (7.5, $vertspan);
$p 2=new Point (370, $vertspan);

$HorizontalAxis =new Line ($p 1, $p 2);
$HorizontalAxis->draw ();

}





The column body is displayed as follows:
function Plot ($arr _bartotal)
{
$vertspan = 200;
Show scale
$scaleHeight = 190;
Determining the maximum value in a column
$maxTotal =max1 (& $arr _bartotal);
Determine the number of column bodies
$total =count ($arr _bartotal);

Loop to show the columnar body
$DX = 15;
$x =10+ $DX;

for ($i =0; $i < $total; $i + +)
{
$rectHeight = ($arr _bartotal[$i]/$maxTotal) * $scaleHeight;

$p 1=new Point ($x, $vertspan-$rectHeight);
$p 2=new Point ($x + $DX, $vertspan);

$CurrRect = new Rectangle ($p 1, $p 2);
$CurrRect->draw ();

$x +=2.0* $DX;
}
}


Main program:
1) determine the display data.
2) Displays the axes.
3) display the columnar body.


Main
$arr _bartotal=array (' 1 ', ' 3 ', ' 34 ', ' 23 ', ' 5 ', ' 25 ', ' 7 '); Display data
Drawaxes (); Draw axes
Plot ($arr _bartotal); Draw a columnar body


So the main program framework is complete: D
The effect is as follows.

To further beautify the work, you can add the underlying display class of the corresponding properties and the corresponding member functions, here is not more.

Attached: Because of the space problem, there is no source code attached.
All the code in this article is available in http://10.31.172.7/cgi-bin/cvsweb.cgi/kai/graph/and has been debugged in the php4.0.2+gd1.62 environment.


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.