How to sort the items within each stacking column? - highcharts

How to sort the items within each stacking column? Asc or desc.

Each series added to a chart is drawn on the chart in the order it was received. To change the order of the chart series you will need to change which series is the first in your list of series items.
That being said - what I think you want to do is to, independently of the series order, sort on each stack by value. I do not think this is possible in HighCharts.

You can only set global index of serie, but you cannot position each single "stack".
http://api.highcharts.com/highcharts#series.index

You may use the script below to sort the Stacked Chart Bars by category name.
var sortData = function(chartSource) {
var series = chartSource.series;
var axis = chartSource.xAxis[0];
var categories = [];
if($.isArray(series)) {
var ser =
$.grep(series, function(ser, seriesIndex)
{
return ser.visible;
})[0];
$.each(ser.data,
function(dataIndex, datum)
{
console.log(datum.category + ':' + datum.stackTotal);
var obj = {
name: datum.category,
index: dataIndex,
stackTotal: datum.stackTotal
}
categories.push(obj);
}
);
}
categories.sort(function(a, b) {
var aName = a.name.toLowerCase();
var bName = b.name.toLowerCase();
var aTotal = a.stackTotal;
var bTotal = b.stackTotal;
//if(aTotal === bTotal) {
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
//} else {
// return ((aTotal > bTotal) ? -1 : ((aTotal < bTotal) ? 1 : 0));
//}
});
var mappedIndex = $.map(categories, function(category, index) {
return category.index;
});
categories = $.map(categories, function(category, index) {
return category.name;
});
console.log(categories);
console.log(mappedIndex);
axis.setCategories(categories);
var newDataArray = [];
$.each(series, function(seriesIndex, ser) {
newDataArray = [];
var data = $.map(mappedIndex, function(mappedIndex2, origIndex) {
var ydata = ser.data[mappedIndex2];
if(ydata.y!=null){
var y = ydata.y
newDataArray.push(y);
return y;
}
else
{
newDataArray.push(null);
return null;
}
});
ser.setData(newDataArray);
});
};

Related

Highcharts Showing Uncaught TypeError: Cannot read properties of undefined (reading 'chart')

I've had this HighCharts spider chart working fine for a while now, but we upgraded to the latest HighCharts code and I noticed that the mouseovers are no longer working. My PHP code looks like this:
// Create a new Highchart
$chart = new Highchart();
$chart->includeExtraScripts();
$chart->chart->renderTo = "control_maturity_spider_chart";
$chart->chart->polar = true;
$chart->chart->type = "line";
$chart->chart->width = 1000;
$chart->chart->height = 1000;
$chart->title->text = "Current vs Desired Maturity by Control Family";
$chart->title->x = -80;
$chart->pane->size = "80%";
$chart->xAxis->categories = $categories;
$chart->xAxis->tickmarkPlacement = "on";
$chart->xAxis->lineWidth = 0;
$chart->yAxis->gridLineInterpolation = "polygon";
$chart->yAxis->lineWidth = 0;
$chart->yAxis->min = 0;
$chart->yAxis->max = 5;
$chart->yAxis->tickInterval = 1;
$chart->tooltip->shared = true;
$chart->tooltip->pointFormat = '<span style="color:{series.color}">{series.name}: <b>{point.y}</b><br/>';
$chart->legend->align = "center";
$chart->legend->verticalAlign = "top";
$chart->legend->layout = "vertical";
// Draw the Current Maturity series
$chart->series[0]->name = $escaper->escapeHtml($lang['CurrentControlMaturity']);
$chart->series[0]->data = empty($categories_current_maturity_average) ? [] : $categories_current_maturity_average;
$chart->series[0]->pointPlacement = "on";
// Draw the Desired Maturity series
$chart->series[1]->name = $escaper->escapeHtml($lang['DesiredControlMaturity']);
$chart->series[1]->data = empty($categories_desired_maturity_average) ? [] : $categories_desired_maturity_average;
$chart->series[1]->pointPlacement = "on";
$chart->credits->enabled = false;
echo "<figure class=\"highcharts-figure\">\n";
echo " <div id=\"control_maturity_spider_chart\"></div>\n";
echo "</figure>\n";
echo "<script type=\"text/javascript\">";
echo $chart->render("control_maturity_spider_chart");
echo "</script>\n";
The actual chart renders just fine, but if you mouse over it, you just get this message in the javascript console over and over again:
HighCharts Error Message
If we comment out these two lines of code, the mouseover works:
$chart->tooltip->shared = true;
$chart->tooltip->pointFormat = '<span style="color:{series.color}">{series.name}: <b>{point.y}</b><br/>';
Any thoughts on what we are doing wrong here, or what changed, would be greatly appreciated. Thank you.
this is the bug which you can track here: https://github.com/highcharts/highcharts/issues/17472
As a temporary workaround, add the following wrap function to your code:
(function(H) {
const isObject = H.isObject;
H.Pointer.prototype.findNearestKDPoint = function(series, shared, e) {
var chart = this.chart;
var hoverPoint = chart.hoverPoint;
var tooltip = chart.tooltip;
if (hoverPoint &&
tooltip &&
tooltip.isStickyOnContact()) {
return hoverPoint;
}
var closest;
/** #private */
function sort(p1, p2) {
var isCloserX = p1.distX - p2.distX,
isCloser = p1.dist - p2.dist,
isAbove = ((p2.series.group && p2.series.group.zIndex) -
(p1.series.group && p1.series.group.zIndex));
var result;
// We have two points which are not in the same place on xAxis
// and shared tooltip:
if (isCloserX !== 0 && shared) { // #5721
result = isCloserX;
// Points are not exactly in the same place on x/yAxis:
} else if (isCloser !== 0) {
result = isCloser;
// The same xAxis and yAxis position, sort by z-index:
} else if (isAbove !== 0) {
result = isAbove;
// The same zIndex, sort by array index:
} else {
result =
p1.series.index > p2.series.index ?
-1 :
1;
}
return result;
}
series.forEach(function(s) {
var noSharedTooltip = s.noSharedTooltip && shared,
compareX = (!noSharedTooltip &&
s.options.findNearestPointBy.indexOf('y') < 0),
point = s.searchPoint.call(s.polar, e, compareX);
if ( // Check that we actually found a point on the series.
isObject(point, true) && point.series &&
// Use the new point if it is closer.
(!isObject(closest, true) ||
(sort(closest, point) > 0))) {
closest = point;
}
});
return closest;
};
}(Highcharts))
Demo:
https://jsfiddle.net/BlackLabel/8b2mhqf0/

