How to display line chart with keys associated with more than one set of values? angularjs-nvd3-directives - nvd3.js

I´m using angularjs-nvd3-directives for display a chart line, but I need to display datasets that have more than one set of values for the same key. For example:
"key": "estimation"
"values": actual: [[x1,a1],[x2,a2]...]
deviation: [[x1,d1],[x2,d2]...]
I have an example in plunker http://plnkr.co/edit/RM0iUx?p=preview but it does not render two lines at the same time.
Is this posible to achieve with nvd3-directives? Has anybody done something similar?
I need the two lines, sets of data values, to be associated with the same key. So when I click on legend corresponding to that key both lines should be affected

it's because you have two sets with the same name "set", just change the names to be different from each other and it will be solved.
$scope.exampleData = [
{
"key": "set1",
"values":[[0, 0], [1, 1], [2, 2], [3, 3]]
},
{
"key":"set2",
"values":[[1,0.5],[2,1.5],[2,2.5],[3,2.5]]
}
];

Related

how to create a set in terraform

I am trying to create a set to use as an argument in the setproduct function in terraform. When I try:
toset([a,b,c])
I get an error saying I can't convert a tuple to a list. I've tried various things like using tolist and ... and just having one pair of () braces in various places but I still can't get this to work - would anyone know how I can create a set from a,b and c?
set must have elements of the same type. Thus it can be:
# set of strings
toset(["a", "b", "c"])
# set of numbers
toset([1, 2, 3])
# set of lists
toset([["b"], ["c", 4], [3,3]])
You can't mix types, so the error you are getting is because your are mixing types, e.g. list and number
# will not work because different types
toset([["b"], ["c", 4], 3])

JSONata prevent array flattening

Q: How do I prevent JSONata from "auto-flattening" arrays in an array constructor?
Given JSON data:
{
"w" : true,
"x":["a", "b"],
"y":[1, 2, 3],
"z": 9
}
the JSONata query seems to select 4 values:
[$.w, $.x, $.y, $.z]
The nested arrays at $.x and $.y are getting flattened/inlined into my outer wrapper, resulting in more than 4 values:
[ true, "a", "b", 1, 2, 3, 9 ]
The results I would like to achieve are
[ true, ["a", "b"], [1, 2, 3], 9 ]
I can achieve this by using
[$.w, [$.x], [$.y], $.z]
But this requires me to know a priori that $.x and $.y are arrays.
I would like to select 4 values and have the resulting array contain exactly 4 values, independent of the types of values that are selected.
There are clearly some things about the interactions between JSONata sequences and arrays that I can't get my head around.
In common with XPath/XQuery sequences, it will flatten the results of a path expression into the output array. It is possible to avoid this in your example by using the $each higher-order function to iterate over an object's key/value pairs. The following expression will get what you want without any flattening of results:
$each($, function($v) {
$v
})
This just returns the value for each property in the object.
UPDATE: Extending this answer for your updated question:
I think this is related to a previous github question on how to combine several independent queries into the same question. This uses an object to hold all the queries in a similar manner to the one you arrived at. Perhaps a slightly clearer expression would be this:
{
"1": t,
"2": u.i,
"3": u.j,
"4": u.k,
"5": u.l,
"6": v
} ~> $each(λ($v){$v})
The λ is just a shorthand for function, if you can find it on your keyboard (F12 in the JSONata Exerciser).
I am struggling to rephrase my question in such as way as to describe the difficulties I am having with JSONata's sequence-like treatment of arrays.
I need to run several queries to extract several values from the same JSON tree. I would like to construct one JSONata query expression which extracts n data items (or runs n subqueries) and returns exactly n values in an ordered array.
This example seems to query request 6 values, but because of array flattening the result array does not have 6 values.
This example explicitly wraps each query in an array constructor so that the result has 6 values. However, the values which are not arrays are wrapped in an extraneous & undesirable array. In addition one cannot determine what the original type was ...
This example shows the result that I am trying to accomplish ... I asked for 6 things and I got 6 values back. However, I must know the datatypes of the values I am fetching and explicitly wrap the arrays in an array constructor to work-around the sequence flattening.
This example shows what I want. I queried 6 things and got back 6 answers without knowing the datatypes. But I have to introduce an object as a temporary container in order to work around the array flattening behavior.
I have not found any predicates that allow me to test the type of a value in a query ... which might have let me use the ?: operator to dynamically decide whether or not to wrap arrays in an array constructor. e.g. $isArray($.foo) ? [$.foo] : $.foo
Q: Is there an easier way for me to (effectively) submit 6 "path" queries and get back 6 values in an ordered array without knowing the data types of the values I am querying?
Building on the example from Acoleman, here is a way to pass in n "query" strings (that represent paths):
(['t', 'u.i', 'u.j', 'u.k', 'u.l', 'v'] {
$: $eval('$$.' & $)
}).$each(function($o) {$o})
and get back an array ofn results with their original data format:
[
12345,
[
"i",
"ii",
"iii"
],
[],
"K",
{
"L": "LL"
},
null
]
It seems that using $each is the only way to avoid any flattening...
Granted, probably not the most efficient of expressions, since each has to be evaluated from a path string starting at the root of the data structure -- but there ya go.

Sort ruby middleman array into 3 columns, content in placed in order to display LTR

