How to display monthly data in Highcharts graph? - datetime

Looks like a dumb question. And probably is. But I don't really get it.
I have monthly data over a ~30-year-period. I am neither sure how to format the data, nor am I sure how to format the Highcharts graph.
What is the correct way of data formatting? Like a "running month", from 1 to 433 (and more):
Date,Value
1,338.45
2,339.15
3,339.47
or in Date.UTC format:
Date,Value
Date.UTC(1980,1,1),338.45
Date.UTC(1980,2,1),339.15
Date.UTC(1980,3,1),339.47
I've done something similar like this, but don't think this is a good style:
Date,Value
1 Jan 1993,
2 Jan 1993,
3 Jan 1993,
4 Jan 1993,
5 Jan 1993,-4.19413878126
6 Jan 1993,
7 Jan 1993,
For the xAxis definition I've see different parameters being used. But as this depends on the data format, not quite sure what to put there:
xAxis: {
type:"datetime",
min: Date.UTC(1980, 1, 1),
...
Thanks a lot for any hints in which direction I should go.

A piece of advice, as you asked:
Date.UTC(1980,1,1) - this is JavaScript function, to return timestamp. For example Date.UTC(1980,1,1) -> 318211200000. I suggest to use timestamp already in your data incoming for a chart. Note: Months in JS starts at 0 (0-> Jan, 1->Feb etc.).
min: Date.UTC(1980, 1, 1) -> this is not necessary, Highcharts lib is smart enough to calculate extremes on the xAxis, you don't have to set it, unless you want to display only part of the data on the chart.
And for example, I would use that format:
Date,Value
318211200000,338.45
320716800000,339.15
323395200000,339.47
Note that all formats are fine - I just suggest to use the easiest one ;)

to set up teh xAxis as a time period you have these options:
1. using Date.UTC(Year, Month, Day):
data: [
[Date.UTC(2016, 0, 5), 4],
[Date.UTC(2016, 1, 6), 6],
[Date.UTC(2016, 1, 7), 9],
check example
using Dates written as numbers, specifies the number of milliseconds since January 1, 1970, 00:00:00.
var xArr = [1455661712000,
1455661791000,
1455661869000,
1455661947000,
];
Check example
I hope these help, otherwise feel free to ask for more details.

Related

Initalizing 0 UTC Time generates a negative time when printed

In the provided code snippet the t2 time initialized explicitly to 0 UTC is negative when printed.
I don't understand why that is, could someone explain it for me?
func main() {
const IsoDatetimeFormat = "2006-01-02T15:04:05"
t1 := time.Time{}
t2 := time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
fmt.Println(t1.Format(IsoDatetimeFormat))
fmt.Println(t2.Format(IsoDatetimeFormat))
}
Output:
0001-01-01T00:00:00
-0001-11-30T00:00:00
playground link
time.Time documents that the zero value for time.Time represents / means January 1, year 1, 00:00:00.000000000 UTC:
The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC. As this time is unlikely to come up in practice, the IsZero method gives a simple way of detecting a time that has not been initialized explicitly.
There is no year 0, so if you use year, month and day values smaller than 1, they will roll over to negative. The time package accepts and normalizes values given outside of valid ranges. Quoting from time.Date():
The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.
The zero value to be January 1, year 1 was an arbitrary choice. As to reasoning why year 1 was chosen:
// The zero value for a Time is defined to be
// January 1, year 1, 00:00:00.000000000 UTC
// which (1) looks like a zero, or as close as you can get in a date
// (1-1-1 00:00:00 UTC), (2) is unlikely enough to arise in practice to
// be a suitable "not set" sentinel, unlike Jan 1 1970, and (3) has a
// non-negative year even in time zones west of UTC, unlike 1-1-0
// 00:00:00 UTC, which would be 12-31-(-1) 19:00:00 in New York.

Assistance finding the floor and ceiling of a vector

I have created vectors for months and month numbers, then for months 6-9, and from there, the mean...
mean(Summer)
Months[mean(Summmer)]
July
Now I have to use floor and ceiling functions to return upper and lower limits of months for average summer month.
I thought I was on the right track, but I'm getting errors! Thoughts?
months[x]
months[floor(x)] # July
months[ceiling(x)] # August
It's not exactly clear what you are doing, but here's my best effort to produce a working example in response:
# This is built into R and it gives month names
month.name
# Let's make seasons:
Spring = 3:5
Summer = 6:8
Autumn = 9:11
Winter = c(12,1,2)
# If each season has 3 months, then we can get out a particular monh like so:
month.name[Summer[1]]
month.name[Summer[2]]
month.name[Summer[3]]
# We can also use the min, mean, and max technique for summer:
month.name[min(Summer)]
month.name[mean(Summer)]
month.name[max(Summer)]
# But it doesn't really work for Winter:
month.name[min(Winter)]
month.name[mean(Winter)]
month.name[max(Winter)]
My guess is that the main confusion here is around the functionality of floor. Floor takes as input a vector and outputs a vector that applies the floor function (round down to nearest integer) to each element of the vector, so floor(c(1,2,3)) will return c(1,2,3) and floor(1.3) will return 1. Ceiling works similarly.
Hopefully this helps with whatever you are trying to do. In general, it's helpful to provide a reproducible example so that others can run the exact same code you are running and help debug your problem.

How to compare cumulative counter vs the best, average and worst using Graphite?

I have a counter that measures the number items sold every 10 minutes.
I currently use this to track the cumulative number of items:
alias(integral(app.items_sold), 'Today')
And it looks like this:
Now, what I want to do to show how well we were are doing TODAY vs best, avg (or may median) worst day we've had for the past say 90 days.
I tried something like this:
alias(integral(maxSeries(timeStack(app.items_sold, '1d', 0, 90))),'Max')
alias(integral(averageSeries(timeStack(app.items_sold, '1d', 0,90))), 'Avg')
alias(integral(minSeries(timeStack(app.items_sold, '1d',0, 90))), 'Min')
which looks great but actually shows me the cumulative amount of all the max, avg and min for all series interval.
Can anyone suggest a way to achieve what I'm looking for?
i.e. determine what the best (and worst and median) day was for the past 90 days and plot that. Can it be done using purely Graphite functions?
Thanks.
The answer was to just flip the order to the function calls: (maxSeries before integral)
Thanks to turner on the grafana#groups.io board for the answer
alias(maxSeries(integral(timeStack(app.items_sold, '1d', 0, 90))),'Max')
alias(averageSeries(integral(timeStack(app.items_sold, '1d', 0,90))), 'Avg')
alias(minSeries(integral(timeStack(app.items_sold, '1d',0, 90))), 'Min')

Altering the data on diagrams

I am working on a statistics project and i came across one tiny issue. I have to work out a tactic for bidding on horse racing. We were given a set of 300 races with data such as the time, favorable odds, and if the favorable won or not. So i thought I could order the table in ascending order in relation to the time, and see how are the wins spread during the day. So I had games starting from 13:35 to 21:20, so I divided the time into 1 hour chunks (except the 1st chunk which is only 25 min, and last chunk which is only 20 min) and related wins to those chunks respectively. Then I added up all the wins inside each chunk, and presented that on the chart, the chart looks like this:
It works with my tactic which is to play between 14:00 and 16:00 cause then the bookie starts making odds as favorable to himself and starts robbing you from your earned money he just gave you. The thing is that on the bottom it says 2, 4, 6, 8.. but I want to write 13:35-14:00, 14:00 - 15:00 etc.. How can I do that, given that the code I used is that:
> plot(chunksVector, type="o", col="blue", xlab="Time", ylab="Wins")
How can I do this? I've been struggling with this one for some time now. Is there any way to alter the code?
P.S: "chunks" is just the name i gave to separated wins based on 1 hour distance. So i basically have a chink13.35_14.00, chunk14.00_15.00, chunk15.00_16.00 etc.. What I want to alter is only the X axis.
I want it to look like this:
the quick answer is:
plot(chunksVector, type="o", col="blue", xlab="Time", ylab="Wins", xaxt="n")
axis(1, at=c(2,4,6,8), labels=c('14:00', '16:00', '18:00', '20:00'))
There are probably a few other ways messing about with time-series packages?
Instead of messing about with x labels, convert x to factor:
df <- data.frame(x=factor(c('14:00', '16:00', '18:00', '20:00')),
y=c(1,2,3,4))
plot(df)

Making DateTimeAxis with weekly labels set ticks on an arbitrary day of the week

I'm using a CartesianChart with a DateTimeAxis to display weekly data in a Flex application. When I set dataUnits="weeks" and labelUnits="weeks" on the DateTimeAxis, it automatically places each major tick on a Sunday. However, I would like to provide users with the option of beginning the week on a Sunday or a Monday. How can I ask the DateTimeAxis to instead place the major ticks on a Monday (or some other day of week)?
For example, if the user is looking at total sum of something over the week, and requests that weeks start on a Sunday, the Series data would look like:
x: Date(July 11, 2010) y: 25
x: Date(July 18, 2010) y: 30
x: Date(July 25, 2010) y: 32
etc.
If the weeks start on a Monday, the Series data would instead look like:
x: Date(July 12, 2010) y: 22
x: Date(July 19, 2010) y: 33
x: Date(July 26, 2010) y: 29
etc.
With the second data set, the major ticks are still on July 11, July 18, July 25, etc. but the bars are slightly shifted off-center from the major ticks.
Thanks!
I looked at the code of DateTimeAxis class and I'm almost sure you can't do what you want. If "alignLabelsToUnits" property is set to true (default) then labels are created by rounding Series data dates to appropriate units. In your case (weeks) it looks like this:
case "weeks":
d[hoursP] = 0;
d[minutesP] = 0;
d[secondsP] = 0;
d[millisecondsP] = 0;
if (d[dayP] != 0)
d[dateP] = d[dateP] + (7 - d[dayP]);
break;
So as you can see, it checks if processed date is first day of the week (hard-coded 0), and if it is not, it's changed to be such.
To achieve the behavior you seek, you'd probably have to override function for label creation and write another one for date rounding, as the default rounding function is declared private.
Even though you specify weeks, DateTimeAxis may still be using the hours and minutes values of the newly created date objects. You can format the date to set these to zero. Have a look at http://www.munkiihouse.com/?p=69, mention setting displayLocalTime=”true” in DateTimeAxis tag. Let us know if it works man

Resources