Highcharts plot weekly data (JS) - highcharts

I'm trying to plot the following object but it gives me a chart with the following x axis : Thursday, Thursday, Friday, Saturday, Saturday, Sunday, Sunday, Monday, Tuesday
var myData = [
{date: "2018-08-02 00:00:00", value : "626.12"},
{date: "2018-08-02 00:50:00", value : "626.95"},
{date: "2018-08-03 00:40:00", value : "646.23"},
{date: "2018-08-04 00:60:00", value : "656.5O"},
{date: "2018-08-04 00:50:00", value : "656.65"},
{date: "2018-08-05 00:30:00", value : "666.32"},
{date: "2018-08-05 00:30:00", value : "666.63"},
{date: "2018-08-06 00:30:00", value : "686.92"},
{date: "2018-08-07 00:30:00", value : "696.21"},
{date: "2018-08-07 00:40:00", value : "696.54"}
];
I tried to filter my object with ES6 array method filter() but I didn't manage to make something like :
var weekly = [
{date: "2018-08-02 00:00:00", value : "626.12"},
{date: "2018-08-03 00:40:00", value : "646.23"},
{date: "2018-08-04 00:60:00", value : "656.50"},
{date: "2018-08-05 00:30:00", value : "666.32"},
{date: "2018-08-06 00:30:00", value : "686.92"},
{date: "2018-08-07 00:30:00", value : "696.21"}
];
Does anyone have an idea ?
Thanks for advance.

var myData = [
{date: "2018-08-02 00:00:00", value : "626.12"},
{date: "2018-08-02 00:50:00", value : "626.95"},
{date: "2018-08-03 00:40:00", value : "646.23"},
{date: "2018-08-04 00:60:00", value : "656.5O"},
{date: "2018-08-04 00:50:00", value : "656.65"},
{date: "2018-08-05 00:30:00", value : "666.32"},
{date: "2018-08-05 00:30:00", value : "666.63"},
{date: "2018-08-06 00:30:00", value : "686.92"},
{date: "2018-08-07 00:30:00", value : "696.21"},
{date: "2018-08-07 00:40:00", value : "696.54"}
];
myData = myData.reverse().filter((obj, index) => {
if(index !== myData.length-1) {
return obj.date.split(' ')[0] !== myData[index+1].date.split(' ')[0]
}
return true;
}).reverse();
console.log(myData)

Related

Failed to create tombstone message with key value in ksqldb 0.27.2

I try to create tombstone message by using below steps in ksqldb 0.27.2 , but the tombstone message missing key value. The reproduce steps are as follows.
Origin topic message:
key: f8b6c12812ef4edc9c2c10ab2f7adceb, value: {"op_type": "I", "ID": "f8b6c12812ef4edc9c2c10ab2f7adceb", "CLASS_ID": "89b65aec513e4610908073dfde5baf46"}
key: f8b6c12812ef4edc9c2c10ab2f7adceb, value: {"op_type": "D", "ID": "f8b6c12812ef4edc9c2c10ab2f7adceb", "CLASS_ID": "89b65aec513e4610908073dfde5baf46"}
Step 1: create source stream
set 'auto.offset.reset'='earliest';
CREATE STREAM STREAM_ORIGIN_OBJ_ATTRIBUTE WITH (KAFKA_TOPIC='SIT.CPLM.OBJ_ATTRIBUTE',VALUE_FORMAT='AVRO');
Step 2: create tombstone stream
CREATE STREAM STREAM_ETL_TOMBSTONE_OBJ_ATTRIBUTE WITH (KAFKA_TOPIC='SIT.CPLM.OBJ_ATTRIBUTE', VALUE_FORMAT='KAFKA') AS SELECT CAST(NULL AS VARCHAR) FROM STREAM_ORIGIN_OBJ_ATTRIBUTE WHERE op_type = 'D';
The output message of tombstone stream:
rowtime: 2022/08/02 06:56:08.867 Z, key: f8b6c12812ef4edc9c2c10ab2f7adceb, value: {"op_type": "I", "ID": "f8b6c12812ef4edc9c2c10ab2f7adceb", "CLASS_ID": "89b65aec513e4610908073dfde5baf46"}, partition: 0
rowtime: 2022/08/02 06:56:19.877 Z, key: f8b6c12812ef4edc9c2c10ab2f7adceb, value: {"op_type": "D", "ID": "f8b6c12812ef4edc9c2c10ab2f7adceb", "CLASS_ID": "89b65aec513e4610908073dfde5baf46"}, partition: 0
rowtime: 2022/08/02 06:56:19.877 Z, key: <null>, value: <null>, partition: 0
Expected tombstone message:
rowtime: 2022/08/02 06:56:19.877 Z, key: f8b6c12812ef4edc9c2c10ab2f7adceb, value: <null>, partition: 0

