Menu
Download Free Trial

How I can add a formula in JSON?

Answered

Hi support.

I need to create a formula where, for example: sum("Price") * sum("Quantity")

var jsonData = [
{ "Color": "green", "M": "September", "W": "Wed", "country": "Canada", "state": "Ontario", "city": "Toronto", "Price": 174, "Quantity": 22 },
{ "Color": "red", "M": "March", "W": "Mon", "Time": "1000", "country": "USA", "state": "California", "city": "Los Angeles", "Price": 1664, "Quantity": 19 },
....
];

function setPivotReport() {
var report = {
dataSourceType: "json",
data: jsonData,
rows: [{ uniqueName: "M" }],
columns: [{ uniqueName: "country" }, { uniqueName: "[Measures]" }],
measures: [{ uniqueName: "Quantity" }, { uniqueName: "Total", formula: 'sum("Price") * sum("Quantity") ' }],
configuratorActive: false,
viewType: "grid"
};
flexmonster.setReport(report);
}

2 answers

Public
Ian Sadovy Flexmonster January 4, 2016

Hello,
 
Thank you for the request.
As for your code, it’s almost correct.
Please add “calculated” and “caption” properties to the calculated measure as follows:

var report = {
...
measures: [{
uniqueName: "Total",
formula: "sum('Price') * sum('Quantity')",
calculated: true,
caption: "Total"
}],
...
};

Also, here is online sample: https://jsfiddle.net/flexmonster/wytcx783/
 
Regards,
Ian

Public
Tanya Gryshko Flexmonster December 5, 2016

Hello Nestor and all Flexmonster users,
We would like to inform you that after the release of version 2.3 the structure of Report object was changed. Find the new structure in the documentation: http://www.flexmonster.com/api/report-object/. Have a look at the updated example: https://jsfiddle.net/flexmonster/xp50d0wn/.
Regards,
Tanya

Please login or Register to Submit Answer