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.