Grails Flot plugin Example server data not working - grails

I just included the Flot plugin and tried running the sample as mentioned in the documentation.
As per the documentation, the index page looks like this
class FlotController {
def index = {
[data: [[0, 10], [4, 5], null, [6, 2.5], [12, 10]]]
}
}
When rendered in GSP, it
var d4 = \u005b\u005b0\u002c 10\u005d\u002c \u005b4\u002c 5\u005d\u002c null\u002c \u005b6\u002c 2.5\u005d\u002c \u005b12\u002c 10\u005d\u005d;
what is the correct way to define data in the Controller? Thanks
The javascript is as shown below
<g:javascript>
var d1 = [];
for (var i = 0; i < 14; i += 0.5)
d1.push([i, Math.sin(i)]);
var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];
var d3 = [[0, 12], [7, 12], null, [7, 2.5], [12, 2.5]];
var d4 = ${raw(data)}; //line causing the problem
var data = [d1, d2, d3, { label: "server data", data: d4}];
var options = { lines: { show: true }, points: { show: true } };
var d5 = [];
var series = Math.floor(Math.random()*10)+1;
for( var i = 0; i < series; i++) {
d5[i] = { label: "Series"+(i+1), data: Math.floor(Math.random()*100)+1 }
}
var pieOptions = { series: { pie: { show: true } }, legend: { show: false } };
</g:javascript>

EDIT (with the good answer):
For those who have the resources plugin, you need to replace :
<g:javascript></g:javascript>
by
<r:script>...</r:script>
And it works (with or without the function raw()
Previous answer:
In your GSP, you can use the raw() function.
More information here: documentation
But watch out:
"So what happens when you want to stop Grails from escaping some content? There are valid use cases for putting HTML into the database and rendering it as-is, as long as that content is trusted. In such cases, you can tell Grails that the content is safe as should be rendered raw, i.e. without any escaping:"

Related

Use reduce method in dart to group consecutive elements in the list like in Javascript

I have following codes in javascript
var ArrayLogs = [1, 3, 5, 4, 9, 11, 0, -4, -10];
var newLogArrays = [];
ArrayLogs.reduce(function(result, value, index, array) {
if (index % 2 === 0){
newLogArrays.push(array.slice(index, index + 2));
}
return newLogArrays;
}, []);
Above method outputs:
[[1,3],[5,4],[9,11],[0,-4],[-10]]
I am looking equivalent code in Dart, I know there is reduce method in dart as well, but I am not sure how to use to get similar result as javascript.
Any help will be highly appreciated.
Thanks
If you just want to do it using Dart only, you can do something like the following:
void main() {
final arrayLogs = [1, 3, 5, 4, 9, 11, 0, -4, -10];
final result = arrayLogs.fold<List<List<int>>>([], (list, element) {
if (list.isEmpty || list.last.length > 1) {
return list..add([element]);
} else {
return list..last.add(element);
}
});
print(result);
// [[1, 3], [5, 4], [9, 11], [0, -4], [-10]]
}
I am sure there are some packages which can do this more automatically or cleaner.

Removing duplicates from list of lists

I'm having issues attempting to remove duplicate lists within a list. For example, using the list below, I need to remove one of the [2, 9] elements from the list.
List listOfLists = [[1, 5], [2, 9], [10, 12], [-1, 4], [2, 9]];
I have tried using toSet, but it only seems to work with a list of int or strings:
List uniqueLists = listOfLists.toSet().toList();
I have been stuck on this all morning, I'm sure its something simple, but the solution isn't coming to me. Any help would be appreciated.
The issue is that == for lists is reference equality, not based on the value of the lists.
for example if you run this program:
void main() {
print([2, 9] == [2, 9]);
}
it will print false.
You can create a HashSet (or LinkedHashSet if you want to preserve the order) with custom equals and hashcode functions.
import 'dart:collection';
import 'package:collection/collection.dart';
void main() {
List<List<int>> listOfLists = [
[1, 5],
[2, 9],
[10, 12],
[-1, 4],
[2, 9],
];
Set<List<int>> uniqueLists = HashSet<List<int>>(
equals: const ListEquality().equals,
hashCode: const ListEquality().hash,
)..addAll(listOfLists);
print(uniqueLists);
}
This solution uses the collection package for the ListEquality class.
This solution traverses the list backwards, and removes the last item when a duplicate is found. To compare lists, _listsAreEqual is used which is taken from this answer.
Less elegant compared to other solutions, but it does not require any additional packages.
bool _listsAreEqual(list1, list2) {
var i=-1;
return list1.every((val) {
i++;
if (val is List && list2[i] is List) {
return _listsAreEqual(val,list2[i]);
} else {
return list2[i] == val;
}
});
}
List _removeDuplicates(list) {
for (var i=list.length-1; i>0; i--) {
for (var j=i-1; j>=0; j--) {
if (_listsAreEqual(list[i], list[j])) {
list.removeAt(i);
break;
}
}
}
return list;
}
void main(){
List listOfLists = [[1, 5], [2, 9], [10, 12], [-1, 4], [2, 9]];
List uniqueLists = _removeDuplicates(listOfLists);
//List uniqueLists = _removeDuplicates(List.from(listOfLists)); // leave original list unmodified
print(uniqueLists);
}
If you do not want the original list to be modified, use the commented statement instead to create a copy of the original list.
Thank you for your help. Not sure why I didn't find this previously, but this does the trick perfectly.
List uniqueList = listOfLists.map((f) => f.toString()).toSet().toList().map((f) => json.decode(f) as List<dynamic>).toList();

title on vAxis isn't set but displayed on hAxis

I'm using Google Charts to display a SCATTER chart and the vAxis title is not displayed.
When I go on my chart graphic editor, I can see that my hAxis title is set but vAxis not.
For testing you can try with this simple code that reproduced the problem :
function test1() {
var app = SpreadsheetApp;
var classeur = app.getActiveSpreadsheet();
var sheet = classeur.getActiveSheet();
var range = sheet.getRange("A1:B7");
var data = [
['Age', 'Weight'],
[ 8, 12],
[ 4, 5.5],
[ 11, 14],
[ 4, 5],
[ 3, 3.5],
[ 6.5, 7]
];
range.setValues(data);
var chart = sheet.newChart()
.setChartType(Charts.ChartType.SCATTER)
.addRange(range)
.setPosition(5, 5, 0, 0)
.setOption('title','title')
.setOption('width', 300)
.setOption('height', 400)
.setOption('hAxis', {title: 'labelX'})
.setOption('vAxis', {title: 'labelY'})
.build()
sheet.insertChart(chart);
}
Thanks in advance for your help !

Adding data to Highcharts after initial rendering

I'm using Highcharts and initializing the data using;
var graph = Highcharts.chart('container', {
................
series: [{
color: 'red',
data: [[5, 2], [800, 2], [801, 1],[802, 3],[803, 2],[804, 2],[1200, 3]]
},
.............
}
The problem I have is when I want to add more data dynamically, after rendering the initial chart.
In this case I'm using the update;
graph.update(
{
series: [{
color: 'blue',
data: [[100, 2], [101, 2], [102, 1] ]
},
});
And this works but it replaces the entire data set.
So, how should I use the update() function to add points and not replace them?
I saw people are using other functions such as addPoint() and setData() but I couldn't make them work properly...
Thanks in advance
Gus
Refer to this live demo: http://jsfiddle.net/kkulig/opr4Lgpe/
I add all the points in a loop. redraw argument (second one) of the addPoint function is set to false - there's no need to redraw the chart after each addition. The redraw is performed only once in the end.
var chart = Highcharts.chart('container', {
series: [{
data: [1, 2]
}]
});
var pointsToAdd = [4, 5, 6, 6, 7];
pointsToAdd.forEach(function(p) {
chart.series[0].addPoint(p, false);
});
chart.redraw();
API reference:
https://api.highcharts.com/class-reference/Highcharts.Series#addPoint

Iterating over a complex collection in Dart

I would like to be able to iterate over this collection.
import "package:collection/collection.dart";
main() {
EqualityMap edges = new EqualityMap.from(const ListEquality(), {
[1, 'a']: [2, 3],
[2, 'a']: [2],
[3, 'b']: [4, 3],
[4, 'c']: [5]
});
}
I am doing a recursive function so this is not allowed.
edges.keys.forEach((m) {
// some more code
return something;
});
Is it possible to achieve something similar to this?
for(var edge in edges)
Something like this should work:
for (var edgeKey in edges.keys) {
var edge = edges[edgeKey];
// do something
}

Resources