Using Jpgraph to create a Cartesian chart Source example tutorial

Source: Internet
Author: User
Tags array php web development

After you've covered most of the PHP jpgraph Setup tutorials, you're about to start using jpgraph for charting, and in the previous Jpgraph tutorial I've covered how to create a line chart and a bar chart instance tutorial with Jpgraph. This tutorial will first introduce the two most commonly used chart type instances, Cartesian charts and Cartesian charts in pie charts to create instances. This tutorial describes the common features of all charts and details the structure of typical chart scripts. By reading this section, the Jpgraph user will have a basic understanding of creating a simple chart script, as well as understanding the core settings and what naming conventions are used.

All of the following Jpgraph chart scripts are, to some extent, the same structure. All scripts must first create a graph class instance. The graph class represents the entire chart, and you can build one or more diagrams (models) from a series of data. Almost all functions used to control the appearance of a chart are of this class.

To feel the usual jpgraph objects, we'll show you the most common examples of chart types, Cartesian charts.

Cartesian diagrams commonly used objects (X,Y)

Use Jpgraph to draw a Cartesian diagram description

The diagram above basically illustrates the meaning of the function that is used to draw the chart, and we can refer to the code for drawing the chart using jpgraph below.

Jpgraph Class Library Document conventions All graph class instances are stored in a variable named $graph. To illustrate some of the commonly used instance variables, the following illustration shows the objects that a basic chart in the script manipulates.

The entire code is as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
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
<?php
Require_once ("jpgraph/jpgraph.php");
Require_once ("jpgraph/jpgraph_line.php");

$ydata = Array (11,3,8,12,5,1,9,13,5,7);
$y 2data = Array (354,200,265,99,111,91,198,225,293,251);

Create a chart and set the scale types for x and y axes
$width =550; $height = 400;
$graph = new Graph (550,400);
$graph->setscale ("Textlin");
$graph->sety2scale ("Lin");
$graph->setshadow ();

Adjust the size of the graphic to leave White
$graph->setmargin (50,150,60,80);

Create two linear graphs
$lineplot =new Lineplot ($ydata);
$lineplot 2=new Lineplot ($y 2data);

Add a graphic to a chart
$graph->add ($lineplot);
$graph->addy2 ($lineplot 2);
$lineplot 2->setcolor ("Orange");
$lineplot 2->setweight (2);

Adjust the color of the axis
$graph->y2axis->setcolor ("darkred");
$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 color of the model
$lineplot->setcolor ("Blue");
$lineplot->setweight (2);
$lineplot 2->setcolor ("darkred");
$lineplot 2->setweight (2);

To set the text description of a model
$lineplot->setlegend ("Plot 1");
$lineplot 2->setlegend ("Plot 2");

Adjust its position
$graph->legend->setpos (0.05,0.5, "right", "center");

Show Chart
$graph->stroke ();
?>

  Jpgraph Source Code Description

1. All charts need to start with a require_once statement containing one or more class library files, and the specific directories included are related to the Jpgraph installation address.

2. Create the data needed for the Y axis, and then draw a specific line chart

3, 2D effect of the line class all chart scripts are called by the following two methods

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

  The Setscale function is primarily used to specify X, the scale type of the y-axis, mainly linear (Lin), is used on the x and y axes; logarithms (log), used on the x and y axes; text, only on the x-axis, integers (int), and on the X and Y axes.

  The Sety2scale function is primarily used to specify the scale type of the second y-axis.

  The Setshadow function mainly adds shadow effects to the chart.

These two invocation statements will create a necessary graph class instance that represents the entire chart and specifies what scale the x and Y axes should use. In the above example, the script call $graph->sety2scale (' ... ') is required because the second pump is used.

4, most charts need to adjust the left and right upper and lower margins, the setmargin function is this meaning. That is, $graph->setmargin ($left, $right, $top, $bottom);

5, and then based on the data given before the creation of two line chart instances and put them in the chart, the default line color is blue, because this figure has two polylines, you need to adjust the second line of color and thickness.

6, all text objects such as charts and axes of the title are commonly used instances of text classes, which means that the text, font, color designation is the same method, namely

$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) ');

Set the axis header method like

$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");

About jpgraph text and font control please refer to the Tutorial: text and Font control tutorials for jpgraph Chinese manual

7, finally, the end of all the icons need to call stroke () or its variant function Strokecsim (), Strokecsimimage (), to send the constructed picture back to the viewer.

  Summarize the ideas by using jpgraph to draw graphs

With this jpgraph example, we can see the general idea of drawing graphs using jpgraph as follows

1, the introduction of Jpgraph class Library and the need to draw graphics model files.

2. Create graph instance, which is the representative of the whole chart, similar to the canvas concept in PS, all the graphics are drawn on this, and make the scale type of x and Y axes.

3, in order to be beautiful, do not let the entire chart does not fill the graphics, you can adjust the graphics in the chart to draw the left and right margin.

4, according to the relevant data to create the need to draw a graphic instance and put it in the diagram.

5, then the entire chart, the various graphics of the title, X, y axis text description, as well as color, font settings.

6, the final drawing of the chart output to the viewer display.

So far, using jpgraph to draw the example of the Cartesian chart tutorial is finished, I believe that through the above Jpgraph source code instructions, will let you use Jpgraph to draw a diagram of ideas to understand.

  Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.



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.