I am using middleman and have a specific structure that I want my articles to fall into:
.row>
.col-1>article 1, article 4...
.col-2>article 2, article 5...
.col-3>article 3, article 6...
The goal is that the articles read left-to-right, however are stacked in their columns so there is now additional row class that is needed. I have these articles enclosed in a visual card, and want to stack them which is why I have this strange problem to solve.
My question is what is the cleanest ruby way to sort the array into this format? The goal is to use it in a structure like this and be able to account for any number of columns, n.
.row
- page_articles.someProcess(n).each_split(n) do | col |
.column-class
- col.each do | art |
...
Verbatim from group_by doc:
(1..6).group_by{ |i| i%3 }
#=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
specifically, use the flatten values
page_articles.group_by.with_index{ |a,i| i%3 }.values.flatten(1)

Is there any other way to remove an element in a list besides knowing its actual value?

Value edits at the bottom.
I would prefer a more robust solution. The following is a very hacky approach.
Suppose the following:
Redis.rpush("list_of_objs", ['{"value": 1, "id: 3}', '{"value": 2, "id": 9}',
'{"value": 8, "id": 1}', '{"value": 9, "id": 99}', '{"value": 10, "id": 1252}',
'{"value": 0, "id": 99999}']).
I now have a list of simple json objects that only have value and id attributes.
If you're curious,
Redis.lrange("list_of_objs", 0, -1)
#=> ['{"value": 1, "id: 3}', '{"value": 2, "id": 9}', '{"value": 8, "id": 1}', '{"value": 9, "id": 99}', '{"value": 10, "id": 1252}', '{"value": 0, "id": 99999}']
Awesome. Now, let us assume we want to delete '{"value": 8, "id": 1}'. That is achieved through the following command:
Redis.lrem("list_of_objs", 1, '{"value": 8, "id": 1}')
If you call Redis.lrange("list_of_objs", 0, -1) once more, you'll now see that we successfully deleted the element.
This works in some cases, but truth be told, in many cases, this isn't sufficient. I'm not guaranteed that what I want to delete will be exactly the same since when I put it into the list. Someone may choose to change '{"value": 8, "id": 1}' to '{"value": 22, "id": 1}' before I decide to delete it. However, if I try to delete it now, I'm screwed. The same command won't work. Now, if we used some Ruby logic, we could easily get this sorted, but the solution becomes rather hacky if you ask me.
Now, the assumption with this type of solution is that some element within your object, in our case the id, stays the same throughout the lifetime of the object.
Now, the solution:
Redis.multi do
array = Redis.lrange("list_of_objs", 0, -1)
index_of_element_to_be_destroyed = array.index { |el| JSON.parse(el)["id"] == 1 }
element = Redis.lindex("list_of_objs", index_of_element_to_be_destroyed)
Redis.lrem("list_of_objs", 1, element)
end
That seems a bit hacky to have to delete 1 element. I'm pretty sure by doing it in multi, I'm safe from the index potentially changing via something concurrent.
Edit 1: Actually, I'm pretty sure the above might not even work. I'm not sure if I am allowed to do such ruby like array.index in a Redis.multi block.
Edit 2: Perhaps I do the deletion in Ruby, then just re-rpush the array? I would like to avoid that.
Edit 3: This question seems to hold valuable information, but if I'm seriously expected to use a loop and watch, then I think this is much more hacky than I anticipated.
That being said, is there a better way to delete an element in a list based on some condition?

How to parse pdf in Ruby

I have been trying a simple Ruby program to parse a simple pdf file and extract the texts I am interested in. I found that pdf-reader is quite good gem for pdf file parsing. I have read through the examples given in that gem and some tutorials around that.
I have tried the callback method and was able to get all the text from my pdf file. But I did not understand the concept behind the arguments for some of the callbacks.
For example, If my pdf has a simple table with 3 columns and 2 rows. (Header row values are Name, Address, Age) and first row values are (Arun, Hoskote, 22) and when U run the a ruby following ruby script
receiver = PDF::Reader::RegisterReceiver.new
reader = PDF::Reader.new("Arun.pdf")
reader.pages.each do |page|
page.walk(receiver)
receiver.callbacks.each do |cb|
puts cb.inspect
end
end
It prints series of callbacks among which some of the interesting callbacks show_text_with_positioning were like following
{:name=>:show_text_with_positioning, :args=>[["N", 5, "am", -4, "e"]]}
{:name=>:show_text_with_positioning, :args=>[[" "]]}
{:name=>:show_text_with_positioning, :args=>[["Ad", 6, "d", 3, "ress"]]}
{:name=>:show_text_with_positioning, :args=>[[" "]]}
{:name=>:show_text_with_positioning, :args=>[["Age"]]}
{:name=>:show_text_with_positioning, :args=>[[" "]]}
{:name=>:show_text_with_positioning, :args=>[["Ar", 4, "u", 3, "n"]]}
{:name=>:show_text_with_positioning, :args=>[[" "]]}
{:name=>:show_text_with_positioning, :args=>[["H", 3, "o", -5, "sk", 9, "o", -5, "te"]]}
{:name=>:show_text_with_positioning, :args=>[[" "]]}
{:name=>:show_text_with_positioning, :args=>[["22"]]}
{:name=>:show_text_with_positioning, :args=>[[" "]]}
From the above callbacks, what does args represent with respect to pdf file ? If I want to extract only name value that is 'Arun' (Anything can come here) here or age value i,e '25' (any value can come here) here in this example, how can I do that in ruby program ? Is there any pdf-parser API or Ruby API to get only a single "interested" value(s) from a pdf file ?
How can I write a Ruby program to access a particular callback which I am interested in which gives me the text I wanted ?
If you particularly only want the text, you can do something like this (but probably using a different stream as the destination for the text):
receiver = PDF::Reader::TextReceiver.new($stdout)
PDF::Reader.file("Arun.pdf", receiver)
Once you have the text, you could use regular expressions or whatever to get the specific value you want out of it.

Resources