PHP-based ultra-Cool HTML5 interactive chart

Source: Internet
Author: User
Tags javascript array

PHP-based ultra-Cool HTML5 interactive chart

Use PHP to implement professional-level Web-based charts without in-depth HTML5 and JavaScript knowledge.

Introduction

Recently, I need to quickly create charts from a group of PHP Data groups. The required charts must be interactive, user-friendly, and downloadable. After evaluating some PHP chart solutions, including phpChart, pChart, and Highcharts, I decided to use phpChart as my preferred tool.

Background

As a major backend programmer, I don't have much time to study using JavaScript to see charts online within 24 hours.) I don't have any advanced front-end coding knowledge. Basically, I want PHP developers who have almost no front-end programming experience to quickly develop exquisite charts.

I tried pChart, a popular PHP chart library. The generated chart looks pretty good. Although it can be downloaded, it is still a static image. Highcharts seems to be the best choice. A chart looks amazing, it is animated, and has many custom options, but it is also very complicated and requires a lot of JavaScript knowledge. Highcharts is neither designed for PHP nor free for business purposes.

Basic elements of phpChart

What I like most about phpChart is that it allows people to start with simple and minimal code.

PhpChart Lite version can be downloaded from http: // phpchArt.org/downLoads /. Download the file and decompress it to the root directory of the website.

Set conf. php

The first thing we need to do is set variables.SCRIPTPATHTo the PhpChart class library in the conf. php file. This variable represents the relative or absolute URL of the phpChart library on your Web server.

 
 
  1. define('SCRIPTPATH','/phpChart/'); 

Create the simplest chart

 
 
  1. Include the PHP header file conf. php:
  2. Require_once ("../conf. php ");

Call ConstructorC_PhpChartXAnd call the draw () function.

 
 
  1. $pc=new C_PhpChartX(array(array(123, 34, 51, 22, 3)), ‘simplest_graph’); 
  2. $pc->draw(); 

This is the code you need. The following figure shows the rendered output.

This is what I call the minimum number of codes. When you have a team of programmers to work, it doesn't make sense to study the basics. One thing that any programmer wants to do as soon as possible is to study complex documents from a new set of libraries or tools.

By the way, the name of the second parameter in the constructor should be unique for your chart. I typed "simplest_graph" or any non-space string. It must be a unique value so that you can have multiple charts on a page.

Add title