Combine two arrays in Ruby?

Suppose we have two arrays:
foo = [1, 2] # can be mapped in [{id: 1}, {id: 2}]
bar = [{id: 2}, {id: 4}]
As a result I need to get following array:
res = [ [1, nil], [2, {id: 2}], [nil, {id: 4}] ]
# Is also ok [ [{id: 1}, nil], [{id: 2}, {id: 2}], [nil, {id: 4}] ]
I there any standard Ruby method to get such combinations? At first I'm looking for Ruby way, elegant, short solution.
In other words, I need to get an array diff map, which shows absent and newly added elements.
The elements:
elements = (foo + bar.map{ |h| h.values.first }).uniq
#=> [1, 2, 4]
and the combinations:
elements.map { |i| [foo.include?(i) ? i : nil, bar.include?({id: i}) ? {id: i} : nil] }
#=> [[1, nil], [2, {:id=>2}], [nil, {:id=>4}]]
Or as Sebastian suggested, you can also use #find instead of #includes?:
elements.map { |i| [foo.find { |e| e == i }, bar.find { |e| e == { id: i } }] }
I would transform the two arrays to lookup hashes. Then collect all the ids to iterate. Map the ids into the foo and bar values, then zip them together.
foo_lookup = foo.to_h { |id| [id, {id: id}] } # change `{id: id}` into `id` for the other output
bar_lookup = bar.to_h { |item| [item[:id], item] }
ids = foo_lookup.keys | bar_lookup.keys
res = ids.map(&foo_lookup).zip(ids.map(&bar_lookup))
#=> [[{:id=>1}, nil], [{:id=>2}, {:id=>2}], [nil, {:id=>4}]]
Just another option.
foo = [1, 2]
bar = [{id: 2}, {id: 4}]
(foo + bar).group_by { |e| e.is_a?(Hash) ? e[:id] : e }.values.map { |e| e.size == 1 ? e << nil : e }
#=> [[1, nil], [2, {:id=>2}], [{:id=>4}, nil]]
The first part returns
(foo + bar).group_by { |e| e.is_a?(Hash) ? e[:id] : e }.values
#=> [[1], [2, {:id=>2}], [{:id=>4}]]
If you need to sort the sub arrays, just map using Array#unshift or Array#push (prepend or append nil) depending in the element already there.

Grouping Elements in Dictionary by The Last Character of The Keys [iOS Swift 5]