Convert google sheet Row and column in JSON using app script

Is there any way to convert this=> google sheet into the below given JSON object using AppScript. I'm new to AppScript I dont have idea how to make this row and column in the below given object. Thanks in advance.
{
"data":[
{
"insurer":"CompanyName1",
"products":[
{
"name":"product1",
"UIN":"104N079V01"
},
{
"name":"product2",
"UIN":"104N079V02"
}
]
},
{
"insurer":"CompanyName2",
"products":[
{
"name":"product1",
"UIN":"104N079V01"
},
{
"name":"product2",
"UIN":"104N079V02"
}
]
}
]
}
Try
function data2json() {
var sh = SpreadsheetApp.getActiveSheet()
var values = sh.getRange('A1').getDataRegion().getValues()
// Logger.log(values)
var jsn = {}
jsn['data'] = []
var n = -1
var m = 0
values.forEach(function (r, i) {
if (i > 0) {
if (r[1] != '') {
n++
jsn['data'][n] = {}
jsn['data'][n]['insurer'] = r[1]
jsn['data'][n]['products'] = []
m = -1
}
m++
jsn['data'][n]['products'][m] = {}
jsn['data'][n]['products'][m]['name'] = r[2]
jsn['data'][n]['products'][m]['UIN'] = r[3]
}
})
Logger.log(JSON.stringify(jsn))
}
example
result
{"data":[{"insurer":"CompanyName1","products":[{"name":"product1","UIN":"A"},{"name":"product2","UIN":"B"},{"name":"product3","UIN":"C"},{"name":"product4","UIN":"D"}]},{"insurer":"CompanyName2","products":[{"name":"product5","UIN":"E"},{"name":"product6","UIN":"F"},{"name":"product7","UIN":"G"},{"name":"product8","UIN":"H"}]},{"insurer":"CompanyName3","products":[{"name":"product9","UIN":"I"},{"name":"product10","UIN":"J"},{"name":"product11","UIN":"K"}]}]}

Joining tables in queries google sheets

Basically I have two google sheets that look something like this:
a table where people can put in their email and select what kind of foo they are using
email
foo
example#email.com
This Foo
and then another table with information about the foo
foo name
foo type
foo boolean1
foo boolean2
This Foo
String
True
True
That Foo
Number
False
True
Other Foo
String
False
False
In a Separate Sheet I'd like to have a dashboard-like view of things wherein I would have counts of various things like number of people, how many of each type of Foo, etc
Where I'm having trouble is figuring out how to pull things like "Number of people who have selected String foos" and such
like, basically i want the google-query equivalent to (in sql)
SELECT COUNT(p.*) FROM people p JOIN info i on p.foo = i.foo_name GROUP BY i.foo_type WHERE i.foo_type = 'String'
What I would be looking for is a table that looks like this:
Data
Count
Active Roster
4
String
3
Number
1
I have also seen many solutions that have complicated formulas using VLOOKUP, INDEX, MATCH, etc.
I decided to write a user function to combine tables, or as I refer to it, de-normalize the database. I wrote the function DENORMALIZE() to support INNER, LEFT, RIGHT and FULL joins. By nesting function calls one can join unlimited tables in theory.
DENORMALIZE(range1, range2, primaryKey, foreignKey, [joinType])
Parameters:
range1, the main table as a named range, a1Notation or an array
range2, the related table as a named range, a1Notation or an array
primaryKey, the unique identifier for the main table, columns start with "1"
foreignKey, the key in the related table to join to the main table, columns start with "1"
joinType, type of join, "Inner", "Left", "Right", "Full", optional and defaults to "Inner", case insensitive
Returns: results as a two dimensional array
Result Set Example:
=QUERY(denormalize("Employees","Orders",1,3), "SELECT * WHERE Col2 = 'Davolio' AND Col8=2", FALSE)
EmpID
LastName
FirstName
OrderID
CustomerID
EmpID
OrderDate
ShipperID
1
Davolio
Nancy
10285
63
1
8/20/1996
2
1
Davolio
Nancy
10292
81
1
8/28/1996
2
1
Davolio
Nancy
10304
80
1
9/12/1996
2
Other Examples:
=denormalize("Employees","Orders",1,3)
=denormalize("Employees","Orders",1,3,"full")
=QUERY(denormalize("Employees","Orders",1,3,"left"), "SELECT * ", FALSE)
=QUERY(denormalize("Employees","Orders",1,3), "SELECT * WHERE Col2 = 'Davolio'", FALSE)
=QUERY(denormalize("Employees","Orders",1,3), "SELECT * WHERE Col2 = 'Davolio' AND Col8=2", FALSE)
=denormalize("Orders","OrderDetails",1,2)
// multiple joins
=denormalize("Employees",denormalize("Orders","OrderDetails",1,2),1,3)
=QUERY(denormalize("Employees",denormalize("Orders","OrderDetails",1,2),1,3), "SELECT *", FALSE)
=denormalize(denormalize("Employees","Orders",1,3),"OrderDetails",1,2)
=QUERY(denormalize("Employees",denormalize("Orders","OrderDetails",1,2),1,3), "SELECT *", FALSE)
=QUERY(denormalize(denormalize("Employees","Orders",1,3),"OrderDetails",4,2), "SELECT *", FALSE)
function denormalize(range1, range2, primaryKey, foreignKey, joinType) {
var i = 0;
var j = 0;
var index = -1;
var lFound = false;
var aDenorm = [];
var hashtable = [];
var aRange1 = "";
var aRange2 = "";
joinType = DefaultTo(joinType, "INNER").toUpperCase();
// the 6 lines below are used for debugging
//range1 = "Employees";
//range1 = "Employees!A2:C12";
//range2 = "Orders";
//primaryKey = 1;
//foreignKey = 3;
//joinType = "LEFT";
// Sheets starts numbering columns starting with "1", arrays are zero-based
primaryKey -= 1;
foreignKey -= 1;
// check if range is not an array
if (typeof range1 !== 'object') {
// Determine if range is a1Notation and load data into an array
if (range1.indexOf(":") !== -1) {
aRange1 = ss.getRange(range1).getValues();
} else {
aRange1 = ss.getRangeByName(range1).getValues();
}
} else {
aRange1 = range1;
}
if (typeof range2 !== 'object') {
if (range2.indexOf(":") !== -1) {
aRange2 = ss.getRange(range2).getValues();
} else {
aRange2 = ss.getRangeByName(range2).getValues();
}
} else {
aRange2 = range2;
}
// make similar structured temp arrays with NULL elements
var tArray1 = MakeArray(aRange1[0].length);
var tArray2 = MakeArray(aRange2[0].length);
var lenRange1 = aRange1.length;
var lenRange2 = aRange2.length;
hashtable = getHT(aRange1, lenRange1, primaryKey);
for(i = 0; i < lenRange2; i++) {
index = hashtable.indexOf(aRange2[i][foreignKey]);
if (index !== -1) {
aDenorm.push(aRange1[index].concat(aRange2[i]));
}
}
// add left and full no matches
if (joinType == "LEFT" || joinType == "FULL") {
for(i = 0; i < lenRange1; i++) {
//index = aDenorm.indexOf(aRange1[i][primaryKey]);
index = aScan(aDenorm, aRange1[i][primaryKey], primaryKey)
if (index == -1) {
aDenorm.push(aRange1[i].concat(tArray2));
}
}
}
// add right and full no matches
if (joinType == "RIGHT" || joinType == "FULL") {
for(i = 0; i < lenRange2; i++) {
index = aScan(aDenorm, aRange2[i][foreignKey], primaryKey)
if (index == -1) {
aDenorm.push(tArray1.concat(aRange2[i]));
}
}
}
return aDenorm;
}
function getHT(aRange, lenRange, key){
var aHashtable = [];
var i = 0;
for (i=0; i < lenRange; i++ ) {
//aHashtable.push([aRange[i][key], i]);
aHashtable.push(aRange[i][key]);
}
return aHashtable;
}
function MakeArray(length) {
var i = 0;
var retArray = [];
for (i=0; i < length; i++) {
retArray.push("");
}
return retArray;
}
function DefaultTo(valueToCheck, valueToDefault) {
return typeof valueToCheck === "undefined" ? valueToDefault : valueToCheck;
}
// Search a multi-dimensional array for a value
function aScan(aValues, searchStr, searchCol) {
var retval = -1;
var i = 0;
var aLen = aValues.length;
for (i = 0; i < aLen; i++) {
if (aValues[i][searchCol] == searchStr) {
retval = i;
break;
}
}
return retval;
}
You can make a copy of the google sheet with data and examples here:
https://docs.google.com/spreadsheets/d/1vziuF8gQcsOxTLEtlcU2cgTAYL1eIaaMTAoIrAS7mnE/edit?usp=sharing

Binding row value to odata model SAP UI5

I have a table with 2 columns - one where user can input a value to return and other column is a checkbox. If a user enters a value in the item row, I make the checkbox checked. If value is greater than 0 then only the checkbox is selected. My issue is with the below code, if I input a value on the 3 rd row, that checkbox is selected but alongside even the first row's checkbox is selected. I think the issue is in the stmt: tableModel.setProperty("/ItemSet/results/0/ReturnItemFlag", "X"); Because I am giving "0" the first row is also getting the value. How to I point to the correct result number.
Controller.js
qtyChange: function(oEvent) {
var a = oEvent.getSource();
var input = a.getValue()
var row = oEvent.getSource().getParent().getParent();
var index = row.getIndex();
var oTable = vc.getView().byId("takeStockHistoryDetailTable");
var selectedRowPath = oTable.getContextByIndex(index).getPath();
var tableModel = vc.getView().getModel(TAKE_STOCK_ORDER_DETAIL);
var selectedPart = tableModel.getObject(selectedRowPath);
var QtyOnHand = selectedPart.QtyOnHand;
var UnitP = selectedPart.UnitPrice;
var f = parseInt(input);
var g = parseInt(QtyOnHand);
var h = parseFloat(UnitP);
if (f > g) {
sap.m.MessageToast.show("Return quantity is more than available quantity");
a.setValue("");
} else if (f === 0 || input === "") {
selectedPart.ReturnItemFlag = 'Y';
tableModel.setProperty("/ItemSet/results/0/ReturnItemFlag", "Y");
} else {
selectedPart.ReturnItemFlag = 'X';
selectedPart.QtyToReturn = input;
var sub = input * h;
// debugger;
var sub1 = sub.toString();
selectedPart.Subtotal = sub1;
tableModel.setProperty("/ItemSet/results/0/ReturnItemFlag", "X");
tableModel.setProperty("/ItemSet/results/0/Subtotal", sub1);
}
},
This is possibly a very complicated way of working with table items.
Here is how you should work with bindingContexts.
on listItemPress Event of the table(list)
qtyChange: function(oEvent){
var oColumnListItem = oEvent.getSource().getParent();
var sPath = oColumnListItem.getBindingContextPath("yourModelName");
OR
var sPath = oColumnListItem.getBindingContext("yourModelName").getPath();
var sReturnItemFlagPath = sPath + "/ReturnItemFlag";
tableModel.setProperty(sReturnItemFlagPath,"newValue");
}
Let me know if this helps!

How to sort prime-ui datatable, based on more than one column

I want to sort the datatable based on 2 columns. If I use the following property,
{sortField: 'ColumnHeader'}
Its not working.
It will not work with the current primeui (at the time of this answer it is 1.1). Have a look at the sort function:
sort: function(field, order) {
if(this.options.selectionMode) {
this.selection = [];
}
if(this.options.lazy) {
this.options.datasource.call(this, this._onLazyLoad, this._createStateMeta());
}
else {
this.data.sort(function(data1, data2) {
var value1 = data1[field],
value2 = data2[field],
result = (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0;
return (order * result);
});
if(this.options.selectionMode) {
this.selection = [];
}
if(this.paginator) {
this.paginator.puipaginator('option', 'page', 0);
}
this._renderData();
}
},
As you can see it uses the Array.prototype.sort() function and accesses the field-to
var value1 = data1[field],
value2 = data2[field],
Maybe you can override this particular function and use your own sort method instead.
http://www.primefaces.org/primeui/#datatableSort
Its available in Prime UI version 4.1.3

Resources