Angular-Chart property to labels and value to data - angular-chart

I am trying to map a result to an angular-chart but am confused on how to map my labels and map my data. My service returns an object that looks like this:
Object {0: 5, 4: 1, 9: 1, 14: 2}
Question
How do I map my labels to the 0, 4, 9, 14 so that I can use it to put into a bar graph and data like below:
angular.module("app", ["chart.js"]).controller("BarCtrl", function ($scope) {
$scope.labels = ['0', '4', '9', '14'];
$scope.data = [
[5, 1, 1, 2]
];
});
Question 2 -- instead of using the numbers can I type in names for the labels?

I resolved this by mapping the value to an array.
var array = $.map($scope.count, function (value, index) {
return [value];
});
console.log(array);
Then using array on my chart.

Related

(Godot Engine) How do I know which exported enum flags are enabled in script

By using the Godot engine and writing in the GDScript language,
let's say I have an enum declared as:
enum eTextMode {CHAR, NUMBER, SYMBOLS_TEXT, SYMBOLS_ALL}
And an export variable as:
export(eTextMode, FLAGS) var _id: int = 0
In the inspector panel I can see which flag is selected or not, but how can I know in code which specifically flag is selected?
By selecting in the inspector, for example: the NUMBER and SYMBOLS_TEXT flags, the _id variable will be set as 5
My approach is the following hard-coded dictionary:
var _selected_flags: Dictionary = {
CHAR = _id in [1, 3, 5, 7, 9, 11, 13, 15],
NUMBER = _id in [2, 3, 6, 7, 10, 11, 14, 15],
SYMBOLS_TEXT = _id in [4, 5, 6, 7, 12, 13, 14, 15],
SYMBOLS_ALL = _id in [8, 9, 10, 11, 12, 13, 14, 15]
}
Resulting in:
{CHAR:True, NUMBER:False, SYMBOLS_ALL:False, SYMBOLS_TEXT:True}
The above result is exactly what I'm expecting (a dictionary with string keys as they are defined in the enum with a boolean value representing the selection state).
How could I manage to do this dynamically for any enum regardless of size?
Thank you very much,
One tacky solution that I could manage is by not using an enum at all, but instead a dictionary like the following example:
const dTextMode: Dictionary = {CHAR = false, NUMBER = false, SYMBOLS_TEXT = false, SYMBOLS_ALL = false}
export(Dictionary) var m_dTextMode: Dictionary = dTextMode setget Set_TextMode, Get_TextMode
func Get_TextMode() -> Dictionary: return m_dTextMode
func Set_TextMode(_data: Dictionary = m_dTextMode) -> void: m_dTextMode = _data
An exported dictionary is not as good-looking as an exported enum with FLAGS, and by following this approach it kind of invalidates my initial problem.
By selecting CHAR and SYMBOLS_TEXT in the exported dictionary from the inspector, and then calling print(self.Get_TextMode()) the result is indeed what I expected:
{CHAR:True, NUMBER:False, SYMBOLS_ALL:False, SYMBOLS_TEXT:True}
I still can't figure out though how to achieve this result by using the export(*enum, FLAGS) aproach.
Edit: also, the setter function is not feasible to be used in script since the user must know to duplicate the dTextMode constant first, edit it and set is as an argument.
Thanks to the comments from #Thearot from my first answer, I have managed to figure out the following solution which meets all expectations, with one caveat: it seems like an overkill solution...
enum eTestFlags {FLAG_1, FLAG_2, FLAG_3, FLAG_5, FLAG_6}
export(eTestFlags, FLAGS) var m_iTestFlags: int = 0 setget Set_TestFlags
func Get_TestFlags() -> Dictionary: return self._get_enum_flags(m_iTestFlags, eTestFlags)
func Set_TestFlags(_id: int = m_iTestFlags) -> void: m_iTestFlags = _id
func _get_enum_flags(_val_selected: int, _enum: Dictionary, _bit_check_limit: int = 32) -> Dictionary:
var _enum_keys: Array = _enum.keys() ; _enum_keys.invert()
var _bin_string: String = ""
var _val_temp: int = 0
var _val_count: int = _bit_check_limit - int(_is_pow2(_bit_check_limit))
while(_val_count >= 0):
_val_temp = _val_selected >> _val_count
_bin_string += "1" if _val_temp & 1 else "0"
_val_count -= 1
var _bin_string_padded: String = "%0*d" % [_enum_keys.size(), int(_bin_string)]
var _result_dict: Dictionary = {}
for _str_id in range(_bin_string_padded.length(), 0, -1):
_result_dict[_enum_keys[_str_id - 1]] = bool(_bin_string_padded[_str_id - 1] == "1")
return _result_dict
func _is_pow2(_value: int) -> bool:
return _value && (not (_value & (_value - 1)))
Now, if I print(self.Get_TestFlags()) after selecting FLAG_2 and FLAG_6 the result is:
{FLAG_1:False, FLAG_2:True, FLAG_3:False, FLAG_5:False, FLAG_6:True}
You're on the right track but overcomplicating things. Without going too much into the math (see Wikipedia), here's what you'd do in Godot:
enum eTextMode {CHAR, NUMBER, SYMBOLS_TEXT, SYMBOLS_ALL}
export(eTextMode, FLAGS) var _id: int = 0
func _ready() -> void:
for modeName in eTextMode:
var bit_flag_value: int = int(pow(2, eTextMode[modeName]))
if _id & bit_flag_value:
printt("Flagged", modeName)
You can access the named fields of your enum like elements in an Array/Dictionary by default (iterate through the keys, get their 0-based index as values). The above math trick turns the 0-based index into the correct bit flag number, and if you (single) '&' it with the combined bit-flags value you can check whether or not that flag is set.

