How to print chart with multiple values between dates? - javascript

I'm trying to display this kind of list on a chart:
[ [ Moment<2020-08-02T02:54:24+02:00>, 9.99 ],
[ Moment<2020-08-05T21:55:33+02:00>, 10.99 ],
[ Moment<2020-08-05T22:00:50+02:00>, 26.87 ],
[ Moment<2020-08-06T23:43:32+02:00>, 10.99 ],
[ Moment<2020-08-07T22:57:13+02:00>, 26.87 ],
[ Moment<2020-08-07T23:01:20+02:00>, 9.99 ],
[ Moment<2020-08-08T22:10:14+02:00>, 10.99 ],
[ Moment<2020-08-08T22:12:20+02:00>, 35.83 ],
[ Moment<2020-08-10T19:03:51+02:00>, 20.24 ],
[ Moment<2020-08-11T17:24:39+02:00>, 10.99 ],
[ Moment<2020-08-11T17:27:41+02:00>, 27.03 ],
[ Moment<2020-08-13T14:04:34+02:00>, 29.54 ],
[ Moment<2020-08-17T04:17:37+02:00>, 9.99 ],
[ Moment<2020-08-25T04:57:57+02:00>, 10.99 ] ]
What I try to get is to have the x-axis separated by one day each square but keeping the fact that there can be multiple values for one day (so every point is separated by a different distance). I don't really know how to approach it, if you can give me some directions on how to start it, it would be much appreciated. Many thanks.

You need to create a range for the x-axis, listing all the dates.
For that you can use: https://www.npmjs.com/package/moment-range
Then loop through that range, for each day get the values from your list (by filtering for example).
It then depends on your chart library and business logic on how you'd combine multiple values per day (for example adding them) or display them individually as stacked column chart.

Related

render hashmap data in react

