i am rendering data in a bar chart as a grouped data through php loops.
my data in js is as
var graphTarget12 = $("#myBarChartOdometer");
var barGraph = new Chart(graphTarget12, {
type: 'bar',
data: {
labels: 'Lahore,Ahmed',
datasets: [
{
label:'hi',
data:[320,100] },
{
label:'hi',
data:[120,200] },
]
}
});
My code in file is as follow
<script>
var graphTarget12 = $("#myBarChartOdometer");
var barGraph = new Chart(graphTarget12, {
type: 'bar',
data: {
<?php
$data = Odometer();
// echo count($data);
$name = array();
for($i=0;$i<count($data);$i++) {
$arrays = $data[$i];
array_push($name,$arrays['groupName']);
// print_r(count($meter));
// die;
$nameImplode = implode(',', $name);
}
?>
labels: <?php echo "'".$nameImplode."'"; ?>,
datasets: [
<?php
for($i=0;$i<count($data);$i++) {
$arrays = $data[$i];
$meter = $arrays['odometer'];
// print_r(count($meter));
// die;
$odoValue = array();
for($s=0;$s<count($meter);$s++)
{
$odo = $meter[$s];
array_push($odoValue,$odo['odometer']);
}
$implode = implode(',', $odoValue);
?>
{
label:'hi',
data:<?php echo "[".$implode."]";?>
},
<?php } ?>
]
}
});
</script>
My data format in php is as follow
$array = array(
array('groupName' => 'Lahore','odometer' => array(array('odometer' =>320),array('odometer' => 100))),
array('groupName' => 'Ahmed','odometer' => array(array('odometer' =>120),array('odometer' => 200)))
);
Please help me solve this.i spent 2days already and couldn't figure out solution.
if any other way please show me
Your label should be like array.
labels: ['Lahore','Ahmed'],
You've used like this
labels: 'Lahore,Ahmed',
Related
I need some help with a script, i have an ajax request that returns a GeoJSON
Images:
JSON Format
"id_map": "2",
"description": "AC1",
"geojson": {a GeoJSON coordinate }
"file": "AC1.geojson"
I can use AJAX JSON Leaflet (plugin( to create a polygon layer using the JSON value file ex (ac1.geojson) and point it up to a folder with the GeoJSON files (ex. geojson), but i have the same GeoJSON saved as a text variable in a database and i want to use it besides the file because of the risk of losing the files, so i recover it (i use GeoJson.io to validate the geojson and copy paste it in a column in my database) using a database connection and then i JSON encode it, but i am unable to use it. And I'm having problems with the format that comes out from the PHP.
$(document).ready(function() {
document.body.onload = function() {
var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 20,
minZoom: 13,
attribution: 'x'
}),
latlng = L.latLng(-32.0312422, -52.0917713);
var mymap = L.map('mapid', {
center: latlng,
zoom: 18,
layers: [tiles]
});
L.marker([-32.0312422, -52.0917713]).addTo(mymap);
function popUp(f, l) {
var out = [];
if (f.properties) {
for (key in f.properties) {
out.push(key + ": " + f.properties[key]);
}
l.bindPopup(out.join("<br />"));
}
}
var j_url = "somephp.php";
var results = $.ajax({
url: j_url,
dataType: 'json',
cache: false,
success: AjaxSucceeded,
error: AjaxFailed
});
function AjaxSucceeded(results) {
console.log("Data successfully loaded.");
alert("OK");
$.each(results, function(index, value) {
var geojsonTESTE = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-52.101760, -32.031909],
[-52.102275, -32.028598],
[-52.100794, -32.028435],
[-52.099206, -32.029053],
[-52.097554, -32.029362],
[-52.097511, -32.029672],
[-52.096760, -32.029672],
[-52.096696, -32.029544],
[-52.095795, -32.029617],
[-52.094142, -32.029835],
[-52.088585, -32.030672],
[-52.088392, -32.030763],
[-52.088027, -32.034656],
[-52.101631, -32.032145],
[-52.101760, -32.031909]
]
]
}
}]
};
var geoObject = JSON.parse(value.geojson);
L.geoJSON(geojsonTESTE).addTo(mymap);
L.geoJSON(features).addTo(mymap);
//With Leaflet Ajax e Files
//j_url = "json/"+value.file;
//var jsonTest = new L.GeoJSON.AJAX([j_url]{onEachFeature:popUp}).addTo(mymap);
});
}
function AjaxFailed(results) {
console.log("ERROR AJAX");
alert("ERRO");
}
$.when(results).done(function() {
});
};
});
<?php
header('Content-Type: application/json');
$connStr = { database connection string }
$conn = pg_connect($connStr);
$result = pg_query($conn, "select * from mapas");
$qtd = pg_num_rows($result);
$res = "[";
if($qtd > 0){
$i = 0;
foreach(pg_fetch_all($result) as $row)
{
$x = $row['geojson'];
$patterns = array ('/[^A-Za-z0-9\-\,\"\:\{\}\[\]]/');
$replace = array ('');
$desc = preg_replace($patterns, $replace,$x);
$desc = str_replace('\"', '"', $desc, $count);
$data['id_map'] = $row['id_map'];
$data['description'] = $row['description'];
$data['geojson'] = $desc;
$data['file'] = $row['file'];
$map = json_readable_encode($data,null,true);
$res .= $map;
if($i < $qtd -1 ){
$res .= ",";
}
$i++;
}
}
$res .= "]";
echo trim($res);
pg_free_result($result);
pg_close();
?>
I want to make drilldown chart. But when I click the chart, the drilldown doesnt work. Here is the picture
]1
Here is the code
the view code (index.php)
<?php $this->registerJs("
$(function () {
$('#my-chart3').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Fakultas'
},
xAxis: {
categories: $tahun2
},
credits: {
enabled: false
},
series: [
{
name: 'FAPERTA',
data: $data_A,
drilldown: 'data A1','data A2','data A3','data A4','data A5'
},
{
name: 'FKH',
data: $data_B
},
{
name: 'FPIK',
data: $data_C
},
{
name: 'FAPET',
data: $data_D
},
{
name: 'FAHUTAN',
data: $data_E
},
{
name: 'FATETA',
data: $data_F
},
{
name: 'FMIPA',
data: $data_G
},
{
name: 'FEM',
data: $data_H
},
{
name: 'FEMA',
data: $data_I
},
{
name: 'DIPLOMA',
data: $data_J
}],
drilldown: {
series: [{
name: 'data A1',
id: 'data A1',
data: [$data_A1]
},
{
name: 'data A2',
id: 'data A2',
data: $data_A2
},
{
name: 'data A3',
id: 'data A3',
data: $data_A3
},
{
name: 'data A4',
id: 'data A4',
data: $data_A4
},
{
name: 'data A5',
id: 'data A5',
data: $data_A5
}
]
}
});
});
")?>
this is the controller code
$masuk2= S2view::find();
$awal2= $masuk2->orderBy('TahunMasuk ASC')->one()->TahunMasuk;
$akhir2= $masuk2->orderBy('TahunMasuk DESC')->one()->TahunMasuk;
$arr_A = [];
$arr_B = [];
$arr_C = [];
$arr_D = [];
$arr_E = [];
$arr_F = [];
$arr_G = [];
$arr_H = [];
$arr_I = [];
$arr_J = [];
$tahun2 = [];
$arr_A1 = [];
$arr_A2 = [];
$arr_A3 = [];
$arr_A4 = [];
$arr_A5 = [];
for($k=$awal2;$k<=$akhir2;$k++){
if($awal2 == $k){
$jum_A = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'A'])->all());
$jum_B = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'B'])->all());
$jum_C = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'C'])->all());
$jum_D = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'D'])->all());
$jum_E = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'E'])->all());
$jum_F = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'F'])->all());
$jum_G = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'G'])->all());
$jum_H = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'H'])->all());
$jum_I = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'I'])->all());
$jum_J = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'J'])->all());
$jum_A1 = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'A1'])->all());
$jum_A2 = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'A2'])->all());
$jum_A3 = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'A3'])->all());
$jum_A4 = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'A4'])->all());
$jum_A5 = count($masuk2->where(['TahunMasuk'=>$awal2,'Fakultas'=>'A5'])->all());
}elseif($k > $awal2 && $k <= $akhir2){
$jum_A = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'A'])->all());
$jum_B = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'B'])->all());
$jum_C = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'C'])->all());
$jum_D = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'D'])->all());
$jum_E = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'E'])->all());
$jum_F = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'F'])->all());
$jum_G = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'G'])->all());
$jum_H = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'H'])->all());
$jum_I = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'I'])->all());
$jum_J = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'J'])->all());
$jum_A1 = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'A1'])->all());
$jum_A2 = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'A2'])->all());
$jum_A3 = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'A3'])->all());
$jum_A4 = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'A4'])->all());
$jum_A5 = count($masuk2->where(['TahunMasuk'=>$k,'Fakultas'=>'A5'])->all());
}
array_push($arr_A,$jum_A);
array_push($arr_B,$jum_B);
array_push($arr_C,$jum_C);
array_push($arr_D,$jum_D);
array_push($arr_E,$jum_E);
array_push($arr_F,$jum_F);
array_push($arr_G,$jum_G);
array_push($arr_H,$jum_H);
array_push($arr_I,$jum_I);
array_push($arr_J,$jum_J);
array_push($tahun2,$k);
array_push($arr_A1,$jum_A1);
array_push($arr_A2,$jum_A2);
array_push($arr_A3,$jum_A3);
array_push($arr_A4,$jum_A4);
array_push($arr_A5,$jum_A5);
}
$data['tahun2'] = json_encode($tahun2);
$data['data_A'] = json_encode($arr_A);
$data['data_B'] = json_encode($arr_B);
$data['data_C'] = json_encode($arr_C);
$data['data_D'] = json_encode($arr_D);
$data['data_E'] = json_encode($arr_E);
$data['data_F'] = json_encode($arr_F);
$data['data_G'] = json_encode($arr_G);
$data['data_H'] = json_encode($arr_H);
$data['data_I'] = json_encode($arr_I);
$data['data_J'] = json_encode($arr_J);
$data['data_A1'] = json_encode($arr_A1);
$data['data_A2'] = json_encode($arr_A2);
$data['data_A3'] = json_encode($arr_A3);
$data['data_A4'] = json_encode($arr_A4);
$data['data_A5'] = json_encode($arr_A5);
return $this->render('index',$data);
}
but those codes result error.
The error code is
series: [
{
name: 'FAPERTA',
data: $data_A,
drilldown: 'data A1','data A2','data A3','data A4','data A5'
},
Please help me to solve that problem. What may I do to solve that problem? It'd help me so much. Thankyou
Below are the codes I am using,
When I selected values from the Database, they are submitted to the database, but if I type something not in the database and I want it submitted, It does not get submitted or echoed by PHP.
Sombody Please help me.
Thank You.
<?php
//Jason File
#Include the connect.php file
include('db_connect2.php');
//get county of selected district
$query = "SELECT * FROM primary_schools ";
$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$customers[] = array(
'Emis_No' => $row['Emis_No'],
'District' => $row['District'],
'County' => $row['County'],
'Subcounty' => $row['Subcounty'],
'Parish' => $row['Parish'],
'School' => $row['School']
);
}
echo json_encode($customers);
?>
//Script
<script type=”text/javascript”>
$(document).ready(function () {
//start EMIS code
var customersSourcel =
{
datatype: "json",
datafields: [
{ name: 'Emis_No'},
{ name: 'District'},
{ name: 'County'},
{ name: 'Subcounty'},
{ name: 'Parish'},
{ name: 'School'}
],
url: 'includes/emis.php',
cache: false,
async: false
};
var customersAdapterl = new $.jqx.dataAdapter(customersSourcel);
$("#emis_no").jqxComboBox(
{
source: customersAdapterl,
width: 200,
height: 25,
promptText: "emis",
displayMember: 'Emis_No',
valueMember: 'Emis_No'
});
$("#emis_no").bind('select', function(event)
{
if (event.args)
{
var index = $("#emis_no").jqxComboBox('selectedIndex');
if (index != -1)
{
var record = customersAdapterl.records[index];
document.form1.district.value = record.District;
$("#county").jqxComboBox({ disabled: false});
document.form1.county.value = record.County;
$("#sub_county").jqxComboBox({ disabled: false});
document.form1.sub_county.value = record.Subcounty;
$("#parish").jqxComboBox({ disabled: false});
document.form1.parish.value = record.Parish;
$("#school").jqxComboBox({ disabled: false});
document.form1.school.value = record.School;
}
}
});
Initialize $customers array with $customers = array(); in the begining of the PHP file before you start pushing values into it with $customers[] = ...
I'm trying to retrieve results using json in order to display world map with country list.
This is what I got so far:
function drawRegionsMap() {
$.ajax({
url: 'getlist.php",
dataType: "json"
}).done(function(result) {
var data = google.visualization.arrayToDataTable(result);
var view = new google.visualization.DataView(data)
view.setColumns([0, 1])
var chart = new google.visualization.GeoChart(
document.getElementById('map'));
chart.draw(data, options);
var geochart = new google.visualization.GeoChart(
document.getElementById('map'));
var options = {
width: "auto",
height: "auto",
colorAxis: {
colors: ['#E4B6D3', '#E06D94']
} // Map Colors
};
geochart.draw(data, options);
});
};
google.load('visualization', '1', {
'packages': ['geochart']
});
google.setOnLoadCallback(drawRegionsMap);
result returned from getlist.php is in json format:
{
"Country":[
"Germany",
"United States",
"Brazil",
"France",
"RU"
],
"Hits":[
200,
300,
400,
500,
600,
700
]
}
php code:
$data = array();
$data['Country'] = array();
$data['Hits'] = array();
$country = array('Germany','United States','Brazil','France','RU');
$hits = array(200,300,400,500,600,700);
for ($i = 0; $i < 6; $i++){
$data['Country'] = $country;
$data['Hits'] = $hits;
}
echo json_encode($data);
Looking in firebug console, I'm always getting : Error: Not an array
I've been trying to solve this puzzle for hours, but no success.
The returned result is an object, not an array.
The expected array would be:
[
["Country","Hits"],
["Germany",200],
["United States",300],
["Brazil",400],
["France",500],
["RU",600]
]
Modified PHP-code that returns this array:
<?php
$data = array(['Country','Hits']);
$country = array('Germany','United States','Brazil','France','RU');
$hits = array(200,300,400,500,600,700);
foreach ($country as $k => $v){
$data[] = array($v,$hits[$k]);
}
echo json_encode($data);
?>
I have a sample code:
<?php
$dateset = array();
$data[] = array("2013-08-22", 1);
$data[] = array("2013-08-23", 5);
$data[] = array("2013-08-24", 3);
$dataset = json_encode($data);
?>
<div id="data"></div>
And javascript
<script>
var dataset = [];
dataset = <?php echo $dataset; ?>;
var data = [{
data: dataset,
}];
$.plot($('#data'), data, {
series: {
lines: {
show: true
},
points: {
show: true
}
},
xaxis: {
mode: 'time',
timeformat: '%d/%m/%y',
minTickSize: [1, 'day']
}
})
</script>
Result not show plot, how to fix it ?
You have array to string conversion. $dataset is an array and cannot print as a string.
dataset = <?php echo $dataset; ?>; // Notice: Array to string conversion
Try something like this:
<?php
$array_values = "";
foreach($dataset as $data){
if($array_values != ""){
$array_values .= ",";
}
$array_values .= $data;
}
?>
<script>
var dataset = [<?php echo $array_values; ?>];
</script>