how to pass date into google charts - google-visualization

I have a row like this,
[[[Date {Wed Dec 18 2013 00:00:00 GMT+0530 (India Standard Time)}, 0, null, 19 more...],
[Date {Tue Dec 17 2013 00:00:00 GMT+0530 (India Standard Time)}, 0, null, 19 more...],
[Date {Mon Dec 16 2013 00:00:00 GMT+0530 (India Standard Time)}, 0, null, 19 more...],
...4 more...]]
Earlier the date in the above line was like this,
['2013-11-18', 0, null, null, 0, null, null, 0, null, null, 0, null, null, 0, null, null, 0, null, null, 0, null, null]
and so on... So, I just converted into above format. But, how to pass it into google charts? Since now,I have used:
googledata.addRows(chartdata)
Where the chartdata contains above lines and I have also tried:
var googledata = google.visualization.arrayToDataTable(chartdata);
I have rows like this,
googledata.addColumn('date', 'Date');
forloop
{
googledata.addColumn('number', items[i]);
googledata.addColumn({type:'string', role:'annotation'});
googledata.addColumn({type:'string', role:'annotationText'});
}
Total 7 items are there and am generating that using for loop.
help me

The arrayToDataTable method does not support inputting dates, so you can't use that. Your dates need to be input in one of two ways, depending on how you are loading the data. If you are manually building the DataTable, like this:
var googledata = new google.visualization.DataTable();
googledata.addColumn('date', 'Date');
// add other columns
then you need to input your dates as Date objects. As an example, this date Date {Wed Dec 18 2013 00:00:00 GMT+0530 (India Standard Time)} would be input as new Date(2013, 11, 18) (months are zero-indexed, so December is 11 not 12). If you are constructing your DataTable from a JSON string, like this:
var googledata = new google.visualization.DataTable(jsonData);
then you need to use a custom string format for the dates. It looks similar to a standard Date object, but is a string and does not contain the new keyword; your example date would be input as "Date(2013, 11, 18)".
[Edit - how to use a DataView to convert date strings into Date objects]
You can also use a DataView to convert date strings into Date objects. You need to parse the date string into its component parts and use those to construct a Date object. Assuming a date string in the format 'yyyy-MM-dd' in column 0 of your DataTable, this is how you would convert it to a Date object:
var view = new google.visualization.DataView(googledata);
view.setColumns([{
type: 'date',
label: data.getColumnLabel(0),
calc: function (dt, row) {
var dateArray = dt.getValue(row, 0).split('-');
var year = dateArray[0];
var month = dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months
var day = dateArray[2]; //The day is the third split
return new Date(year, month, day);
}
}, /* list of the rest of your column indices */]);

Related

Trouble using utcoffset with Chart.js

I'm trying to use Chart.js with a datetime x axis, and I need to adjust all my values by subtracting 5 hours. Here's some of my code:
var timeFormat = 'MM/DD HH:mm';
time: {
format: timeFormat,
tooltipFormat: 'll',
parser: function(utcMoment) {
return moment(utcMoment).utcOffset(5, true);
}
},
Without the parser function, my values are normal (10:00, January 10, 2021), but with the parser function, for some reason my values are set back all the way to 2001. Yes two-thousand-and-one.(10:00, January 10, 2001) Note that the time is not actually changed (So two errors: 1.time not adjusted when it should be. 2:years adjusted when it shouldn't be). Why could this be?
I will assume that the reason you want to roll it back by 5 hours is because of a timezone difference. If that's the case, you should use moment-timezone instead of moment.
With that said, subtracting 5 hours from the current date is actually simpler than what you're doing.
Before feeding a date into moment, you need to convert it to the js Date object like so: new Date('2021-01-10 00:00:00'). Since your parser function accepts the date in m/d H:M format, you would need to append the year to it first.
So here is how your code should look:
parser: function(utcMoment) {
const new_date = utcMoment.split(' ')[0] + '/' + (new Date().getFullYear()) + ' ' + utcMoment.split(' ')[1];
return moment(new Date(new_date)).subtract({hours: 5})
}

Power Query convert of 3 byte binary date getting variable wasn't recognized

Date displays as 3byte hex value example = 72, 02, 11
Converts to 72 to decimal = 114 + 1900 = 2014, 02 = 02, 11 = 17
So date is 02/17/2014
Bdate comes in as a three byte field, binary so defined
1st byte = year, 2nd byte = mth, 3rd byte = day.
Building a function to call because several dates in data base are of this format.
Secondary function to convert hex to decimal is fnhex2Dec from a library.
Here is code for FixDateBn
let
FixDateBn = (Bdate as binary) =>
let
FixDateBn = BinaryFormat.ByteOrder(
BinaryFormat.Record ([
Yr= BinaryFormat.Byte, //<- the Yr variable is getting Name Yr wasn't recognized.
Mth = BinaryFormat.Byte,
Day = BinaryFormat.Byte ]),
ByteOrder.LittleEndian),
Yrnum = fnhex2Dec(Yr,16)+1900,
Mthnum = fnhex2Dec(Mth,16),
Daynum = fnhex2Dec(Day,16),
Gooddate = #date(Yrnum,Mthnum,Daynum) as date
in
if Bdate is null then null else Gooddate
in
FixDateBn
Appreciate any help.
Here is the sql that converts from hex to regular date. Don't know how to do this in Power Query M language.
CREATE FUNCTION "GetDateFromWDate" (:wDate CHAR(3)) RETURNS DATE AS
BEGIN
DECLARE :tmpdate DATE
SET :tmpdate = '1900-01-01';
IF (:wDate <> null) and (:wDate <> '') THEN
SET :tmpdate = DATEFROMPARTS (ascii(substring(:wDate, 1, 1)) + 1900, ascii(substring(:wDate, 2, 1)), ascii(substring(:wDate, 3, 1)));
END IF;
RETURN :tmpdate;
END
Thanks
Sammy

Calculating results pro rata over several months with PowerQuery

I am currently stuck on below issue:
I have two tables that I have to work with, one contains financial information for vessels and the other contains arrival and departure time for vessels. I get my data combining multiple excel sheets from different folders:
financialTable
voyageTimeTable
I have to calculate the result for above voyage, and apportion the result over June, July and August for both estimated and updated.
Time in June : 4 hours (20/06/2020 20:00 - 23:59) + 10 days (21/06/2020 00:00 - 30/06/2020 23:59) = 10.1666
Time in July : 31 full days
Time in August: 1 day + 14 hours (02/08/2020 00:00 - 14:00) = 1.5833
Total voyage duration = 10.1666 + 31 + 1.5833 = 42.7499
The result for the "updated" financialItem would be the following:
Result June : 100*(10.1666/42.7499) = 23.7816
Result July : 100*(31/42.7499) = 72.5148
Result August : 100*(1.5833/42.7499) = 3.7036
sum = 100
and then for "estimated" it would be twice of everything above.
This is the format I ideally would like to get:
prorataResultTable
I have to do this for multiple vessels, with multiple timespans and several voyage numbers.
Eagerly awaiting responses, if any. Many thanks in advance.
Brds,
Not sure if you're still looking for an answer, but code below gives me your expected output:
let
financialTable = Table.FromRows({{"A", 1, "profit/loss", 200, 100}}, type table [vesselName = text, vesselNumber = Int64.Type, financialItem = text, estimated = number, updated = number]),
voyageTimeTable = Table.FromRows({{"A", 1, #datetime(2020, 6, 20, 20, 0, 0), #datetime(2020, 8, 2, 14, 0, 0)}}, type table [vesselName = text, vesselNumber = Int64.Type, voyageStartDatetime = datetime, voyageEndDatetime = datetime]),
joined =
let
joined = Table.NestedJoin(financialTable, {"vesselName", "vesselNumber"}, voyageTimeTable, {"vesselName", "vesselNumber"}, "$toExpand", JoinKind.LeftOuter),
expanded = Table.ExpandTableColumn(joined, "$toExpand", {"voyageStartDatetime", "voyageEndDatetime"})
in expanded,
toExpand = Table.AddColumn(joined, "$toExpand", (currentRow as record) =>
let
voyageInclusiveStart = DateTime.From(currentRow[voyageStartDatetime]),
voyageExclusiveEnd = DateTime.From(currentRow[voyageEndDatetime]),
voyageDurationInDays = Duration.TotalDays(voyageExclusiveEnd - voyageInclusiveStart),
createRecordForPeriod = (someInclusiveStart as datetime) => [
inclusiveStart = someInclusiveStart,
exclusiveEnd = List.Min({
DateTime.From(Date.EndOfMonth(DateTime.Date(someInclusiveStart)) + #duration(1, 0, 0, 0)),
voyageExclusiveEnd
}),
durationInDays = Duration.TotalDays(exclusiveEnd - inclusiveStart),
prorataDuration = durationInDays / voyageDurationInDays,
estimated = prorataDuration * currentRow[estimated],
updated = prorataDuration * currentRow[updated],
month = Date.MonthName(DateTime.Date(inclusiveStart)),
year = Date.Year(inclusiveStart)
],
monthlyRecords = List.Generate(
() => createRecordForPeriod(voyageInclusiveStart),
each [inclusiveStart] < voyageExclusiveEnd,
each createRecordForPeriod([exclusiveEnd])
),
toTable = Table.FromRecords(monthlyRecords)
in toTable
),
expanded =
let
dropped = Table.RemoveColumns(toExpand, {"estimated", "updated", "voyageStartDatetime", "voyageEndDatetime"}),
expanded = Table.ExpandTableColumn(dropped, "$toExpand", {"month", "year", "estimated", "updated"})
in expanded
in
expanded
The code tries to:
join financialTable and voyageTimeTable, so that for each vesselName and vesselNumber combination, we know: estimated, updated, voyageStartDatetime and voyageEndDatetime.
generate a list of months for the period between voyageStartDatetime and voyageEndDatetime (which get expanded into new table rows)
for each month (in the list), do all the arithmetic you mention in your question
get rid of some columns (like the old estimated and updated columns)
I recommend testing it with different vesselNames and vesselNumbers from your dataset, just to see if the output is always correct (I think it should be).
You should be able to manually inspect the cells in the $toExpand column (of the toExpand step/expression) to see the nested rows before they get expanded.

Making Date With Given Numbers

I have the following Swift (Swift 3) function to make a date (Date) with date components (DateComponents).
func makeDate(year: Int, month: Int, day: Int, hr: Int, min: Int, sec: Int) -> NSDate {
let calendar = NSCalendar(calendarIdentifier: .gregorian)!
let components = NSDateComponents()
components.year = year
components.month = month
components.day = day
components.hour = hr
components.minute = min
components.second = sec
let date = calendar.date(from: components as DateComponents)
return date! as NSDate
}
If I use it, it will return a GMT date.
override func viewDidLoad() {
super.viewDidLoad()
let d = makeDate(year: 2017, month: 1, day: 8, hr: 22, min: 16, sec: 50)
print(d) // 2017-01-08 13:16:50 +0000
}
What I actually want to return is a date (2017-01-08 22:16:50) literally based on those numbers. How can I do that with DateComponents? Thanks.
The function does return the proper date. It's the print function which displays the date in UTC.
By the way, the native Swift 3 version of your function is
func makeDate(year: Int, month: Int, day: Int, hr: Int, min: Int, sec: Int) -> Date {
var calendar = Calendar(identifier: .gregorian)
// calendar.timeZone = TimeZone(secondsFromGMT: 0)!
let components = DateComponents(year: year, month: month, day: day, hour: hr, minute: min, second: sec)
return calendar.date(from: components)!
}
But if you really want to have UTC date, uncomment the line to set the time zone.
NSDate doesn't know anything about time zones. It represents a point in time independent of any calendars or time zones. Only when printing it out like you did here it is converted to GMT. That's OK though - this is only meant for debugging. For real output use a NSDateFormatter to convert the date to a string.
As a hacky solution you might of course just configure your calendar to use GMT when creating your date object from your components. That way you will get the string you expect. Of course any other calculation with that date then might end up wrong.

Initializing boost date to the first year of AD

I would want to initialize a gregorian date like this
boost::gregorian::date d = { 1, 1, 1 };
but year = 1 is not allowed.
How can I create a day before Jesus was born ?
The current implementation of Gregorian date supports dates in the range 1400-Jan-01 to 9999-Dec-31. So handling a date outside this range is no directly possible.