I'm retrieving hashmap data from an api into my react app. the data looks like this..
[
[
"d243dc7-6fe8-151b-4ca8-1be528f2b36",
"[\"Jonny\",70]"
],
[
"8affa17-76d1-13e-6380-7cd2a3e3647",
"[\"Lucy\",106,"pic3.jpg"]"
],
[
"841cb28-24c7-872-3c66-63253800c8d",
"[\"Mike\",0]"
],
[
"6128e-182-cfb4-708b-c40a3ba2e6e",
"[\"Elodie\",23,"me.jpg"]"
],
[
"e55ef4c-8d41-3be4-27d-aae53330584",
"[\"Jacob\",9,"img-004.jpeg"]"
]
]
I need to render this data into a table.
Using map and with with data.slice(0, 1) I've managed to pull out the key (the long string), but I need help separating the name, number and optional image. I tried experimenting with various operators. I thought this would work
{data.slice(1, 2).toString().substring(2,data.slice(1, 2).length-2)}
but it just returns ["
I'm hoping there is a simpler way to do it!
You presented the data in a strange format, but this works:
const data = [
[
"d243dc7-6fe8-151b-4ca8-1be528f2b36",
"[\"Jonny\",70]"
],
[
"8affa17-76d1-13e-6380-7cd2a3e3647",
"[\"Lucy\",106,\"pic3.jpg\"]"
],
[
"841cb28-24c7-872-3c66-63253800c8d",
"[\"Mike\",0]"
],
[
"6128e-182-cfb4-708b-c40a3ba2e6e",
"[\"Elodie\",23,\"me.jpg\"]"
],
[
"e55ef4c-8d41-3be4-27d-aae53330584",
"[\"Jacob\",9,\"img-004.jpeg\"]"
]
];
const data1 = JSON.parse(data.slice(1, 2)[0][1]);
console.log('name', data1[0]);
console.log('numb', data1[1]);
console.log('file', data1[2]);
{data.slice(1, 2).toString().substring(2,data.slice(1, 2).length-2)}
^ ^^^^^^^^^^^^^^^^
In the string "["Jonny",70]" , you only want to skip the first character "[", so your first parameter of substring() should be index 1
you forgot to transfer data.slice(1, 2) to string, so it's still an array and its length would be 1
so your code should be revised to:
data.slice(1, 2).toString().substring(1, data.slice(1, 2).toString().length - 2)

Matches/indices returns entire string

Using http://fusejs.io/
In just about all situations, I'm finding that the 'indices returned contain pretty much the entire string.
For a very simple example, if you go to fusejs.io, and change the first object (in 'step 1' on the left) to
{
title: "so ummmmmm Old Man's War is a book about a thing",
author: {
firstName: "John",
lastName: "Scalzi"
}
},
then tick 'Include matches' (in 'step 2' in the middle)
the resulting indices matches most of the strong, including parts that are obviously way off:
"matches": [
{
"indices": [
[
0,
0
],
[
2,
2
],
[
4,
10
],
[
14,
23
]
],
"value": "so ummmmmm Old Man's War is a book about a thing",
"key": "title",
"arrayIndex": 0
}
]
whereas I'm expecting it to just return the matched part of the string, like so:
"matches": [
{
"indices": [
[
14,
23
]
],
"value": "Man's War",
"key": "title",
"arrayIndex": 0
}
]
I've tried it in my own app and seeing similar results across a large dataset. The matching itself is quite accurate - it's just the highlighting via matches[i].indices that's wrong. It seems to always match up to the matched string, and also sometimes far beyond. Am I going bonkers? Or is this just not possible with fuse.js?
Thanks!
P.S. Can someone please let me know if I'm supposed to include or not include the 'javascript' tag on a library question like this?

Pie chart series color are black on NodeRed

when I try to draw pie chart on nodered , colors are always black. I m using dashboard-ui node. What is the missing ? or What is best way draw pie chart on nodered ui ?
msg.payload=[{
"series": [ "X", "Y"],
"colors":['#8b4513','#26138b'],
"data": [ 77,23],
"labels": [ "Jan","mon" ]
}];
There are two issues with this payload, which are stopping it from doing what you would expect:
the colors option is not currently supported as an input, and
the data must be an array of arrays, with the number of inner arrays being the same as the number of series to be shown (so in your case, an array of 1 array of y-values)
This revised payload works for me:
[
{
"series": [ "X", "Y" ],
"data": [
[ 77, 23 ]
],
"labels": [ "Jan", "mon" ]
}
]
The option to specify colors for each series would be a nice feature to add to the ui_chart node. For now, you will have to select the colors you want inside the node configuration editor. Please see the chart node's README for other examples of valid data inputs.
--
Steve

How to Get Last Array

Using cheerio, I have managed to scrape a PHP generated table that contains a column of dates, locations, etc. As the number of rows is variable, I opted to use .map() to iterate through each row, setting the matching starting event dates (startDate) with the provided CSS selectors. The above process seems to be functioning well, as when I call console.log(startDate) I recieve the output below. it would appear, however, that the process creates an array for each time it moves to the next row, appending an additional date each time. How can I set a variable to only the last array in the array startDate?
[ '03/18/2014' ]
[ '03/18/2014', '03/01/2014' ]
[ '03/18/2014', '03/01/2014', '02/15/2014' ]
[ '03/18/2014', '03/01/2014', '02/15/2014', '01/31/2014' ]
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014' ]
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014',
'12/06/2013' ]
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014',
'12/06/2013',
'11/16/2013' ]
So the desired output of console.log(newArray) would be:
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014',
'12/06/2013',
'11/16/2013' ]
If startDate is an array, you should be able to get the last item in the array by using the index, like this:
var lastStartDate = startDate[startDate.length-1]; //now lastStartDate contains the last item in the array

Google Fusion , Map Overlays and breaking out polyline via a javascript array

My question is based on the following example from Google. I have gotten it to work great to pull out my polygon shapes from my fusion table, similar to their shapes. My issue arises when I try and alter the script to pull out some polyline data I have. The issue is in the breaking down of the rows data array. The dataset for the correct polygons is as follows.
"rows": [
[
"129",
{
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-86.3055326754,
39.8143343479,
0.0
],
[
-86.3056957826,
39.8139995783,
0.0
],
[
-86.3059116214,
39.8137158473,
0.0
],
[
-86.3055326754,
39.8143343479,
0.0
]
]
]
}
}
],etc....
The polylines is as follows
"rows": [
[
"1",
{
"geometry": {
"type": "LineString",
"coordinates": [
[
-86.2411593096,
40.1368952707,
0.0
],
[
-86.2413739094,
40.1367194041,
0.0
],
[
-86.241866376,
40.1197924711,
0.0
]
]
}
}
],etc..
The polygon has an extra [] that is throwing off my ability to parse through the data! It is driving me nuts! Serious points to answer this one! Example of mine here. I just dont know enough about javascript arrays. Thanks
Here is a version that only parses polylines:
http://www.geocodezip.com/v3_GoogleEx_FusionTables_mouseover_map_styles_polylines.html
I couldn't find any documentation or description of the expected output (other than "GeoJSON"), it would be nice if there was a way to detect polygons vs. polylines vs. markers and parse them appropriately.
Here is a description of GeoJSON
Here is a page that parse polylines, polygons and markers (not well tested).

Categories