I have a dictionary that I want to group by the last character of the keys. This is the dictionary:
var displayValues = ["volume_1": 1, "price_2": 6, "price_1": 2, "stock_1": 3, "volume_2": 5, "stock_2": 7]
This is the code that I used in order to group them
let groupValues = Dictionary(grouping: displayValues) { $0.key.last! }
print(groupValues)
This is the result of this code
["2": [(key: "price_2", value: 6), (key: "volume_2", value: 5), (key: "stock_2", value: 7)], "1": [(key: "volume_1", value: 1), (key: "price_1", value: 2), (key: "stock_1", value: 3)]]
The grouping is correct, however, how do I remove the words key and value from the dictionary so that it will display the following?
[
"2": ["price_2": 6, "volume_2" : 5, "stock_2": 7],
"1": ["volume_1": 1, "price_1": 2, "stock_1": 3]
]
You are almost there !!
now You have key as you wanted and value as array of tuple
You can convert array of tuple into dictionary with new reduce(into:)
full code would be
var displayValues = ["volume_1": 1, "price_2": 6, "price_1": 2, "stock_1": 3, "volume_2": 5, "stock_2": 7];
let dict = Dictionary(grouping: displayValues) { $0.key.suffix(1)}
let final = dict. mapValues { value in
return value.reduce(into: [:]) { $0[$1.key] = $1.value }
}
print(final)
Output :
["2": ["price_2": 6, "volume_2": 5, "stock_2": 7], "1": ["price_1": 2, "stock_1": 3, "volume_1": 1]]
In this case, Dictionary(grouping:by:) creates a Dictionary of type [Character : [(key: String, value: Int)]]. So the values are an array of (key: String, value: Int) tuples.
Use .mapValues() to convert the Array of (key: String, value: Int) tuples into a Dictionary by calling Dictionary(uniqueKeysWithValues) with the array:
var displayValues = ["volume_1": 1, "price_2": 6, "price_1": 2, "stock_1": 3, "volume_2": 5, "stock_2": 7]
let groupValues = Dictionary(grouping: displayValues) { String($0.key.suffix(1)) }
.mapValues { Dictionary(uniqueKeysWithValues: $0) }
print(groupValues)
Result:
["1": ["stock_1": 3, "price_1": 2, "volume_1": 1], "2": ["volume_2": 5, "stock_2": 7, "price_2": 6]]
Note:
To avoid a force unwrap (which will crash if you have an empty String as a key), I used String($0.key.suffix(1)) instead of $0.key.last!. This will make the final dictionary [String : [String : Int]] which can be conveniently indexed with a String.
Thanks to #LeoDabus for this suggestion.

iOS swift 4 sort dictionary by a particular values

Can you please help me sorting the dictionary below with value "Val1".
var data = ["key1" : ["val1": 1,"val2": 1],
"key2" : ["val1": 5,"val2": 2],
"key3" : ["val1": 0,"val2": 9]]
I am expecting this dictionary to be sorted like this. Highest values of 'Val1' to be on top (descending order)..
var data = [ "key2" : ["val1": 5,"val2": 2],
"key1" : ["val1": 1,"val2": 1],
"key3" : ["val1": 0,"val2": 9]]
As #Sulthan correctly pointed out Dictionary cannot be sorted. If you still want to, you'll get an Array as result.
let data = [
"key1": ["val1": 1,"val2": 1],
"key2": ["val1": 5,"val2": 2],
"key3": ["val1": 0,"val2": 9]
]
let result = data.sorted { $0.1["val1"]! > $1.1["val1"]! }
print(result)
[(key: "key2", value: ["val1": 5, "val2": 2]),
(key: "key1", value: ["val1": 1, "val2": 1]),
(key: "key3", value: ["val1": 0, "val2": 9])]

Simplest way to sum 2D array elements based on duplicates?