You should add titles for your charts so that users can know what they are watching.

 
 
  1. $pc->set_title(array('text'=>’My Simplest Graph')); 

Add an animation

One thing pChart cannot do is animation. In phpChart, animations can be used simply by calling set_animate and passing the true value.

 
 
  1. $pc->set_animate(true); 

That's it. At this time, your chart should already have titles and animations. The complete code is as follows:

 
 
  1. $pc = new C_PhpChartX(array(array(123, 34, 51, 22, 3)),'simplest_graph'); 
  2. $pc->set_animate(true); 
  3. $pc->set_title(array('text'=>'My Simplest Graph')); 
  4. $pc->draw(); 

Code Essence

If you view the source in the browser, you will find that phpChart automatically contains many JavaScript and CSS files, including jquery. js, jquery-ui, and jqplot.js、jquery-ui.css. Although the chart is rendered in the browser using the client's JavaScript, all the code at the front end is PHP.

The reason for popularity is that JavaScript is no longer needed for me, a PHP developer, because phpChart will automatically process it for me. The following is all the JavaScript code generated when you view the source code-the result of the previous four lines of PHP code.

 
 
  1. <script language="JavaScript" type="text/javascript"> 
  2.      var _simplest_graph_plot_properties; 
  3.     $(document).ready(function(){ 
  4.     setTimeout( function() { 
  5.     _simplest_graph_plot_properties = { 
  6.     "title":{ 
  7.     "text":"My Simplest Graph","show":1 
  8.     },"animate":true,"animateReplot":true 
  9.     } 
  10.  
  11.     $.jqplot.config.enablePlugins = true; 
  12.     $.jqplot.config.defaultHeight = 300; 
  13.     $.jqplot.config.defaultWidth = 400; 
  14.     _simplest_graph= $.jqplot("simplest_graph", 
  15.            [[123, 34, 51, 22, 3]], _simplest_graph_plot_properties); 
  16.  
  17.     }, 200 ); 
  18.     }); 
  19. </script> 

As you may also notice,"simplest_graphUsed as part of JavaScript variables, such_simplest_graph_plot_propertiesJqplot object. This is the only reason why the name I mentioned above must be unique.

In addition, the PHP Data Group is automatically converted to a JavaScript array, so the following PHP array:

 
 
  1. array(array(123, 34, 51, 22, 3)) 

Become a JavaScript array:

 
 
  1. [[123,34,51,22,3]] 

Change the Renderer type

PhpChart supports bar chart, line chart, and stack chart. It supports bar chart, block chart, bubble chart, candle chart, gecko chart, meter chart, and other types of charts. Renderer support:

  • BarRenderer

  • BezierCurveRenderer

  • BlockRenderer

  • BubbleRenderer

  • CanvasAxisLabelRenderer

  • CanvasAxisTickRenderer

  • CategoryAxisRenderer

  • DateAxisRenderer

  • DonutRenderer

  • EnhancedLegendRenderer

  • FunnelRenderer

  • LogAxisRenderer

  • MekkoAxisRenderer

  • MekkoRenderer

  • MeterGaugeRenderer

  • OHLCRenderer

  • PyramidAxisRenderer

  • PieRenderer

If you do not specify a type, the default chart type is a line chart. To change the chart type, call the set_series_default function. For example, change the preceding example to a pie chart.

 
 
  1. $pc->set_series_default(array('renderer'=>'plugin::PieRenderer')); 

Note that I use phpChart Enterprise Edition. PhpChart Lite version only supports line charts.

Array and naming conventions

There are also some noteworthy points. First, almost all parameters used in the phpChart function are an array, not all, but almost all. You only need to remember this to avoid a lot of headaches during debugging. I will briefly cover the debugging function later ). Second, the Renderer is called a "plug-in" in phpChart. Therefore, you must pass "plugin: PieRenderer" in the same way, with a double colon in the middle. For custom JavaScript, use "js: yourJavascriptFunctioName ".

Advanced phpChart: Custom JavaScript

So far, all I have shown are PHP. In most cases, phpChart can be fully used for simple PHP function calls. To make full use of phpChart, you may want to use custom JavaScript. For example, you can use phpChart to load data from JavaScript Functions and external sources.

The followingsineRendererIs a custom JavaScript function that defines the display of sine values from a group of random numbers. Then it is passed to the set_data_renderer function.

PHP:

 
 
  1. $data1 = array(); 
  2. $pc = new C_PhpChartX(array($data1),'basic_chart_4'); 
  3. $pc->set_title(array('text'=>'Basic Chart with Custom JS')); 
  4. $pc->set_data_renderer("js::sineRenderer"); 
  5. $pc->add_plugins(array('pointLabels')); 
  6. $pc->set_animate(true); 
  7. $pc->draw(); 
  8.  
  9. JavaScript: 
  10.  
  11. sineRenderer = function() { 
  12.     var data = [[]]; 
  13.     for (var i=0; i<13; i+=0.5) { 
  14.       data[0].push([i, Math.sin(i)]); 
  15.     } 
  16.     return data; 
  17.   }; 

To learn more about the set_data_renderer function, click here: http://phpchart.org/phpChart/docs/output/C_PhpChartX_set_data_renderer@.html

Export charts to images

At the beginning, I was troubled by this because I did not know how to export charts. It turns out that phpChart charts can be exported as Downloadable images, but this process is not well documented. I found that by adding the following code to the bottom of all pages, you can turn around:

 
 
  1. <script type="text/javascript" 
  2.   src="http://www.codeproject.com/phpChart/js/showjs.js"></script> 

Download showjs. js: http: // phpchArt.org/phpCHart/js/showJs. js

Debug phpChart

Finally, before the conclusion, I would like to mention a very valuable feature of phpChart. That is, its built-in debugging function. On its website, all online example http: // phpchArt.org/examPles/) There are debugging options to enable vivid demos and two clear tabs for easy porting of the following code-JavaScript and PHP.

To enable debugging, add the following code line to the conf. php file:

 
 
  1. define('DEBUG', true); 

Last thought

One of the main advantages of PhpChart is that PHP programmers can use this tool to implement professional-level Web-based charts without having to gain a deep understanding of HTML5 and JavaScript.

If you are a layman in front-end programming like me, but you also need to generate interactive Web charts, you may like phpChart. The HTML5 chart examples of phpChart are listed on the following page. If you are lucky, you may not need a document-you can understand the Code directly.

Related Article

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.