Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 转帖:一分钟教会你用google图表中的曲线图和柱状图

转帖:一分钟教会你用google图表中的曲线图和柱状图

Jun 23, 2016 pm 02:08 PM


原文地址: http://2sitebbs.com/thread-671-1-1.html


毋庸置疑谷歌的图表api是非常棒的,
又非常稳定的且灵活的图表解决‘方案。

下面介绍一个简单的例子,
旨在为需要用到google图表尤其是它的曲线图(LineChart)和柱状图(ColChart)的朋友快速集成提供帮助。

迅速集成google图表的曲线图或柱状图的详细步骤如下:

1、第一步:把google的javascript api文件引入到你的网页中来;
在网页的html代码中加入如下代码:
<script></script>
复制代码
注意:
此文件需在你的调用google图表的js代码前面引入。

2、第二步:引入曲线图和柱状图js封装函数代码到你的网页中来;
直接引入外部js文件:
<script></script>
复制代码

或者把下面这个简单的封装函数加到你的js代码中去:
/**
* functions for chart
* --参数说明--
* sId: 用来展示google图标的标签的id名
* sType: 图表的类型,曲线图或者柱状图;可选值:col 和 line
* sTitle: 图表的标题
* oData: 图表数据,是个二维数组,第一个元素是x轴和y轴的标题,格式如:[['x轴数据标题', 'y轴数据标题'], ['xValue 1', 'yValue 1'], ...]
* max: y轴高度的最大值
*/
function showChart(sId, sType, sTitle, oData, max) {
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(function () {
        var data = google.visualization.arrayToDataTable(oData);

        var options = {
            title: sTitle,
            width: 720,
            height: 200,
            chartArea: {left: 30, width: '98%', top: 25, height: '75%'},
            titleTextStyle: {color:'#666', fontSize: '14px'},
            curveType: "function",
            vAxis: {maxValue: max}
        };

        if (sType == 'line') {
            var chart = new google.visualization.LineChart(document.getElementById(sId));
        }else if (sType == 'col'){
            var chart = new google.visualization.ColumnChart(document.getElementById(sId));
        }
        chart.draw(data, options);
    });
}
复制代码

3、第三步:以php为例,拼装图表数据;
示例代码:
//给图表设置x轴和y轴的数据标题
$oDataCity = array(array('City', '均价/万'));
//$citys是一个从db查询出来的保存了多个城市数据的数组,它其中包含cityName和price两个字段
foreach ($citys as $key => $val) {
    $oDataCity[] = array($val['cityName'], $val['price']);
}
复制代码

4、第四步:把数据输出到js代码中,调用showChart()函数显示图表;
示例代码:
//准备参数
$sData = json_encode($oDataCity);
$chartType = 'col';
$chartTitle = '全国各大城市二手车热销品牌排行';
$maxXValue = 100;

//输出调用显示图表函数的js代码
echo  <script> <br /> (function(){ <br /> var sId = 'gchart', <br /> sType = '{$chartType}', <br /> sTitle = '{$chartTitle}'; <br /> var oData = eval('({$sData})'); <br /> showChart(sId, sType, sTitle, oData, {$maxXValue}); <br /> })(); <br /> </script>
eof;
复制代码

简单四步就把google的柱状图集成到你的网页中了。

两个在线的示例:
柱状图在线示例: http://www.ruralusedcar.com/%E5%AE%9D%E9%A9%AC/#h1
曲线图在线示例: http://www.ruralusedcar.com/%E5%AE%9D%E9%A9%AC/city-%E6%B7%B1%E5%9C%B3/#h1

----欢迎转载,转载请注明原创出处,谢谢~----


回复讨论(解决方案)

很好的文章学习了

不错,收下了

https://www.google.com.hk/jsapi

https://www.google.com.hk/jsapi
引用之后页面打开速度很慢,怎么解?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles