How To Access 'List<Map<String, Object>>' Elements In Dart? - dart

How Can I Print 'White' Element From The Given Code ?
void main() {
var a = [
{
'question': "Which is your favourite colour ?",
'answer': ['Blue', 'White', 'Black', 'Pink']
}
];
print(`Enter Code Here`);
}

Well, the hacky, answer that will solve you immediate problem is
void main() {
var a = [
{
'question': "Which is your favourite colour ?",
'answer': ['Blue', 'White', 'Black', 'Pink']
}
];
print((a[0]['answer'] as Iterable).elementAt(1));
}
However, since you probably not only have this very specific problem to solve, you may want to look into json serialization techniques.

Related

Group a Searchkick result?

I have a basic Searchkick system set-up. I want to take the results and then group them by an attribute to sum a another attribute etc.
This question is close to my issue:
Elasticsearch + searckick
and the only answer was to use aggregations. I could do that but then I would be building an active record call for each of the agg keys returned.
Here is what I have so far:
BudgetItem.all.search("*", body_options: { aggs: { cbs_item_id: { terms: { field: "cbs_item_id" }, aggs: { "total": { "sum": { "field": "total" } } } } } } )
which results in:
"aggregations"=>{"cbs_item_id"=>{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>[{"key"=>5, "doc_count"=>2, "total"=>{"value"=>2956.0}}, {"key"=>6, "doc_count"=>2, "total"=>{"value"=>7734.0}}]}}}>
in my search_data I have a term 'cbs' which is a text value that relates to the 'cbs_item_id'. I am looking for this result:
"aggregations"=>
{"cbs_item_id"=>
{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>
[{"key"=>5, "doc_count"=>2, "total"=>{"value"=>2956.0}, "cbs"=>{"value"=>"MY CBS Related Field" }},
{"key"=>6, "doc_count"=>2, "total"=>{"value"=>7734.0}, "cbs"=>{"value"=>"MY OTHER CBS Related Field" }}]}}}
This of this where you have in inventory of cars and a separate table of car_colors ( [id = 1, color = red], [id = 3, color = blue ]. I want to search for the cars of a given color then group them and sum etc.
I am sure I am perhaps missing something simple here.
UPDATE
Getting close:
BudgetItem.all.search("*", body_options: { aggs: { cbs_item_id: { terms: { field: "cbs_item_id" }, aggs: { cbs: { terms: { field: "cbs" } }, "total": { "sum": { "field": "total" } } } } } } )
which results:
"buckets"=>
[{"key"=>5, "doc_count"=>2, "total"=>{"value"=>2956.0}, "cbs"=>{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>[{"key"=>"001", "doc_count"=>2}]}},
{"key"=>6, "doc_count"=>2, "total"=>{"value"=>7734.0}, "cbs"=>{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>[{"key"=>"002", "doc_count"=>2}]}}]}}
The second "key"s 001 and 002 are the data I am looking for.

VictoryPie click single series of chart at a time