How can i get the values in eloquent relation array result of laravel 8

I have a result array from laravel8 relations. like below
$val = {
"q_id": 1,
"q_text": "1111",
"q_mandatory": 1,
"q_status": "unpublished",
"que_logic_relation": [
{
"ql_id": 1,
"ql_quest_id": 1,
"ql_answer_choice_id": null,
"ql_succeeding_q_order": 3,
},
{
"ql_id": 4,
"ql_quest_id": 1,
"ql_answer_choice_id": null,
"ql_succeeding_q_order": 3,
}
]
}
When I print $val['q_text'] // output 1111
when I print $val['que_logic_relation'] //no result or empty
I want to print the data in que_logic_relation seperately. How can I do that?
Issued fixed by adding toArray() to the query result and everything works fine now.

Make a file hierarchy as a list of edges, then draw the tree structure as a graph

So I want to draw directory structures as tree graphs. I'm using NetworkLayout.jl. I'm stuck on Step 1, but I can do Step 2 and Step 3.
Build a list of links between files and folders, to produce something like this (hand-made) one:
links = Pair[
"/" => "System",
"/" => "Library",
"/" => "Users",
"System" => "sys1",
"System" => "sys2",
"System" => "sys3",
"Library" => "lib1",
"Library" => "lib2",
"Library" => "lib3",
"Users" => "u1",
"Users" => "u2",
"Users" => "u3",
"Users" => "u4",
"Users" => "u5",
"u5" => "MyFolder"]
Create an adjacency list. This is easy enough if all the links are unique (also handmade):
adjlist = [
[2, 3, 4],
[5, 6, 7],
[8, 9, 10],
[],
[11],
[],
[],
[11, 12, 13, 14],
[],
[],
[15],
[],
[],
[],
[]]
Make a layout. This is the easy bit, because NetworkLayout.jl does it all for you:
using NetworkLayout
NetworkLayout.Buchheim.layout(adjlist)
...>
GeometryTypes.Point{2,Float64}[[0.0, -0.0], [-2.0, -2.0], [1.0, -2.0],
[2.0, -2.0], [-3.0, -4.0], [-2.0, -4.0], [-1.0, -4.0], [0.0, -4.0],
[1.0, -4.0], [2.0, -4.0], [-1.5, -6.0], [-0.5, -6.0], [0.5, -6.0],
[1.5, -6.0], [-1.5, -8.0]]
So my question is, in Step 1, how to build the initial list of links between files. Some files may have identical names?
# get links with full path
function read_sub_dirs(path::AbstractString)
try # avoid access issue
a = readdir(path)
return a[isdir.((path*"/").*a)]
catch
return String[]
end
end
function deepreaddir_raw(working_dir_path::AbstractString,search_depth::Int=2)
links = Array{Pair,1}()
previous_dirs = [working_dir_path]
for i in 1:search_depth
if length(previous_dirs) > 0
next_dirs = String[]
for each_dir in previous_dirs
if each_dir[end] == '/'
sub_dirs = each_dir.*read_sub_dirs(each_dir)
else
sub_dirs = (each_dir*"/").*read_sub_dirs(each_dir)
end
append!(links,each_dir.=>sub_dirs)
append!(next_dirs,sub_dirs)
end
deleteat!(previous_dirs,1:length(previous_dirs))
append!(previous_dirs,next_dirs)
else
break
end
end
return links
end
# generate list of links
function deepreaddir(working_dir_path::AbstractString,search_depth::Int=2)
links_raw = deepreaddir_raw(working_dir_path,2)
links = Pair[]
for each_p in links_raw
a = split(each_p[1],"/")[end]; b = split(each_p[2],"/")[end];
a == "" ? a = working_dir_path : nothing
push!(links,a=>b)
end
links
end
links = deepreaddir(".")
# construct relationships
tree_dic = Dict{String,Array}()
[tree_dic[x] = String[] for x in map(x->x[1],links)]
for p in links
push!(tree_dic[p[1]],p[2])
end
# "System" ==> 1
str2id_dic = Dict{String,Int64}()
[str2id_dic[links[i][2]] = i for i in 1:length(links)]
# loop through col2 of `links`, guess it's the output you want?
str_res = map(xx->haskey(tree_dic,xx) ? tree_dic[xx] : String[], map(x->x[2],links))
# to ids
res = [map(k->str2id_dic[k],x) for x in str_res]
julia> res = [map(k->str2id_dic[k],x) for x in str_res]
15-element Array{Array{T,1} where T,1}:
[4, 5, 6]
[7, 8, 9]
[10, 11, 12, 13, 14]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
[15]
Any[]
I’m biased here, but why not use MetaGraphs.jl instead of rolling your own graph structure? Also, for files with the same name, a combination of file system ID and inode should be globally unique at a given point in time.
I would create the Metagraph and populate it with output from stat: https://docs.julialang.org/en/v1/base/file/#Base.stat