So say I have a 2D array. Maybe something like:
data = [['oct 1', 4], ['oct 2', 5], ['oct 3', 9], ['oct 1', 2]]
And I want to get a new array out of this, that removes the duplicate values (eg. oct 1), but sums the corresponding values.
So I would end up with:
data = (['oct 1', 6], ['oct 2', 5], ['oct 3', 9])
I can think of a few ways to do this but they seem pretty inefficent and usually there's always some crazy ruby code that can do anything in a few lines, any suggestions?
Here are a couple of ways of doing that, my preference being the first.
Use a counting hash
Code
def combine(data)
data.each_with_object(Hash.new(0)) { |(date, val), h| h[date] += val }.to_a
end
See Hash::new, the discussion of the default value in particular.
Example
data = [['oct 1', 4], ['oct 2', 5], ['oct 3', 9], ['oct 1', 2]]
combine data
#=> [["oct 1", 6], ["oct 2", 5], ["oct 3", 9]]
Explanation
enum = data.each_with_object(Hash.new(0))
#=> #<Enumerator: [["oct 1", 4], ["oct 2", 5], ["oct 3", 9],
# ["oct 1", 2]]:each_with_object({})>
We can see the values generated by this enumerator by converting it to an array.
enum.to_a
#=> [[["oct 1", 4], {}], [["oct 2", 5], {}], [["oct 3", 9], {}],
# [["oct 1", 2], {}]]
The first value of enum is passed to the block and the values of the block variables are computed, using disambiguation and parallel assignment.
(date, val), h = enum.next
#=> [["oct 1", 4], {}]
date
#=> "oct 1"
val
#=> 4
h[date] += val
#=> h[date] = h[date] + val
#=> h["oct 1"] = h["oct 1"] + 4
#=> h["oct 1"] = 0 + 4 (no key "oct 1" so default value of `0` used)
#=> h["oct 1"] = 4
Now,
h #=> {"oct 1"=>4}
The remaining three values of enum are passed to the block and the block calculations are performed.
(date, val), h = enum.next
#=> [["oct 2", 5], {"oct 1"=>4}]
h[date] += val
#=> 5 (the default value of `0` is again used)
h #=> {"oct 1"=>4, "oct 2"=>5}
(date, val), h = enum.next
#=> [["oct 3", 9], {"oct 1"=>4, "oct 2"=>5}]
h[date] += val
#=> 9 (the default value of `0` is again used)
h #=> {"oct 1"=>4, "oct 2"=>5, "oct 3"=>9}
(date, val), h = enum.next
#=> [["oct 1", 2], {"oct 1"=>4, "oct 2"=>5, "oct 3"=>9}]
h[date] += val
#=> 6
h #=> {"oct 1"=>6, "oct 2"=>5, "oct 3"=>9}
In the last calculation, the default value was not used because the hash h already had a key "oct 1":
h[date] += val
#=> h[date] = h[date] + val
#=> h["oct 1"] = h["oct 1"] + 2
#=> h["oct 1"] = 4 + 2
Lastly,
h.to_a
#=> [["oct 1", 6], ["oct 2", 5], ["oct 3", 9]]
Use Enumerable#group_by
Code
def combine(data)
data.group_by(&:first).map { |date, vals| [date, vals.map(&:last).reduce(:+)] }
end
Example
combine data
#=> [["oct 1", 6], ["oct 2", 5], ["oct 3", 9]]
Explanation
The steps:
h = data.group_by(&:first)
#=> {"oct 1"=>[["oct 1", 4], ["oct 1", 2]],
# "oct 2"=>[["oct 2", 5]], "oct 3"=>[["oct 3", 9]]}
The first key-value pair of h is passed to the block:
date, vals = h.first
#=> ["oct 1", [["oct 1", 4], ["oct 1", 2]]]
date
#=> "oct 1"
vals
#=> [["oct 1", 4], ["oct 1", 2]]
and the block calculation is performed.
a = vals.map(&:last)
#=> [4, 2]
t = a.reduce(:+)
#=> 6
So the first key-value pair of h is mapped to
[date, t]
#=> ["oct 1", 6]
The remaining calculations are similar.
Try following with inject method:
data = [["oct 1", 4], ["oct 2", 5], ["oct 3", 9], ["oct 1", 2]]
data.inject({}) { |sum, (n, t)| sum[n] ||= 0; sum[n] += t; sum }.to_a
=> [["oct 1", 6], ["oct 2", 5], ["oct 3", 9]]

Resources