I am using VictoryPie on react native (victory-native). My goal is to be able to change the color of one slice of the pie chart onClick (onPress). At any one time the color of only a single slice should be changed to my highlight color.
Using the events prop, I am able to change the color of a slice onPress, but not able to reset it upon clicking on another slice using code like this:
onPress: () => {
return [
{
target: 'data',
mutation: (props) => {return {style: {...props.style, fill: #000000}}}
}
}
Ideally I would like to use the additional eventKey prop to be able to return {style: undefined} for the other slices. But I am not able to determine how to get the array of other elements for the eventKey prop. The onPress does not have any argument stating the index or element. Is there some other way that I can know which item was clicked inside the onPress function?
Thanks for the help in advance!
I was also stuck on the same thing. Here's the code of how I solved that issue. Hope this helps 😊.
<VictoryPie
standalone={false}
labelRadius={({ innerRadius }) => innerRadius + 25}
labelPlacement={({ index }) => {
return "perpendicular";
}}
labelComponent={
<VictoryLabel
style={[
{
fill: Colors.white,
fontSize: 15,
fontFamily: "Bold",
},
]}
/>
}
style={{
data: {
fillOpacity: 1,
stroke: Colors.white,
strokeWidth: 5,
fill: ({ datum, index }) => {
let data = emotions.map((item, index) => {
return item;
});
if (
selectedEmotion.emotion === data[index].emotion_name
) {
return "tomato";
}
return data[index].background_color;
},
},
}}
events={[
{
target: "data",
eventHandlers: {
onPressOut: () => {
return [
{
target: "data",
mutation: (props) => {
let { x, description, id } = props.datum;
setSelectedEmotion({
emotion_id: id,
emotion: x,
description: description,
});
},
},
];
},
},
},
]}
data={EmotionsData}
width={450}
height={450}
colorScale={graphicColorData}
innerRadius={100}
/>
Didn't find it in documentation but searching online, see that you can use props as an argument in the onClick/onPress
you can just first check fill props like this in the mutation object
mutation: (props) => {
const fill = props.style && props.style.fill;
return fill === "#7CFC00" ? null : { style: { fill: "#7CFC00" } };
}

Nearley grammar matches the same bit of text as a terminal and a non-terminal one after the other, producing wrong result

Grammar noob here.
I need to parse math formulas similar to those accepted by SymPy and transform them into some kind of left-to-right syntax tree, using Nearley this grammar.
The problem appears when I have an expression like a*sin(x)*y where sin is first recognised as a SY, and then as a FN. I think this is sort of a necessary evil if I want to be able to parse variables (that's what SY is for). The result is something like
[ { type: 'Symbol',
properties: { letter: 'a' },
children:
{ right:
{ type: 'Symbol',
properties: { letter: 'sin' },
children:
{ right:
{ type: 'Fn',
properties: { name: 'sin' },
children:
{ argument: { type: 'Symbol', properties: { letter: 'x' }, children: {} },
right: { type: 'Symbol', properties: { letter: 'y' }, children: {} } } } } } },
position: { x: 200, y: 200 } } ]
Even worse, when the expression is a*sin(x)^y, I get
[ { type: 'Symbol',
properties: { letter: 'a' },
children:
{ right:
{ type: 'Symbol',
properties: { letter: 'sin' },
children:
{ right:
{ type: 'Fn',
properties: { name: 'sin' },
children:
{ argument: { type: 'Symbol', properties: { letter: 'x' }, children: {} },
superscript: { type: 'Symbol', properties: { letter: 'y' }, children: {} },
right: [Circular] } } } } },
position: { x: 200, y: 200 } } ]
I presume [Circular] means there's some sort of wicked loop somewhere.
I suspect I could resolve the first issue above hardcoding a check that replaces the SY with the correct FN if the two "match", but I'd rather avoid such a botch-up. I have no clue as to what's happening with the second one -- though I have been on this for a full day and my mind is likely clouded. I'll investigate more once I get to the office today.
Any clues?
EDIT: I managed to "solve" the first issue (Fn as a child of a Symbol of the same name) with a horrible hack. The circular problem remains. I'm investigating, but I am probably going to find another horrible hack. I'd rather see a fix for the grammar rather than for the transformation functions, if at all possible.

Customizing the tooltip with chartkick in Ruby on Rails

I'm doing a rails app and was able to get chartkick working but customizing it is a bit of a pain in the butt. I'm trying to customize the tooltip with the Google chart API but I can't seem to figure out. I just want to be able to change the text, "Value".
Could someone put me in the right direction.
Here is my code:
<%=
column_chart [
["Strongly Disagree", p[1]],
["Disagree", p[2]],
["Neutral", p[3]],
["Agree", p[4]],
["Strongly Agree", p[5]]
],
height: "220px",
library: {
width: 665,
fontName: "Helvetica Neue",
colors: ["#29abe2"],
tooltip: {
textStyle: {
color: "#333333"
}
},
bar: {
groupWidth: "50%"
},
vAxis: {
title: "Everyone",
titleTextStyle: {
italic: false,
color: '#777'
},
gridlines: {
color: "#eeeeee"
},
viewWindow: {
max: m
}
}
}
%>
The chartkick documentation is sparse, but if this system works the way I suspect it does, then you need to add an array of column headers to the start of your data, eg:
[
["Label for column 0", "Label for column 1"],
["Strongly Disagree", p[1]],
["Disagree", p[2]],
["Neutral", p[3]],
["Agree", p[4]],
["Strongly Agree", p[5]]
]

fusion table style with multiple styles

I'm trying to apply multiple styles to a Google Fusion Table Layer.
This works and colors all polygons that are in an array to the blue color:
layer = new google.maps.FusionTablesLayer({
map : map,
query : {
select : "geometry",
from : "1gwSN6n_00uZ7YuAP7g4FiUiilybqDRlRmWJrpvA"
},
styles: [{
polygonOptions:
{
fillColor: "#ffffff",
strokeColor: "#bcbcbc",
fillOpacity: ".75"
}
},
{
where: whereClause,
polygonOptions: {
fillColor: "#0D58A6"
}
}
]
});
layer.setMap(map);
But this doesn't work -- no polygons even appear on my map:
layer = new google.maps.FusionTablesLayer({
map : map,
query : {
select : "geometry",
from : "1gwSN6n_00uZ7YuAP7g4FiUiilybqDRlRmWJrpvA"
},
styles: [{
polygonOptions:
{
fillColor: "#ffffff",
strokeColor: "#bcbcbc",
fillOpacity: ".75"
}
},
{
where: whereClause,
polygonOptions: {
fillColor: "#0D58A6"
}
}
,
{
where: whereClause,
polygonOptions: {
fillColor: "#ff0000"
}
}
]
});
layer.setMap(map);
Never mind that I'm coloring the same thing one color and then another--I just want the second style to work. When I take out the second style, all is fine. I put in the second style--and no polygons any more.
Can someone tell me what I'm doing wrong, please?
This may very well be an error (the exact same where clause with two different styles):
{
where: whereClause,
polygonOptions: {
fillColor: "#0D58A6"
}
}
,
{
where: whereClause,
polygonOptions: {
fillColor: "#ff0000"
}
}
I would expect the where clause to have to be unique.
UPDATE:
My current guess is that you are running into a query limit on the size of the data sent back to the server.
Each of the whereClauses works independently
If this is the issue (the query string is too big), mapping the "COUSUBFP" codes to something shorter (3 decimal digits or two hex digits), might make it work (or for that matter, just truncating the leading "0's").
03320 -> 0
03608 -> 1
etc.

Resources