I am trying to have my line chart to show the result within tooltip as a string format (e.g: Jun 28, 2015 - Jul 4, 2015) while having my hAxis to display its values as date format (e.g: Jun 29).
Similar as Analytics:
However when I draw the chart with the returned Json data shown below:
{
"cols": [{
"id": "A",
"label": "Date Range",
"pattern": "",
"type": "string"
}, {
"id": "B",
"label": "Sessions",
"pattern": "",
"type": "number"
}, {
"id": "C",
"label": "Pageviews",
"pattern": "",
"type": "number"
}],
"rows": [{
"c": [{
"v": "Date(2015,5,23)",
"f": "Jun 23, 2015 - Jun 27, 2015"
}, {
"v": 1645
}, {
"v": 5237
}]
}, {
"c": [{
"v": "Date(2015,5,28)",
"f": "Jun 28, 2015 - Jul 04, 2015"
}, {
"v": 2189
}, {
"v": 6977
}]
}, {
"c": [{
"v": "Date(2015,6,05)",
"f": "Jul 05, 2015 - Jul 11, 2015"
}, {
"v": 2168
}, {
"v": 6862
}]
}, {
"c": [{
"v": "Date(2015,6,12)",
"f": "Jul 12, 2015 - Jul 18, 2015"
}, {
"v": 1661
}, {
"v": 5735
}]
}, {
"c": [{
"v": "Date(2015,6,19)",
"f": "Jul 19, 2015 - Jul 23, 2015"
}, {
"v": 1109
}, {
"v": 3826
}]
}]
}
My chart is showing the hAxis with f property's value instead of v property's value as per shown below:
The data type for the hAxis is set as string.
With the information given, may I ask how I can achieve my desired results?
Have you tried setting the 'options' -> 'vAxis' -> 'ticks' parameter?
Then your json data would look something like this:
{
"cols": [your columns],
"rows": [your rows],
"options": {
"vaxis": {
"ticks": ["jun 29", "jul 6", "jul 13"]
}
}
}
Here's a link to google charts documentation on line charts, I hoe this helps :-)
Edit:
Here's a working jsfiddle. I don't know what method you are using to load your data but this worked for me.
For some reason, the labels were displayed correctly after I set the width and height parameters in options.
Based on the working jsfiddle provided by #bjarkig82. I have figured on how to fix the issue and achieve my objective.
First, the data type for column date needs to be changed from string to date
var cols = [{
"id": "A",
"label": "Date Range",
"pattern": "",
"type": "date"
}, {
"id": "B",
"label": "Sessions",
"pattern": "",
"type": "number"
}, {
"id": "C",
"label": "Pageviews",
"pattern": "",
"type": "number"
}];
var rows = [{
"c": [{
"v": "Date(2015,5,23)",
"f": "Jun 23, 2015 - Jun 27, 2015"
}, {
"v": 1645
}, {
"v": 5237
}]
}, {
"c": [{
"v": "Date(2015,5,28)",
"f": "Jun 28, 2015 - Jul 04, 2015"
}, {
"v": 2189
}, {
"v": 6977
}]
}, {
"c": [{
"v": "Date(2015,6,05)",
"f": "Jul 05, 2015 - Jul 11, 2015"
}, {
"v": 2168
}, {
"v": 6862
}]
}, {
"c": [{
"v": "Date(2015,6,12)",
"f": "Jul 12, 2015 - Jul 18, 2015"
}, {
"v": 1661
}, {
"v": 5735
}]
}, {
"c": [{
"v": "Date(2015,6,19)",
"f": "Jul 19, 2015 - Jul 23, 2015"
}, {
"v": 1109
}, {
"v": 3826
}]
}];
Secondly, I comment the section of code shown below or it will cause my string date (e.g: Jun 28, 2015 - Jul 4, 2015) to show as date (e.g: Jun 28, 2015) within the tooltip instead.
var formatter = new google.visualization.DateFormat({ pattern: "EEEE, MMM dd, yyyy" });
formatter.format(data, 0);
To understand better, please check out this jsfiddle
Related
I have data on invoices and I want to group them by time period wall. for example 0-10 and 11-20:
I need to solve them with $aggregate in mongoDB.
{
'_id': '1',
'value': 10,
'due_date': '20221001'
},
{
'_id': '2',
'value': 10,
'due_date': '20221012'
},
{
'_id': '2',
'value': 10,
'due_date': '20221030'
},
I need to group by period 0-10 days, 11-20 days and sum the values: For the example above the result would be:
[{
"_id": '0-10 days',
"total": 10,
},
{
"_id": '11-20 days',
"total": 10,
},
{
"_id": '>20 days',
"total": 10,
}]
I try with:
['$facet' => [
['due_date_one' => [
['$match' => [
'due_date' => [
'$gt' => new UTCDateTime((new Carbon())-> subDays(100) -> getTimestamp()),
'$lte' => date('Y-m-d', strtotime('now'))
]
]
]],
]]
You could use $bucket or add a few $addFields and $group stages:
Example mongo playground - https://mongoplayground.net/p/B0zl_GGH4sG
Example Documents:
[
{
"_id": "1a",
"value": 10,
"due_date": "20221001"
},
{
"_id": "1b",
"value": 5,
"due_date": "20221102"
},
{
"_id": "1c",
"value": 7,
"due_date": "20221102"
},
{
"_id": "2a",
"value": 10,
"due_date": "20221012"
},
{
"_id": "2b",
"value": 7,
"due_date": "20221113"
},
{
"_id": "2c",
"value": 8,
"due_date": "20221113"
},
{
"_id": "3a",
"value": 10,
"due_date": "20221030"
},
{
"_id": "3b",
"value": 9,
"due_date": "20221131"
},
{
"_id": "3c",
"value": 11,
"due_date": "20221131"
}
]
Aggregation query:
db.collection.aggregate([
{
$addFields: {
day: {
$toInt: { $substr: [ "$due_date", 6, 2 ] }
}
}
},
{
$addFields: {
bucketDate: {
$switch: {
branches: [
{ case: { $gt: [ "$day", 20 ] }, then: ">20 days" },
{ case: { $gt: [ "$day", 10 ] }, then: "11-20 days" }
],
"default": "0-10 days"
}
}
}
},
{
$addFields: {
bucketDateWithMonth: {
$concat: [
{ $substr: [ "$due_date", 0, 6 ] },
" ",
"$bucketDate"
]
}
}
},
{
$group: {
//_id: "$bucketDate", //No grouped month
_id: "$bucketDateWithMonth", //With grouped month
count: { $sum: 1 },
value: { $sum: "$value" }
}
}
])
Output: (grouped month)
[
{
"_id": "202210 0-10 days",
"count": 1,
"value": 10
},
{
"_id": "202211 0-10 days",
"count": 2,
"value": 12
},
{
"_id": "202210 11-20 days",
"count": 1,
"value": 10
},
{
"_id": "202211 11-20 days",
"count": 2,
"value": 15
},
{
"_id": "202210 \u003e20 days",
"count": 1,
"value": 10
},
{
"_id": "202211 \u003e20 days",
"count": 2,
"value": 20
}
]
Output: (No grouped month)
[
{
"_id": "\u003e20 days",
"count": 3,
"value": 30
},
{
"_id": "0-10 days",
"count": 3,
"value": 22
},
{
"_id": "11-20 days",
"count": 3,
"value": 25
}
]
Below is rest api json output.
{
"data": {
"AB": [
{
"month": "Nov 2016",
"total_tax": "132"
},
{
"month": "Dec 2016",
"total_tax": 88
}
],
"BC": [
{
"month": "Nov 2016",
"total_tax": "132"
},
{
"month": "Dec 2016",
"total_tax": 76
}
],
"MB": [
{
"month": "Nov 2016",
"total_tax": "84"
},
{
"month": "Dec 2016",
"total_tax": 12
}
]
}
}
Above response is pass to jquery ajax function to plot highchart heatmap graph.We are trying to parse ajax response data to get the below pattern for heatmap graph.
var xaxisCategory = ['AB','BC','MB'];
var yaxisCategory = ['Nov 2016', 'Dec 2016'];
var series = [
[0, 0, 132], [0, 1, 88],
[1, 0, 132], [1, 1, 76],
[2, 0, 84], [2, 1, 12]
];
By using jquery inArray getting distinct x-axis and y-axis category.In series pattern having [x-axis,y-axis,value].In x-axis having 'AB' index is '0',on y-axis 'Nov 2016' index is '0' and values is total_tax is '132'.So how to plot series data.HoW to plot cartesian series data please help.
Try below example
var data = {
"data": {
"AB": [
{
"month": "Nov 2016",
"total_tax": 132
},
{
"month": "Dec 2016",
"total_tax": 88
}
],
"BC": [
{
"month": "Nov 2016",
"total_tax": 132
},
{
"month": "Dec 2016",
"total_tax": 76
}
],
"MB": [
{
"month": "Nov 2016",
"total_tax": 84
},
{
"month": "Dec 2016",
"total_tax": 12
}
]
}
};
var parsedata = data.data;
var xaxisCategory = Array();
var yaxisCategory = Array();
var series = Array();
jQuery.each( parsedata, function( key, value ) {
xaxisCategory.push(key);
jQuery.each( value, function( key, value ) {
yaxisCategory.push(value.month);
series.push(value.total_tax);
});
});
console.log(xaxisCategory);
console.log(yaxisCategory);
var final = Array();
var i = 0;
$.each(series, function( index, value ) {
var item = Array();
item.push(i);
if (index % 2 == 0) {
item.push(0);
}
else {
item.push(1);
}
if (index % 2 != 0) {
i++;
}
item.push(value);
final.push(item);
});
console.log(final);
Here is the jsfiddle see output in console
c_id: 76523456,
output: {
alert_level: 1,
data: {
volume: 56,
date: '10th June 2016'
},
alert_level: 2,
data: {
volume: 26,
date: '10th July 2016'
}
}
}
Can someone please help. How can we apply double quotes via php code, then decode it?
if i am not wrong this should be the structure of json what you are expecting ...
[{
"alert_level": 1,
"data": {
"volume": 56,
"date": "10th June 2016"
}
}, {
"alert_level": 2,
"data": {
"volume": 26,
"date": "10th July 2016"
}
}]
dict inside the list/Array
I have a feed from a parcel tracking service that i am trying to integrate into my site. I have a url which allows me to put the tracking number at the end and get a json response. I have multiple objects from this which include some static info such as the senders address and some info which I will need to use a foreach for like the tracking progress.
I believe I have got the string okay however I am not sure how I am meant to display the info.
This is what I have so far:
Example URL:
domain.com/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665
URL Returns:
{
"Agent": null,
"Consignee": {
"Address1": "25 HEATHFIELD ROAD",
"Address2": "SHOLING",
"Address3": "",
"Code": null,
"Company": "ERIK HANSON",
"Country": "GREAT BRITAIN",
"Dept": "",
"Email": "ERIK.HANSON66#GOOGLEMAIL.COM",
"Name": "",
"Phone": "07770320490",
"Postcode": "SO19 1DL",
"State": "HANTS",
"Town": "SOUTHAMPTON"
},
"CrossIdx": "",
"Error": null,
"NonDel": null,
"POD": {
"Date": "13 Jul 2016",
"Status": "Harnett",
"Time": "09:17"
},
"Pieces": 1,
"PosErr": 0,
"Tracks": [
{
"Date": "13 Jul 2016",
"Status": "Out for delivery",
"Time": "07:10"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:24"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:22"
},
{
"Date": "12 Jul 2016",
"Status": "Arrived At Ryton",
"Time": "22:12"
},
{
"Date": "12 Jul 2016",
"Status": "Preparing for despatch",
"Time": "14:00"
},
{
"Date": "12 Jul 2016",
"Status": "Scanned into OCS HEATHROW LONDON",
"Time": "13:59"
},
{
"Date": "12 Jul 2016",
"Status": "Consignment details verified",
"Time": "13:59"
},
{
"Date": "14 Jun 2016",
"Status": "Shipment prepared by customer",
"Time": "11:20"
},
{
"Date": "02 Jan 2003",
"Status": "Collected from Customer",
"Time": "09:32"
}
],
"Weight": 7
}
Current PHP:
//set tracking url
$url = "http://www.ocscourier.co.uk/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665";
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.ocscourier.co.uk/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
//call api
//$json = file_get_contents($url);
$json = json_decode($output);
$Address1 = $json->results[0]->Consignee->Address1;
$Address2 = $json->results[0]->Consignee->Address2;
echo "Address 1: " . $Address1 . ", Address 2: " . $Address2;
Your json string doesn't have any results key (so I'm not sure why you trying to access results[0].
You can just use
$Address1 = $json->Consignee->Address1;
$Address2 = $json->Consignee->Address2;
Check this code:
$s = <<< END
{
"Agent": null,
"Consignee": {
"Address1": "25 HEATHFIELD ROAD",
"Address2": "SHOLING",
"Address3": "",
"Code": null,
"Company": "ERIK HANSON",
"Country": "GREAT BRITAIN",
"Dept": "",
"Email": "ERIK.HANSON66#GOOGLEMAIL.COM",
"Name": "",
"Phone": "07770320490",
"Postcode": "SO19 1DL",
"State": "HANTS",
"Town": "SOUTHAMPTON"
},
"CrossIdx": "",
"Error": null,
"NonDel": null,
"POD": {
"Date": "13 Jul 2016",
"Status": "Harnett",
"Time": "09:17"
},
"Pieces": 1,
"PosErr": 0,
"Tracks": [
{
"Date": "13 Jul 2016",
"Status": "Out for delivery",
"Time": "07:10"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:24"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:22"
},
{
"Date": "12 Jul 2016",
"Status": "Arrived At Ryton",
"Time": "22:12"
},
{
"Date": "12 Jul 2016",
"Status": "Preparing for despatch",
"Time": "14:00"
},
{
"Date": "12 Jul 2016",
"Status": "Scanned into OCS HEATHROW LONDON",
"Time": "13:59"
},
{
"Date": "12 Jul 2016",
"Status": "Consignment details verified",
"Time": "13:59"
},
{
"Date": "14 Jun 2016",
"Status": "Shipment prepared by customer",
"Time": "11:20"
},
{
"Date": "02 Jan 2003",
"Status": "Collected from Customer",
"Time": "09:32"
}
],
"Weight": 7
}
END;
$json = json_decode($s);
$Address1 = $json->Consignee->Address1;
$Address2 = $json->Consignee->Address2;
echo "Address 1: " . $Address1 . ", Address 2: " . $Address2;
Here is the output:
Address 1: 25 HEATHFIELD ROAD, Address 2: SHOLING
Below is json string in $json variable and access with RecursiveIteratorIterator
Example
$json='{
"Agent": null,
"Consignee": {
"Address1": "25 HEATHFIELD ROAD",
"Address2": "SHOLING",
"Address3": "",
"Code": null,
"Company": "ERIK HANSON",
"Country": "GREAT BRITAIN",
"Dept": "",
"Email": "ERIK.HANSON66#GOOGLEMAIL.COM",
"Name": "",
"Phone": "07770320490",
"Postcode": "SO19 1DL",
"State": "HANTS",
"Town": "SOUTHAMPTON"
},
"CrossIdx": "",
"Error": null,
"NonDel": null,
"POD": {
"Date": "13 Jul 2016",
"Status": "Harnett",
"Time": "09:17"
},
"Pieces": 1,
"PosErr": 0,
"Tracks": [
{
"Date": "13 Jul 2016",
"Status": "Out for delivery",
"Time": "07:10"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:24"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:22"
},
{
"Date": "12 Jul 2016",
"Status": "Arrived At Ryton",
"Time": "22:12"
},
{
"Date": "12 Jul 2016",
"Status": "Preparing for despatch",
"Time": "14:00"
},
{
"Date": "12 Jul 2016",
"Status": "Scanned into OCS HEATHROW LONDON",
"Time": "13:59"
},
{
"Date": "12 Jul 2016",
"Status": "Consignment details verified",
"Time": "13:59"
},
{
"Date": "14 Jun 2016",
"Status": "Shipment prepared by customer",
"Time": "11:20"
},
{
"Date": "02 Jan 2003",
"Status": "Collected from Customer",
"Time": "09:32"
}
],
"Weight": 7
}';
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key:\n</br>";
} else {
echo "$key => $val\n";
}
}
OUTPUT:
Agent => Consignee:
Address1 => 25 HEATHFIELD ROAD Address2 => SHOLING Address3 => Code => Company => ERIK HANSON Country => GREAT BRITAIN Dept => Email => ERIK.HANSON66#GOOGLEMAIL.COM Name => Phone => 07770320490 Postcode => SO19 1DL State => HANTS Town => SOUTHAMPTON CrossIdx => Error => NonDel => POD:
Date => 13 Jul 2016 Status => Harnett Time => 09:17 Pieces => 1 PosErr => 0 Tracks:
0:
Date => 13 Jul 2016 Status => Out for delivery Time => 07:10 1:
Date => 13 Jul 2016 Status => At Delivery location Portsmouth Time => 02:24 2:
Date => 13 Jul 2016 Status => At Delivery location Portsmouth Time => 02:22 3:
Date => 12 Jul 2016 Status => Arrived At Ryton Time => 22:12 4:
Date => 12 Jul 2016 Status => Preparing for despatch Time => 14:00 5:
Date => 12 Jul 2016 Status => Scanned into OCS HEATHROW LONDON Time => 13:59 6:
Date => 12 Jul 2016 Status => Consignment details verified Time => 13:59 7:
Date => 14 Jun 2016 Status => Shipment prepared by customer Time => 11:20 8:
Date => 02 Jan 2003 Status => Collected from Customer Time => 09:32 Weight => 7
{
"response": {
"request": {
"date": "Tue, 13 Sep 2011 11:24:28 +0200",
"resource": "/users/2cec711d-ca14-4472-98c8-ca74432bc2d3.json",
"status": {
"flag": "success",
"code": 200
}
},
"result": {
"data": {
"user": {
"user_token": "2cec711d-ca14-4472-98c8-ca74432bc2d3",
"date_creation": "Tue, 1 Sep 2011 11:01:12 +0200",
"date_last_login": "Tue, 13 Sep 2011 01:05:07 +0200",
"num_logins": "64",
"identities": [{
"identity_token": "cd3bd13b-b393-4d6c-a7f6-950b4c47938f",
"provider": "twitter",
"id": "http://twitter.com/ExampleUser",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname"
},
"gender": "male",
"utcOffset": "2:00"
}, {
"identity_token": "3ab5257b-ba2b-4242-a7f6-950b4c47938f",
"provider": "facebook",
"id": "http://www.facebook.com/profile.php?id=1046121518",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname",
"givenName": "Firstname",
"familyName": "Lastname"
},
"gender": "male",
"birthday": "01/01/1980",
"utcOffset": "2:00",
"emails": [{
"value": "email#example.com",
"is_verified": "true"
}]
}]
}
}
}
}
}
The above code is Result: the code returned by the ONEALL API
I am using oneall api for my website social login ,after i login i requested user details by get method using user token, the values like last login got by using $data->user->date_last_login, but i am unable to get email and dispalyname how can i do that one , i didn't wrote all the code i gave just sample, hope this will help for others also
http://docs.oneall.com/api/resources/users/read-user-details/
If you follow the object structure, you can get them as:
$data = json_decode($json);
$displayName = $data->response->result->data->user->identities[1]->displayName;
$email = $data->response->result->data->user->identities[1]->emails[0]->value;
Update:
If you are using intermediate var for response data, then just shrink the chain:
$json = json_decode($result_json);
//extract response data
$data = $json->response->result->data;
$displayName = $data->user->identities[1]->displayName;
$email = $data->user->identities[1]->emails[0]->value;
This will give you:
Firstname Lastname
email#example.com
Here we go complete code, based on your JSON object:
<?php
$result_json = '
{
"response": {
"request": {
"date": "Tue, 13 Sep 2011 11:24:28 +0200",
"resource": "/users/2cec711d-ca14-4472-98c8-ca74432bc2d3.json",
"status": {
"flag": "success",
"code": 200
}
},
"result": {
"data": {
"user": {
"user_token": "2cec711d-ca14-4472-98c8-ca74432bc2d3",
"date_creation": "Tue, 1 Sep 2011 11:01:12 +0200",
"date_last_login": "Tue, 13 Sep 2011 01:05:07 +0200",
"num_logins": "64",
"identities": [{
"identity_token": "cd3bd13b-b393-4d6c-a7f6-950b4c47938f",
"provider": "twitter",
"id": "http://twitter.com/ExampleUser",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname"
},
"gender": "male",
"utcOffset": "2:00"
}, {
"identity_token": "3ab5257b-ba2b-4242-a7f6-950b4c47938f",
"provider": "facebook",
"id": "http://www.facebook.com/profile.php?id=1046121518",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname",
"givenName": "Firstname",
"familyName": "Lastname"
},
"gender": "male",
"birthday": "01/01/1980",
"utcOffset": "2:00",
"emails": [{
"value": "email#example.com",
"is_verified": "true"
}]
}]
}
}
}
}
}';
$json = json_decode($result_json);
$data = $json->response->result->data;
echo $displayName = $data->user->identities[1]->displayName;
echo '<br>';
echo $email = $data->user->identities[1]->emails[0]->value;
?>