Skip to content

Instantly share code, notes, and snippets.

@ahomu
Created February 7, 2011 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahomu/814085 to your computer and use it in GitHub Desktop.
Save ahomu/814085 to your computer and use it in GitHub Desktop.
Japan Map Chart (using aka Chart API)
<?php
// http://code.google.com/intl/ja/apis/chart/docs/gallery/new_map_charts.html
// detectColorメソッドを実装して,値に対して色(16進数)を割り振る
abstract class JapanMapChart
{
private
$_baseUrl,
$_params,
$_glue;
/**
* __construct
*
* @return void
*/
public function __construct($x=null, $y=null, $data=array())
{
$this->_baseUrl = 'http://chart.apis.google.com/chart?';
$this->_params = array(
'cht' => 'map'
);
$this->_glue = array(
'chld' => '|',
'ch' => ',',
'chco' => '|'
);
if ( $x !== null && $y !== null ) {
$this->setSize($x, $y);
}
if ( !empty($data) ) {
$this->setAll($data);
}
}
/**
* detectColor
*
* @abstract
* @param mixed $val
* @return string $hexColor
*/
abstract public function detectColor($val);
/**
* setSize (chs)
* http://code.google.com/intl/ja/apis/chart/docs/chart_params.html#gcharts_chs
*
* @param int $x
* @param int $y
* @return void
*/
public function setSize($x, $y)
{
$size = $x*$y;
if ( 1
and $x <= 600
and $y <= 600
and $size < 300000
) {
$this->_params['chs'] = $x.'x'.$y;
} else {
trigger_error("The parameter 'chs={$x}x{$y}' specifies a chart with $size pixels, which exceeds the maximum size of 300000 pixels.", E_USER_ERROR);
}
}
/**
* _setPrefecture (chld)
* http://en.wikipedia.org/wiki/Prefectures_of_Japan (ISO 3166-2:JP)
*
* @param int|string $pref
* @param int $fix
* @return void
*/
private function _setPrefecture($pref, $fix = 0)
{
$prefCode = strpos($pref, 'JP-') === 0 ? $pref : sprintf('JP-%02d', ($pref - $fix));
$this->_params['chld'][] = $prefCode;
}
/**
* _setColor (chco)
*
* @param mixed $val
* @return void
*/
private function _setColor($val)
{
$this->_params['chco'][] = $this->detectColor($val);
}
/**
* set
*
* @param int|string $prefCode
* @param mixed $val
* @return void
*/
public function set($pref, $val)
{
$this->_setPrefecture($pref, 10);
$this->_setColor($val);
}
/**
* setAll
*
* @param array $data
* @return void
*/
public function setAll($data)
{
foreach ( $data as $prefCode => $val ) {
$this->set($prefCode, $val);
}
}
/**
* build
*
* @return string $comleteUrl
*/
public function build()
{
$completeParams = array();
foreach ( $this->_params as $key => $val ) {
if ( is_array($val) ) {
$val = implode($this->_glue[$key], $val);
}
$completeParams[$key] = $val;
}
return $this->_baseUrl.http_build_query($completeParams);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment