Mapa de ventas con la biblioteca FusionCharts
1- Crear una aplicación del tipo blank.
2- Procedemos a crear una librería externa
3- Cree una nueva biblioteca a nivel de proyecto, llamada “fusioncharts_sample” (1). Haga clic en el botón “Usar biblioteca” (2), guárdelo en el proyecto (3) y luego haga clic en la opción para “editar” la biblioteca (4).
4- Al editar la biblioteca, crearemos dos archivos, uno .js y otro .php
maps.php
<?php
function getChartSQL()
{
//fusionchart key
//label
//chart type map
//the value itself
if(isset($_GET['map']) && isset($_GET['region']) && isset($_GET['country']))
{
$str_sql = "
SELECT
states.fs_id,
states.statename,
states.statename,
SUM(orders.priceorder)
FROM
orders INNER JOIN customers ON orders.customerid = customers.customerid
INNER JOIN states ON customers.stateid = states.stateid
INNER JOIN country ON customers.country = country.iso
INNER JOIN country_region ON country.country_region = country_region.regionid
WHERE
country_region.short_label = \"". str_replace('"', "\"", $_GET['region']) . "\" AND
country.fs_id = \"". str_replace('"', "\"", $_GET['country']) . "\"
GROUP BY
states.stateid,
states.statename,
states.statename
";
}
elseif(isset($_GET['map']) && isset($_GET['region']))
{
$str_sql = "
SELECT
country.fs_id,
country.printable_name,
country.fs_map,
SUM(orders.priceorder)
FROM
orders INNER JOIN customers ON orders.customerid = customers.customerid
INNER JOIN states ON customers.stateid = states.stateid
INNER JOIN country ON customers.country = country.iso
INNER JOIN country_region ON country.country_region = country_region.regionid
WHERE
country_region.short_label = \"". str_replace('"', "\"", $_GET['region']) . "\"
GROUP BY
country.fs_id,
country.printable_name,
country.fs_map
";
}
else
{
$str_sql = "
SELECT
country_region.short_label,
country_region.label,
country_region.fs_map,
SUM(orders.priceorder)
FROM
orders INNER JOIN customers ON orders.customerid = customers.customerid
INNER JOIN states ON customers.stateid = states.stateid
INNER JOIN country ON customers.country = country.iso
INNER JOIN country_region ON country.country_region = country_region.regionid
GROUP BY
country_region.short_label,
country_region.label,
country_region.fs_map
";
}
return $str_sql;
}
function getChartMapType()
{
if(isset($_GET['map']))
{
return $_GET['map'];
}
else
{
return "world";
}
}
function getChartData($rs)
{
$arr_data = array();
$arr_data['min'] = 0;
$arr_data['max'] = 0;
$arr_data['data'] = "";
foreach($rs as $row)
{
$link = "";
if(isset($_GET['map']) && isset($_GET['region']) && isset($_GET['country']))
{
}
elseif(isset($_GET['map']) && isset($_GET['region']))
{
$link = "?map=". $row[2] ."®ion=" . $_GET['region'] ."&country=" . $row[0] . "&last_map=" . $_GET['map'];
}
else
{
$link = "?map=". $row[2] ."®ion=" . $row[0];
}
if($row[3] > $arr_data['max'])
{
$arr_data['max'] = $row[3];
}
$arr_data['data'] .= '{
"id": "'. $row[0] .'",
"displayValue": "'. $row[1] .'",
"showLabel": "1",
"value": "'. $row[3] .'",
"link": "'. $link .'",
},';
}
return $arr_data;
}
function getHtmlBackButton($strLang)
{
if(isset($_GET['last_map']) && isset($_GET['region']) && isset($_GET['country']))
{
return "<a id='back' href='?map=". $_GET['last_map'] ."®ion=". $_GET['region'] ."'>". $strLang ."</a>";
}
elseif(isset($_GET['map']) && isset($_GET['region']))
{
return "<a id='back' href='?'>". $strLang ."</a>";
}
else
{
return "";
}
}
maps.js
<?php
function getChartSQL()
{
//fusionchart key
//label
//chart type map
//the value itself
if(isset($_GET['map']) && isset($_GET['region']) && isset($_GET['country']))
{
$str_sql = "
SELECT
states.fs_id,
states.statename,
states.statename,
SUM(orders.priceorder)
FROM
orders INNER JOIN customers ON orders.customerid = customers.customerid
INNER JOIN states ON customers.stateid = states.stateid
INNER JOIN country ON customers.country = country.iso
INNER JOIN country_region ON country.country_region = country_region.regionid
WHERE
country_region.short_label = \"". str_replace('"', "\"", $_GET['region']) . "\" AND
country.fs_id = \"". str_replace('"', "\"", $_GET['country']) . "\"
GROUP BY
states.stateid,
states.statename,
states.statename
";
}
elseif(isset($_GET['map']) && isset($_GET['region']))
{
$str_sql = "
SELECT
country.fs_id,
country.printable_name,
country.fs_map,
SUM(orders.priceorder)
FROM
orders INNER JOIN customers ON orders.customerid = customers.customerid
INNER JOIN states ON customers.stateid = states.stateid
INNER JOIN country ON customers.country = country.iso
INNER JOIN country_region ON country.country_region = country_region.regionid
WHERE
country_region.short_label = \"". str_replace('"', "\"", $_GET['region']) . "\"
GROUP BY
country.fs_id,
country.printable_name,
country.fs_map
";
}
else
{
$str_sql = "
SELECT
country_region.short_label,
country_region.label,
country_region.fs_map,
SUM(orders.priceorder)
FROM
orders INNER JOIN customers ON orders.customerid = customers.customerid
INNER JOIN states ON customers.stateid = states.stateid
INNER JOIN country ON customers.country = country.iso
INNER JOIN country_region ON country.country_region = country_region.regionid
GROUP BY
country_region.short_label,
country_region.label,
country_region.fs_map
";
}
return $str_sql;
}
function getChartMapType()
{
if(isset($_GET['map']))
{
return $_GET['map'];
}
else
{
return "world";
}
}
function getChartData($rs)
{
$arr_data = array();
$arr_data['min'] = 0;
$arr_data['max'] = 0;
$arr_data['data'] = "";
foreach($rs as $row)
{
$link = "";
if(isset($_GET['map']) && isset($_GET['region']) && isset($_GET['country']))
{
}
elseif(isset($_GET['map']) && isset($_GET['region']))
{
$link = "?map=". $row[2] ."®ion=" . $_GET['region'] ."&country=" . $row[0] . "&last_map=" . $_GET['map'];
}
else
{
$link = "?map=". $row[2] ."®ion=" . $row[0];
}
if($row[3] > $arr_data['max'])
{
$arr_data['max'] = $row[3];
}
$arr_data['data'] .= '{
"id": "'. $row[0] .'",
"displayValue": "'. $row[1] .'",
"showLabel": "1",
"value": "'. $row[3] .'",
"link": "'. $link .'",
},';
}
return $arr_data;
}
function getHtmlBackButton($strLang)
{
if(isset($_GET['last_map']) && isset($_GET['region']) && isset($_GET['country']))
{
return "<a id='back' href='?map=". $_GET['last_map'] ."®ion=". $_GET['region'] ."'>". $strLang ."</a>";
}
elseif(isset($_GET['map']) && isset($_GET['region']))
{
return "<a id='back' href='?'>". $strLang ."</a>";
}
else
{
return "";
}
}
5- Después de guardar la biblioteca en el proyecto, podemos usar la macro sc_include_lib () para llamar a la biblioteca externa dentro del evento onExecute de la aplicación Blank.
6- Usamos el siguiente código para crear el mapa.
sc_include_library(“prj”, “fusioncharts_sample”, “maps.php”, true, true);
$str_sql = getChartSQL();
sc_lookup(rs, $str_sql);
$json_data = getChartData({rs});
?>
<?php echo {lang_maps_title}; ?>
sc_include_lib(“fusioncharts”);
“>
json_data = [ <?php echo $json_data[‘data’]; ?> ]; langs = [ “<?php echo {lang_maps_subs_low}; ?>”, “<?php echo {lang_maps_subs_avg}; ?>”, “<?php echo {lang_maps_subs_high}; ?>” ]; drawMap(‘<?php echo getChartMapType(); ?>’, “<?php echo {lang_maps_title}; ?>”, “<?php echo {lang_maps_subtitle}; ?>”, ‘<?php echo $json_data[‘min’]; ?>’, ‘<?php echo ($json_data[‘max’])/2; ?>’, ‘<?php echo $json_data[‘max’]; ?>’, json_data, langs);
<style> a#back{ border: 1px solid #e4d9c1; border-radius: 5px; padding: 5px 8px; position: fixed; right: 15px; top: 20px; background: #e4d9c1; *text-transform: uppercase; font-family: sans-serif; font-size: 12px; color: #555; text-decoration: none; font-weight: bold; } a#back:hover{ text-decoration: underline; } </style>
A world map will load here!
<?php
7- Finalmente, ejecutamos la aplicación.
Comment this post