Create a Cartesian chart source code example using jpgraph

Source: Internet
Author: User
After introducing most phpjpgraph installation and configuration tutorials, we will begin to introduce how to use jpgraph to draw charts, in the previous jpgraph tutorial, I have introduced how to use jpgraph to create a line chart and a column chart instance, this tutorial will first introduce the two most common chart types. after introducing the installation and configuration of most php jpgraph, we will begin to introduce how to use jpgraph to draw charts, in the previous jpgraph tutorial, I have introduced how to use jpgraph to create a line chart and a column chart instance, this tutorial will first introduce two examples of the most common chart types: Cartesian charts and Cartesian charts in pie charts. This tutorial describes common features of all charts and describes the structure of typical chart scripts. By reading this section, users of jpgraph will have a basic understanding of creating simple chart scripts, and understand what the core settings and naming conventions are.

All the following jpgraph chart scripts have the same structure to some extent. All scripts must first create a Graph class instance. The Graph class represents the entire chart. you can use a series of data to build one or more graphs (models ). Almost all functions used to control the chart appearance are of this class.

To get a glimpse of commonly used jpgraph objects, we will display the most commonly used Chart type instances, Cartesian charts.

Common Objects of Cartesian charts (X, Y)


Use jpgraph to draw Cartesian charts


This section describes the meaning of the functions used to draw a chart. For more information, see the code for using jpgraph to draw a chart.

The jpgraph class library documentation stipulates that all Graph class instances are stored in a variable named $ graph. To illustrate some common instance variables, the objects manipulated by a basic chart in the script are displayed.

The code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Require_once ('jpgraph/jpgraph. php ');
Require_once ('jpgraph/jpgraph_line.php ');
 
$ Ydata = array );
$ Y2data = array (354,200,265, 99,111, 91,198,225,293,251 );
 
// Create a chart and specify the scale types of X and y axes.
$ Width = 550; $ height = 400;
$ Graph = new Graph (550,400 );
$ Graph-> SetScale ('textline ');
$ Graph-> SetY2Scale ('line ');
$ Graph-> SetShadow ();
 
// Adjust the image white space size
$ Graph-> SetMargin (50,150, 60, 80 );
 
// Create two linear graphs
$ Lineplot = new LinePlot ($ ydata );
$ Lineplot2 = new LinePlot ($ y2data );
 
// Add the image to the chart
$ Graph-> Add ($ lineplot );
$ Graph-> AddY2 ($ lineplot2 );
$ Lineplot2-> SetColor ('Orange ');
$ Lineplot2-> SetWeight (2 );
 
// Adjust the axis color
$ Graph-> y2axis-> SetColor ('dark ');
$ Graph-> yaxis-> SetColor ('blue ');
 
$ Graph-> title-> SetFont (FF_ARIAL, FS_BOLD, 14 );
$ Graph-> title-> Set ('using JpGraph Library ');
$ Graph-> title-> SetMargin (10 );
 
$ Graph-> subtitle-> SetFont (FF_ARIAL, FS_BOLD, 10 );
$ Graph-> subtitle-> Set ('(common objects )');
 
$ Graph-> xaxis-> title-> SetFont (FF_ARIAL, FS_BOLD, 10 );
$ Graph-> xaxis-> title-> Set ('x-title ');
$ Graph-> yaxis-> title-> SetFont (FF_ARIAL, FS_BOLD, 10 );
$ Graph-> yaxis-> title-> Set ('Y-title ');
 
// Set the model color
$ Lineplot-> SetColor ('Blue ');
$ Lineplot-> SetWeight (2 );
$ Lineplot2-> SetColor ('dark ');
$ Lineplot2-> SetWeight (2 );
 
// Set the text description of the model
$ Lineplot-> SetLegend ('plot 1 ');
$ Lineplot2-> SetLegend ('plot 2 ');
 
// Adjust its location
$ Graph-> legend-> SetPos (0.05, 0.5, 'right', 'center ');
 
// Display charts
$ Graph-> Stroke ();
?>

  Jpgraph source code description

1. all charts start with the require_once statement that contains one or more class library files. the specific directory is related to the jpgraph installation address.

2. create the data required for the y axis, and draw a line chart after one side.

3. all graph scripts of the 2D effect line type are called using the following two methods:

$ Graph = new Graph ($ width, $ height );
$ Graph-> SetScale ('... ');

  SetScale functionIt is mainly used to specify the scale types of X and Y axes, mainly including linear (lin), used in X and y axes, log, used in X and y axes, text (text ), it can only be used on the X axis; an integer (int) can be used on the X and y axes.

  SetY2Scale functionIt is mainly used to specify the scale type of the second y axis.

  SetShadow functionIt mainly adds shadow effects to charts.

The preceding two call statements create a necessary Graph class instance, which represents the entire chart and specifies the scale used by the X and y axes. Because the second extract is used in the above instance, you need to use a script to call $ graph-> SetY2Scale ('... ');

4. most charts need to adjust the left and right margins. this is what the SetMargin function means. That is, $ graph-> SetMargin ($ left, $ right, $ top, $ bottom );

5. create two line chart instances based on the previous data and place them in the chart. the default line color is blue. because the chart has two lines, therefore, you need to adjust the color and width of the second line.

6. all text objects are examples of common text classes than the headers of tables and axes. This means that the text, font, and color are specified in the same way, that is

$ Graph-> title-> SetFont (FF_ARIAL, FS_BOLD, 14 );

$ Graph-> title-> Set ("Using JpGraph Library ");

$ Graph-> title-> SetMargin (10 );

$ Graph-> subtitle-> SetFont (FF_ARIAL, FS_BOLD, 10 );

$ Graph-> subtitle-> Set ('(common objects )');

The method for setting the axis title is similar

$ Graph-> xaxis-> title-> SetFont (FF_ARIAL, FS_BOLD, 10 );

$ Graph-> xaxis-> title-> Set ("X-title ");

$ Graph-> yaxis-> title-> SetFont (FF_ARIAL, FS_BOLD, 10 );

$ Graph-> yaxis-> title-> Set ("Y-title ");

For details about jpgraph text and font control, refer to: jpgraph Chinese user manual text and font control tutorial

7. call Stroke () or its variant functions StrokeCSIM () and StrokeCSIMIMage () at the end of all icons to send the constructed images back to the browser.

  How to use jpgraph to draw charts

Through this jpgraph instance, we can see the general idea of using jpgraph to draw charts is as follows:

1. introduce the jpgraph class library and graphic model files to be drawn.

2. create a Graph instance, which is the representative of the entire chart. similar to the canvas concept in PS, all the graphs are drawn on it and the scale type of X and y axes is developed.

3. to make the chart more beautiful, you can adjust the margins between the left and right sides of the chart.

4. create a graph instance based on the relevant data and place it in the chart.

5. set the title of the entire chart, the title of various images, the text description of X and y axes, and the color and font.

6. finally, output the chart to the browser for display.

Now, the examples of using jpgraph to draw Cartesian charts are described. I believe that the above jpgraph source code will help you understand the idea of using jpgraph to draw charts.

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.