Change color of pie chart with google sheets API - google-sheets-api

I'm using Python google sheet API v4 to produce a pie chart using the following code:
body = {
"requests": [
{
"addChart": {
"chart": {
"spec": {
"backgroundColorStyle" : {
"themeColor": "BACKGROUND"
},
"pieChart": {
"legendPosition": "LABELED_LEGEND",
"pieHole":0.7,
"domain": {
"sourceRange": {
"sources": [
{
"sheetId": 0,
"startRowIndex": 0,
"endRowIndex": last_row,
"startColumnIndex": 0,
"endColumnIndex": 1
}
]
}
},
"series": {
"sourceRange": {
"sources": [
{
"sheetId": 0,
"startRowIndex": 0,
"endRowIndex": 7,
"startColumnIndex": 3,
"endColumnIndex": 4
}
]
}
},
}
},
"position": {
"overlayPosition": {
"anchorCell": {
"sheetId": 0,
"rowIndex": 1,
"columnIndex": 5
}
}
}
}
}
}
]
}
request = service.spreadsheets().batchUpdate(spreadsheetId=sheet_id, body=body)
response = request.execute()
This get me exactly what I want, but I would like to set the color of each slice according to a give color palette. I've looked for a way to do so but haven't found it, so is it possible to do so thought the API?
To further clarify with an image, this is what I get with 4 class. How can I set the color of this 4 slice to go from green, blu, yellow, red to green, purple, yellow, orange?
Graph I get with 4 class

Related

Mongodb aggregation to find outliers

In my mongodb collection documents are stored in the following format:
{ "_id" : ObjectId("62XXXXXX"), "res" : 12, ... }
{ "_id" : ObjectId("63XXXXXX"), "res" : 23, ... }
{ "_id" : ObjectId("64XXXXXX"), "res" : 78, ... }
...
I need to extract id's for the document for which the value of "res" is outlier (i.e. value < Q1 - 1.5 * IQR or value > Q3 + 1.5 * IQR (Q1, Q3 are percentiles)). I have done this using pandas functionality by retrieving all documents from the collection, which may become slow if the number of documents in collection become too big.
Is there a way to do this using mongodb aggregation pipeline (or just calculating percentiles)?
If I understand how you want to retrieve outliers, here's one way you might be able to do it.
db.collection.aggregate([
{ // partition res into quartiles
"$bucketAuto": {
"groupBy": "$res",
"buckets": 4
}
},
{ // get the max of each quartile
"$group": {
"_id": "$_id.max"
}
},
{ // sort the quartile maxs
"$sort": {
"_id": 1
}
},
{ // put sorted quartile maxs into array
"$group": {
"_id": null,
"maxs": {"$push": "$_id"}
}
},
{ // assign Q1 and Q3
"$project": {
"_id": 0,
"q1": {"$arrayElemAt": ["$maxs", 0]},
"q3": {"$arrayElemAt": ["$maxs", 2]}
}
},
{ // set IQR
"$set": {
"iqr": {
"$subtract": ["$q3", "$q1"]
}
}
},
{ // assign upper/lower outlier thresholds
"$project": {
"outlierThresholdLower": {
"$subtract": [
"$q1",
{"$multiply": ["$iqr", 1.5]}
]
},
"outlierThresholdUpper": {
"$add": [
"$q3",
{"$multiply": ["$iqr", 1.5]}
]
}
}
},
{ // get outlier _id's
"$lookup": {
"from": "collection",
"as": "outliers",
"let": {
"oTL": "$outlierThresholdLower",
"oTU": "$outlierThresholdUpper"
},
"pipeline": [
{
"$match": {
"$expr": {
"$or": [
{"$lt": ["$res", "$$oTL"]},
{"$gt": ["$res", "$$oTU"]}
]
}
}
},
{
"$project": {
"_id": 1
}
}
]
}
}
])
Try it on mongoplayground.net.
One more option based on #rickhg12hs's answer, is to use $setWindowFields:
db.collection.aggregate([
{$setWindowFields: {
sortBy: {res: 1},
output: {
totalCount: {$count: {}},
index: {$sum: 1, window: {documents: ["unbounded", "current"]}}
}
}
},
{$match: {
$expr: {$lte: [
{$abs: {$subtract: [
{$mod: [
{$multiply: [
{$add: ["$index", {$round: {$divide: ["$totalCount", 4]}}]}, 2]},
"$totalCount"
]}, 0]}
}, 1]}
}},
{$group: {_id: null, res: {$push: "$res"}}},
{$project: {_id: 0, q1: {$first: "$res"}, q3: {$last: "$res"},
iqr: {"$subtract": [{$last: "$res"}, {$first: "$res"}]}
}},
{$project: {
outlierThresholdLower: {$subtract: ["$q1", {$multiply: ["$iqr", 1.5]}]},
outlierThresholdUpper: {$add: ["$q3", {$multiply: ["$iqr", 1.5]}]}
}
},
{$lookup: {
from: "collection",
as: "outliers",
let: {oTL: "$outlierThresholdLower", oTU: "$outlierThresholdUpper"},
pipeline: [
{$match: {$expr: {$or: [{$lt: ["$res", "$$oTL"]}, {$gt: ["$res", "$$oTU"]}]}}},
{$project: {_id: 1}}
]
}
}
])
See how it works on the playground example

Error creating protected columns with google sheet create api

I am following the google sheet v4 api doumentation to create google sheet with protected columns (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create)
I am able to create sheet without using protectedRange in api, using it always gives error, below are request /response i am getting
"properties": {
"title": "NEW SHEET"
},
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"numberValue": 10
}
},
{
"userEnteredValue": {
"numberValue": 20
}
},
{
"userEnteredValue": {
"numberValue": 30
}
}
]
}
]
}
]
},
{
"protectedRanges": [
{
"description": "Locked columns",
"range": {
"sheetId": 0,
"startColumnIndex": 0,
"endColumnIndex": 2
}
}
]
}
]
}
response
{
"error": {
"code": 400,
"message": "Invalid sheets[1].protectedRanges[0]: No grid with id: 0",
"status": "INVALID_ARGUMENT"
}
}
You want to create new Spreadsheet.
When the new Spreadsheet is created, you want to add the protected ranges.
In your sample, you want to create new Spreadsheet including a sheet which has the protected columns "A" and "B" and values of 10, 20, 30 in the cells "A1:C1".
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Issue and solution:
In your request body, please include the property of protectedRanges to the 1st index of sheets.
Please set the sheet ID at properties.
Modified request body:
{
"properties": {
"title": "NEW SHEET"
},
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"userEnteredValue": {
"numberValue": 10
}
},
{
"userEnteredValue": {
"numberValue": 20
}
},
{
"userEnteredValue": {
"numberValue": 30
}
}
]
}
]
}
],
"protectedRanges": [
{
"description": "Locked columns",
"range": {
"startColumnIndex": 0,
"endColumnIndex": 2,
"sheetId": 0
}
}
],
"properties": {
"sheetId": 0
}
}
]
}
For example, when "sheetId": 123 is set, the sheet is created as the sheet ID of 123.
You can also test above request body at Try this API.
Reference:
Method: spreadsheets.create
If I misunderstood your question and this was not the direction you want, I apologize.

Error when creating a chart via a batch request

I'm trying to create a new chart, following the examples presented in Google sheets API. I'm getting the following error:
HttpError 400 when requesting
https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate?alt=json
returned "Invalid JSON payload received. Unknown name "add_chart" at
'requests[0]': Cannot find field."
Has anyone encountered this before?
Other requests are working normal (replace text, add text, clone presentation, etc)
this request is being copied from the example in Google sheets API.
sourceSheetId is the id where I have the data for the chart saved in.
{
"addChart": {
"chart": {
"spec": {
"title": "Model Q1 Sales",
"basicChart": {
"chartType": "COLUMN",
"legendPosition": "BOTTOM_LEGEND",
"axis": [
{
"position": "BOTTOM_AXIS",
"title": "Model Numbers"
},
{
"position": "LEFT_AXIS",
"title": "Sales"
}
],
"domains": [
{
"domain": {
"sourceRange": {
"sources": [
{
"sheetId": sourceSheetId,
"startRowIndex": 0,
"endRowIndex": 7,
"startColumnIndex": 0,
"endColumnIndex": 1
}
]
}
}
}
],
"series": [
{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": sourceSheetId,
"startRowIndex": 0,
"endRowIndex": 7,
"startColumnIndex": 1,
"endColumnIndex": 2
}
]
}
},
"targetAxis": "LEFT_AXIS"
},
{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": sourceSheetId,
"startRowIndex": 0,
"endRowIndex": 7,
"startColumnIndex": 2,
"endColumnIndex": 3
}
]
}
},
"targetAxis": "LEFT_AXIS"
},
{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": sourceSheetId,
"startRowIndex": 0,
"endRowIndex": 7,
"startColumnIndex": 3,
"endColumnIndex": 4
}
]
}
},
"targetAxis": "LEFT_AXIS"
}
],
"headerCount": 1
}
},
"position": {
"newSheet": True
}
}
}
}
I was expecting the chart to be created and receive a response with chartId, however I'm getting from the request a 400 status:
HttpError 400 when requesting
https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate?alt=json
returned "Invalid JSON payload received. Unknown name "add_chart" at
'requests[0]': Cannot find field."

Openflow Swicth not pushing MPLS tag. (OpenDayLight)

