highcharts Example Tutorial Two: Build a pie chart with PHP and MySQL

Source: Internet
Author: User
Tags php and mysql

Last time we analyzed the use of highcharts combined with PHP and MySQL to generate a line chart example, we use the Technical CTO website search engine traffic as an example of using Highcharts to generate pie chart.
Pie charts are often used when we need to visually display the proportions of each part, such as the amount of traffic we need to count the major search engines.
First step: Create a Database save PV numbers for each search engine traffic
CREATE TABLE ' Pie ' (
' id ' int (ten) is not NULL auto_increment,
' title ' varchar (+) not NULL,
' PV ' int (ten) is not NULL,
PRIMARY KEY (' id ')
) Engine=myisam auto_increment=7 DEFAULT Charset=utf8;

The second step: write PHP code to get data, converted to Highcharts can render the data format, Highcharts can parse data in JSON format, such as: [' Baidu ', 120], [' Google ', 86];
Include_once (' connect.php ');
$res = mysql_query ("SELECT * from pie");
while ($row = Mysql_fetch_array ($res)) {
This means that the data is highlighted by default, and then we can not write
if ($row [' id ']==1) {
$arr 1[] = Array (
"Name" = $row [' title '],
"Y" = intval ($row [' PV ']),
"Sliced" = true,
"Selected" = True
);
}else{
$arr [] = Array (
$row [' title '],intval ($row [' PV '])
);
}
}
Merging arrays
$arrs = Array_merge ($arr 1, $arr);
$data = Json_encode ($arrs);
The need to pay special attention to the number of words must be added intval conversion, or highcharts will not recognize;




This article is from the technical cto:http://www.jscto.net, reproduced please indicate the source.

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.