Idiomatic `obj.value = f(obj)` in Ramda?

R.evolve lets us replace object properties with the result of a function applied to that property's current value:
R.evolve({ count: R.inc }, { count: 1 })
== { count: 2 }
But I frequently find I want to add a property calculated from multiple properties of input object:
assocFruitTotal({ appleCount: 5, orangeCount: 3 })
== { appleCount: 5, orangeCount: 3, fruitCount: 8 }
I came up with my own simple utility function:
const assocDerived = R.curry(
(name, f, obj) => ({
...obj,
[name]: f(obj)
});
... and I use it a lot:
const sumFruit = R.pipe(
R.props(['appleCount', 'orangeCount']),
R.sum);
const assocFruitTotal = assocDerived('fruitCount', sumFruit);
But the sheer frequency with which I use this makes me wonder why it's not
native to Ramda, as so many other convenient functions are. And that makes
me wonder whether I'm missing a better idiom that achieves the outcome -- that is, building up detail in an object by adding properties based upon combinations of other properties.
Is there an idiomatic functional programming construct I should be using instead?
Personally I would do it this way:
const fruitCount = applySpec({fruitCount: compose(sum, values)})
fruitCount({apple: 5, orange: 3})
//=> {"fruitCount": 8}
const withFruitCount = converge(mergeRight, [identity, fruitCount]);
withFruitCount({apple: 5, orange: 3});
//=> {"apple": 5, "fruitCount": 8, "orange": 3}
If there are non-count properties to exclude from the sum, you can use pickBy:
const pickCount = pickBy(flip(includes('Count')));
pickCount({appleCount: 5, orangeCount: 3, foo: 'bar'});
//=> {"appleCount": 5, "orangeCount": 3}
Let's start by recognizing that obj.value = f(obj) is a mutable assignment and therefore not a functional idiom to begin with. This is imperative-style thinking at work.
Storing a computed value as a property on your object is a misstep, in most cases. If either appleCount or orangeCount changes, there's nothing there to enforce the integrity of fruitCount.
fruitCount should be a function, not a property.
const fruitCount =
pipe
( props ([ 'appleCount', 'orangeCount' ])
, sum
)
fruitCount ({ appleCount: 1, orangeCount: 3 }) // 4
fruitCount ({ appleCount: 5, orangeCount: 3 }) // 8
If I had to guess, this is fake data and an example problem. In some scenarios, a computed value does make sense (memoisation is the first technique that comes to mind) but those cases make up the exception, not the rule. You say "the sheer frequency with which I use this ...", so I'd wager you do it in more areas than you should.
And as you pointed out, Ramda doesn't have a built-in for this, so this should further indicate that there are more conventional ways of solving this kind of problem.
An object-oriented programmer would assign this as a computed property -
const FruitData = function (apples = 0, oranges = 0)
{ this.apples = apples
this.oranges = oranges
}
Object.defineProperty
( FruitData.prototype
, 'fruitCount'
, { get () { return this.apples + this.oranges } }
)
const f =
new FruitData (3, 4)
console .log (f.fruitCount) // 7
When writing functional style, we leave OOP concepts at the door. Start thinking in terms of functions and your problems go away -
const FruitData = (apples = 0, oranges = 0) =>
({ apples, oranges })
const appleCount = fd =>
fd.apples
const orangeCount = fd =>
fd.oranges
const fruitCount = fd =>
appleCount (fd) + orangeCount (fd)
console .log (fruitCount (FruitData (10, 3))) // 13

ractivejs list sorting , ascending decending support?

Im trying to understand if there is a feature in ractivejs , for descending sorting , and ascending .
I couldnt find anyhting in the documentation .
No - Ractive purposely avoids being a 'kitchen sink' utility library. But it's very easy to add an ascending or descending helper:
var helpers = Ractive.defaults.data;
// assuming a and b are numbers...
helpers.ascending = function ( a, b ) {
return a - b;
};
helpers.descending = function ( a, b ) {
return b - a;
};
ractive = new Ractive({
el: 'body',
template: '' +
'<p>ascending: {{ numbers.slice().sort(ascending) }}</p>' +
'<p>descending: {{ numbers.slice().sort(descending) }}</p>'
},
data: {
numbers: [ 9, 4, 6, 2, 4, 1, 10, 2, 7, 8 ]
}
});
Note that you could also put the ascending and descending functions directly on the data object, if that's preferable.
Here's a JSFiddle to demonstrate: http://jsfiddle.net/rich_harris/nszt3150/

Resources