I have the following linear SDN architecture with an ODL controller:
Host1 -- ZodiacFX1 -- ZodiacFX2 --- Host2
I am using 2 laptops as hosts and 2 ZodiacFX openflow switches.
I Want the ZodiacFX1 to push a MPLS tag to all the IP packets received from Host1 and the ZodiacFX2 to pop the MPLS tag and send the IP packets to the Host2.
I have added a flow for the MPLS push on the ZodiacFX1 and I can see the flow active in the ZodiacFX1 and also in the operational datastore of ODL. But if I ping h1->h2 there is no push.
The flow is this:
NOTE:(Host1 is conected to port 1 of ZodiacFX1 and ZodiacFX1 port2 is conected to ZodiacFX2 port1.)
GET http://192.168.21.147:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:123917682137538/table/2
{
"flow-node-inventory:table": [
{
"id": 2,
"opendaylight-flow-table-statistics:flow-table-statistics": {
"active-flows": 1,
"packets-looked-up": 0,
"packets-matched": 0
},
"flow": [
{
"id": "125",
"idle-timeout": 0,
"cookie": 401,
"flags": "",
"hard-timeout": 0,
"instructions": {
"instruction": [
{
"order": 0,
"apply-actions": {
"action": [
{
"order": 2,
"output-action": {
"output-node-connector": "2",
"max-length": 0
}
},
{
"order": 1,
"set-field": {
"protocol-match-fields": {
"mpls-label": 27
}
}
},
{
"order": 0,
"push-mpls-action": {
"ethernet-type": 34887
}
}
]
}
}
]
},
"cookie_mask": 0,
"opendaylight-flow-statistics:flow-statistics": {
"duration": {
"nanosecond": 0,
"second": 7
},
"byte-count": 0,
"packet-count": 0
},
"priority": 0,
"table_id": 2,
"match": {
"in-port": "1",
"ethernet-match": {
"ethernet-type": {
"type": 2048
}
}
}
},
{
"id": "124",
"idle-timeout": 0,
"cookie": 401,
"flags": "",
"hard-timeout": 0,
"instructions": {
"instruction": [
{
"order": 0,
"apply-actions": {
"action": [
{
"order": 2,
"output-action": {
"output-node-connector": "2",
"max-length": 0
}
},
{
"order": 1,
"set-field": {
"protocol-match-fields": {
"mpls-label": 27
}
}
},
{
"order": 0,
"push-mpls-action": {
"ethernet-type": 34887
}
}
]
}
}
]
},
"cookie_mask": 0,
"opendaylight-flow-statistics:flow-statistics": {
"duration": {
"nanosecond": 0,
"second": 180
},
"byte-count": 0,
"packet-count": 0
},
"priority": 8,
"table_id": 2,
"match": {
"in-port": "1",
"ethernet-match": {
"ethernet-type": {
"type": 2048
}
}
}
}
]
}
]
}
And I can see too in Zodiac console interface:
Flow 6
Match:
In Port: 1
ETH Type: IPv4
Attributes:
Table ID: 2 Cookie:0x191
Priority: 8 Duration: 247 secs
Hard Timeout: 0 secs Idle Timeout: 0 secs
Byte Count: 0 Packet Count: 0
Last Match: 00:04:07
Instructions:
Apply Actions:
Push MPLS tag
Set MPLS Label: 27
Output Port: 2
What can be the problem? I think that the main problem is that in this case Zodiac is following this flow, I have tried my flow with priority 0 too and there is no MPLS push.
Flow 5
Match:
In Port: 1
Attributes:
Table ID: 0 Cookie:0x2b00000000000008
Priority: 2 Duration: 2845 secs
Hard Timeout: 0 secs Idle Timeout: 0 secs
Byte Count: 576265 Packet Count: 5246
Last Match: 00:00:00
Instructions:
Apply Actions:
Output Port: 3
Output Port: 2
Output: CONTROLLER
it looks like you have some other application (maybe it's l2switch) pushing
flows to your switches as well. Maybe just install openflowplugin and try.
also, try putting your mpls flows in table 0 instead of table 2.

How to customize czml datasource?

I have a CZML data that I extracted via python.
I have buildings, with their geometry, height, building ID, and intervals. each interval has a value.
After loading the czml data to Cesium, I'd like to access the attributes and then customize the color of the buildings according to the value given.
Here is a sample of my CZML:
[{
"id": "document",
"version": "1.0"
}, {
"id": 32,
"availability": "2014-01-01T00:00:00Z/2014-12-31T00:00:00Z",
"polygon": {
"positions": {
"cartographicDegrees": [54.7162360431897, 24.4519912715277, 0, 54.716219612921, 24.4519754832587, 0, 54.7162501395131, 24.4519488635358, 0, 54.7162465684811, 24.4519454316688, 0, 54.7162670831639, 24.4519275432238, 0, 54.7162707308589, 24.4519310439514, 0, 54.7163022563025, 24.4519035537608, 0, 54.7161962974502, 24.4518018819532, 0, 54.7161647729823, 24.4518293730395, 0, 54.7162035538772, 24.4520196028966, 0, 54.7162360431897, 24.4519912715277, 0]
},
"someProperty": [{
"interval": "2014-00-01T00:00:00Z/2014-01-01T00:00:00Z",
"En_C_need": 0.7
}, {
"interval": "2014-01-01T00:00:00Z/2014-02-01T00:00:00Z",
"En_C_need": 1.0
}, {
"interval": "2014-02-01T00:00:00Z/2014-03-01T00:00:00Z",
"En_C_need": 2.6
}, {
"interval": "2014-03-01T00:00:00Z/2014-04-01T00:00:00Z",
"En_C_need": 12.1
}, {
"interval": "2014-04-01T00:00:00Z/2014-05-01T00:00:00Z",
"En_C_need": 30.2
}, {
"interval": "2014-05-01T00:00:00Z/2014-06-01T00:00:00Z",
"En_C_need": 37.8
}],
"extrudedHeight": 6.0
}
}]
I have other GeoJSON data that I customized, I tried the same method but it didn't work.
Here is what I'm trying to do (this does not work):
var test2 = Cesium.CzmlDataSource.load ('Data/czml/TESTING/example_8.czml');
test2.then(function (dataSource) {
viewer.dataSources.add(test2);
viewer.zoomTo (test2);
var entities = dataSource.entities.values;
var colorHash = {};
var Energy = [];
for (var i = 0; i < entities.length; i++) {
var entity = entities[i];
Energy = entity.someProperty.En_C_need;
var color = colorHash [Energy];
if(!color ) {
if (Energy < 5 ) {
color = Cesium.Color.DARKSALMON.withAlpha (0.95);
} else if (Energy < 10) {
color = Cesium.Color.BURLYWOOD.withAlpha (0.95);
} else if (Energy < 20) {
color = Cesium.Color.RED.withAlpha (0.95);
} else {
color = Cesium.Color.PINK.withAlpha (0.95);
};
colorHash[Energy] = color;
};
entity.polygon.fill = true;
entity.polygon.material = color;
entity.polygon.outline = false;
};
});
To start with the solution - here's the working plnkr:
https://plnkr.co/edit/P1Cg4DNJYtK9r9XrLzxK?p=preview
I've changed several things in your code:
1) viewer.dataSources.add(test2); should be outside your promise (it doesn't really matter, but it's cleaner code - using the promise inside the promise feels strange).
2) According to the CZML properties spec, you need to place the properties in the right location in the CZML. It should not be inside the polygon, but in the "root":
var czml = [{"id": "document", "version": "1.0"},
{
"id": 32, "availability": "2014-01-01T00:00:00Z/2014-12-31T00:00:00Z", "properties": {
"someProperty": [
{"interval": "2014-00-01T00:00:00Z/2014-01-01T00:00:00Z", "En_C_need": 0.7},
{"interval": "2014-01-01T00:00:00Z/2014-02-01T00:00:00Z", "En_C_need": 1.0},
{"interval": "2014-02-01T00:00:00Z/2014-03-01T00:00:00Z", "En_C_need": 2.6},
{"interval": "2014-03-01T00:00:00Z/2014-04-01T00:00:00Z", "En_C_need": 12.1},
{"interval": "2014-04-01T00:00:00Z/2014-05-01T00:00:00Z", "En_C_need": 30.2},
{"interval": "2014-05-01T00:00:00Z/2014-06-01T00:00:00Z", "En_C_need": 37.8}
],
},
"polygon": {
"positions": {
"cartographicDegrees":
[54.7162360431897, 24.4519912715277, 0, 54.716219612921, 24.4519754832587, 0, 54.7162501395131, 24.4519488635358, 0, 54.7162465684811, 24.4519454316688, 0, 54.7162670831639, 24.4519275432238, 0, 54.7162707308589, 24.4519310439514, 0, 54.7163022563025, 24.4519035537608, 0, 54.7161962974502, 24.4518018819532, 0, 54.7161647729823, 24.4518293730395, 0, 54.7162035538772, 24.4520196028966, 0, 54.7162360431897, 24.4519912715277, 0]
},
"extrudedHeight": 6.0
}
}];
Then you can ask for the properties according to the interval (Energy = entity.properties.getValue(viewer.clock.currentTime).someProperty.En_C_need;) and get the Energy at viewer's current time.
Update after commentchatting:
Your code was not meant to change dynamically. You need to set the value either by callbackProperty as in this example: plnkr.co/edit/lm290uaQewEvfIOgXbDP?p=preview or by using timeIntervalCollection