wasted lot of hours searching up and down, but didnt found my very simple needing.
I just only need xaxis showing: (weekdays rolling looping repeating)
Mon tue wed thu wed sat sun Mon tue wed thu wed sat sun Mon tue wed thu wed sat sun Mon tue wed thu wed sat sun ....... repeating or looping the 7 days till infinite (Y has a value).
This should be very easy to make, should be 1st chapter in the first demo lines but No Way, noone treat the simple.
Thank YOU in advance
Best regards
eng G. Bono from Italy Turin.
For a categorized axis you can for example programmatically create an array with weekdays, for example:
var data = [...],
categories = ['mon', 'tue', 'wed', 'thu', 'wed', 'sat', 'sun'],
i = 7;
for (; i < data.length; i++) {
categories.push(categories[i - 7]);
}
Highcharts.chart('container', {
series: [{
data: data
}],
xAxis: {
categories: categories
}
});
Live demo: http://jsfiddle.net/BlackLabel/6m4e8x0y/4962/
For a datetime axis you need to only use the proper format:
xAxis: {
type: 'datetime',
labels: {
formatter: function() {
return Highcharts.dateFormat('%a', this.value)
}
}
}
Live demo: http://jsfiddle.net/BlackLabel/6m4e8x0y/4963/
API Reference:
https://api.highcharts.com/highcharts/xAxis.type
https://api.highcharts.com/class-reference/Highcharts#.dateFormat
https://api.highcharts.com/highcharts/xAxis.labels.formatter
Related
I have some data in a table that looks like the following:
date apples pears oranges
1 3 2 0
2 1 5 0
3 0 2 1
4 0 0 1
5 0 1 1
I can plot the count of a fruit by date using:
<%= line_chart FruitCount.pluck(:date, :apples), xtitle: "Date", ytitle: "Count" %>
I can't workout how to plot apples, pears and oranges onto the same plot. I thought it might be something like the following:
<%= line_chart [
{ name: Apples, data: FruitCount.pluck(:date, :apples) },
{ name: Pears, data: FruitCount.pluck(:date, :pears) }
{ name: Oranges, data: FruitCount.pluck(:date, :oranges) }
], xtitle: "Date", ytitle: "Count" %>
All help greatly appreciated.
Edit:
The issue was a missed comma and the names not being strings. The code below works:
<%= line_chart [
{ name: "Apples", data: FruitCount.pluck(:date, :apples) },
{ name: "Pears", data: FruitCount.pluck(:date, :pears) },
{ name: "Oranges", data: FruitCount.pluck(:date, :oranges) },
], xtitle: "Date", ytitle: "Count" %>
I have a line chart showing items that are "expiring" on a given date grouped by month using line_chart. I like the built-in feature of clicking on the color keys to show/hide each line. I use a complicated query to get info in this format:
{["Footwear", Sat, 01 May 2021]=>0,
["Footwear", Tue, 01 Jun 2021]=>1,
["Footwear", Thu, 01 Jul 2021]=>0,
...
["Coat", Sun, 01 Aug 2021]=>0,
["Coat", Wed, 01 Sep 2021]=>0,
["Coat", Fri, 01 Oct 2021]=>0,
["Coat", Mon, 01 Nov 2021]=>0,
...
["Helmet", Wed, 01 Dec 2021]=>0,
["Helmet", Sat, 01 Jan 2022]=>0,
["Helmet", Tue, 01 Feb 2022]=>1,
["Helmet", Tue, 01 Mar 2022]=>2,
["Helmet", Fri, 01 Apr 2022]=>0...}
So just hand line_chart a hash with data like: ["Apple", <date>] => count] and chartkick will do the rest. I have a very limited view of your table structures but going on what you have given us I think you could do:
hash_for_chart = {}
FruitCount.pluck(:date, :apples, :pears, :oranges).each do |arr|
hash_for_chart['Apples', arr[0]] = arr[1]
hash_for_chart['Pears', arr[0]] = arr[2]
hash_for_chart['Oranges', arr[0]] = arr[3]
end
Here we are grabbing every FruitCount record, plucking the date and the three counts so we have an array of those values. Then we add a hash pair for each of the three values on that date to a hash that we will pass to line_chart
line_chart hash_for_chart
There may be a more complex AR query that could give you the same data but hard for me to say with such limited info about your DB.
I have this information:
Days Dec'15 Jan'16
---------------------
Sun 27
Mon 28
Tue 29
Wed 30
Thu 31
Fri 1
Sat 2
I have 1st Jan'16. So I have to get Fri and then the difference of days from the Sun. So, in this case, the difference should be 5. Because, before Friday there are 5 other days. So if I want to know it for 2nd Jan'16 it should be 6. And likewise.
How do I get it easy with date functions?
The following code may help you
extension Date {
func weekdayDiffence() -> Int {
return Calendar.current.dateComponents([Calendar.Component.weekday], from: self).weekday ?? 0
}
}
Example
let d = Date().weekdayDiffence()
print(d)
I have an array of hashes like this:
[{Mon, 09 May 2016 14:49:17 UTC +00:00=>12},
{Sun, 17 Apr 2016 14:08:40 UTC +00:00=>30},
{Sun, 16 Apr 2016 14:08:40 UTC +00:00=>18},
{Sun, 15 Apr 2016 14:03:33 UTC +00:00=>21}]
How can I sum the previous value from the oldest date to the current date, my expected output will be:
[{Mon, 09 May 2016 14:49:17 UTC +00:00=>81},
{Sun, 17 Apr 2016 14:08:40 UTC +00:00=>69},
{Sun, 16 Apr 2016 14:08:40 UTC +00:00=>39},
{Sun, 15 Apr 2016 14:03:33 UTC +00:00=>21}]
Thanks!
Assuming that the key of every hash in your array is a DateTime object, you can get what your want with this:
balance = [
{DateTime.parse('Mon, 09 May 2016 14:49:17 UTC +00:00')=>12},
{DateTime.parse('Sun, 17 Apr 2016 14:08:40 UTC +00:00')=>30},
{DateTime.parse('Sun, 16 Apr 2016 14:08:40 UTC +00:00')=>18},
{DateTime.parse('Sun, 15 Apr 2016 14:03:33 UTC +00:00')=>21}
] # => your original array
# Get expected array.
balance.map{ |h|
{
h.keys.first => balance.select{ |e|
e.keys.first <= h.keys.first }.map{ |s|
s[s.keys.first] }.reduce(:+)
}
}
I split the code in lines in order to improve readability.
Another approach would be to sort the array first and then use the map function to keep a running total to collect the required data.
# sort the balances by date
balance = balance.sort {|a, b| a.keys.first <=> b.keys.first }
# get running total and collect for each date
total = 0
balance.map do |entry|
date, value = entry.first
total += value
{date => total}
end
I assume your array is in lastest-to-earliest date order and looks something like arr below:
a = [{ "Mon, 09 May 2016 14:49:17 UTC +00:00"=>12 },
{ "Sun, 17 Apr 2016 14:08:40 UTC +00:00"=>30 },
{ "Sun, 16 Apr 2016 14:08:40 UTC +00:00"=>18 },
{ "Sun, 15 Apr 2016 14:03:33 UTC +00:00"=>21 }]
require 'date'
arr = a.map do |h|
(d, v) = h.to_a.first
{ DateTime.parse(d) => v }
end
#=> [{#<DateTime: 2016-05-09T14:49:17+00:00 ((2457518j,53357s,0n),+0s,2299161j)>=>12},
# {#<DateTime: 2016-04-17T14:08:40+00:00 ((2457496j,50920s,0n),+0s,2299161j)>=>30},
# {#<DateTime: 2016-04-16T14:08:40+00:00 ((2457495j,50920s,0n),+0s,2299161j)>=>18},
# {#<DateTime: 2016-04-15T14:03:33+00:00 ((2457494j,50613s,0n),+0s,2299161j)>=>21}]
We can then compute the required array as follows.
cumv = 0
arr.reverse.
map { |h| h.to_a.first }.
each_with_object([]) do |(d,v),a|
cumv += v
a << { d => cumv }
end.
reverse
#=> [{#<DateTime: 2016-05-09T14:49:17+00:00 ((2457518j,53357s,0n),+0s,2299161j)>=>81},
# {#<DateTime: 2016-04-17T14:08:40+00:00 ((2457496j,50920s,0n),+0s,2299161j)>=>69},
# {#<DateTime: 2016-04-16T14:08:40+00:00 ((2457495j,50920s,0n),+0s,2299161j)>=>39},
# {#<DateTime: 2016-04-15T14:03:33+00:00 ((2457494j,50613s,0n),+0s,2299161j)>=>21}]
My network only achieve around 80%, but the reported best score is around 85% accuracy. I m using same input data and same initalization. I dont know whats wrong, so I try to check my gradients and implemented what is recommended for gradient checking: http://ufldl.stanford.edu/tutorial/supervised/DebuggingGradientChecking/
But i m not sure, if my implementation is correct:
public void gradientchecking(double[] theta){
System.out.println("Gradient Checking started");
//costfunction returns cost and gradients
IPair<Double, double[]> org = costfunction(theta);
double[] theta_pos = new double[theta.length];
double[] theta_neg = new double[theta.length];
for (int i = 0; i < theta.length; i++) {
theta_pos[i]= theta[i];
theta_neg[i]=theta[i];
}
double mu = 1e-5;
for (int k = 0; k < 20; k++) {
theta_pos[k] = theta_pos[k] + mu;
theta_neg[k] = theta_neg[k] - mu;
IPair<Double, double[]> pos = costfunction(theta_pos);
IPair<Double, double[]> neg = costfunction(theta_neg);
System.out.println("Org: "+org.getSecond()[k] +" check:"+ ((pos.getSecond()[k]-neg.getSecond()[k])/(2*mu)));
//System.out.println("Org: "+org.getSecond()[k] +"check:"+ ((pos.getSecond()[k]-neg.getSecond()[k])/(2*mu)));
theta_pos[k] = theta_pos[k] - mu;
theta_neg[k] = theta_neg[k] + mu;
}
}
}
I got the following result after a freshly initialized theta:
Gradient Checking started
Cost: 1.1287071297725055 | Wrong: 124 | start: Thu Jul 30 22:57:08 CEST 2015 |end: Thu Jul 30 22:57:18 CEST 2015
Cost: 1.128707130295382 | Wrong: 124 | start: Thu Jul 30 22:57:18 CEST 2015 |end: Thu Jul 30 22:57:28 CEST 2015
Cost: 1.1287071292496391 | Wrong: 124 | start: Thu Jul 30 22:57:28 CEST 2015 |end: Thu Jul 30 22:57:38 CEST 2015
Org: 5.2287135944026004E-5 check:1.0184607936733826E-4
Cost: 1.1287071299252593 | Wrong: 124 | start: Thu Jul 30 22:57:38 CEST 2015 |end: Thu Jul 30 22:57:47 CEST 2015
Cost: 1.1287071296197628 | Wrong: 124 | start: Thu Jul 30 22:57:47 CEST 2015 |end: Thu Jul 30 22:57:56 CEST 2015
Org: 1.5274823511207024E-5 check:1.141254586229615E-4
Cost: 1.1287071299063134 | Wrong: 124 | start: Thu Jul 30 22:57:56 CEST 2015 |end: Thu Jul 30 22:58:05 CEST 2015
Cost: 1.1287071296387077 | Wrong: 124 | start: Thu Jul 30 22:58:05 CEST 2015 |end: Thu Jul 30 22:58:14 CEST 2015
Org: 1.3380293717695182E-5 check:1.0008639478696018E-4
Cost: 1.1287071297943114 | Wrong: 124 | start: Thu Jul 30 22:58:14 CEST 2015 |end: Thu Jul 30 22:58:23 CEST 2015
Cost: 1.1287071297507094 | Wrong: 124 | start: Thu Jul 30 22:58:23 CEST 2015 |end: Thu Jul 30 22:58:32 CEST 2015
Org: 2.1800899147740388E-6 check:9.980780136716263E-5
that indicates that my gradient calculation has an error, or the gradientchecking() method. I m not sure, can somebody help me?
In Java arrays are reference types.
int[] arr = { 8,7,6,5,4,3,2,1,8};
int[] b = arr;
b [0] = -10;
for (int i:arr) {
System.out.print (' ');
System.out.print (i);
}
outputs -10 7 6 5 4 3 2 1 8
So i mean that you incorrectly creating arrays
double[] theta_pos = theta;
double[] theta_neg = theta;
they are just references to theta, and by changing their contents you change theta also, +mu-mu = 0. Use clone() methods while copying array.
double[] theta_pos = theta.clone();
double[] theta_neg = theta.clone();
But remember that clone may not work as you expecting in some cases, with simple(non-reference) types it works ideal. Look at this
Does calling clone() on an array also clone its contents?
The following Ruby code gets me the first day of each month :
require 'active_support/all'
# get the date at the beginning of this month
date = Date.today.beginning_of_month
# get the first day of the next 5 months
5.times do |num|
date = date.next_month
p date
end
Which gives :
=> Fri, 01 Aug 2014
=> Mon, 01 Sep 2014
=> Wed, 01 Oct 2014
=> Sat, 01 Nov 2014
=> Mon, 01 Dec 2014
But how do I get the first Thursday of each month? i.e.
=> Thu, 07 Aug 2014
=> Thu, 04 Sep 2014
=> Thu, 02 Oct 2014
=> Thu, 06 Nov 2014
=> Thu, 04 Dec 2014
There's no need for iterations or conditions just get the so called delta of days till next thursday:
#4 is thursday because wday starts at 0 (sunday)
date = Date.today.beginning_of_month
date += (4 - date.wday) % 7
p date
=> Thu, 03 Jul 2014
That my opinion:
date_begin = Date.today.beginning_of_month
date_end = date_begin + 5.month
[*date_begin..date_end].select(&:thursday?).uniq(&:month)
=> [Thu, 03 Jul 2014, Thu, 07 Aug 2014, Thu, 04 Sep 2014, Thu, 02 Oct 2014, Thu, 06 Nov 2014]
Just for fun
class Date
def skip_to_thursday
# given current weekday, how many days we need to add for it to become thursday
# for example, for monday (weekday 1) it's 3 days
offset = lambda {|x| (4-x) % 7 }
self + offset[wday]
end
end
# get the date at the beginning of this month
date = Date.today.beginning_of_month
date.skip_to_thursday # => Thu, 03 Jul 2014
Here is my way :
def first_thursday
date = Date.today.beginning_of_month
date += 1 until date.wday == 4
date
end
first_thursday # => Thu, 03 Jul 2014
you can use something like this:
def first_thursday(months_ahead)
start_of_month = months_ahead.months.from_now.beginning_of_month.to_date
start_of_month += (4 - start_of_month.cwday) % 7
end
first_thursday 1
=> Thu, 07 Aug 2014
first_thursday 2
=> Thu, 04 Sep 2014
I ran into this problem for a recurring_events feature that I needed to build. I changed some of the variables to find the first Thursday but it also shows how you could evolve the answer to find the 2nd or 3rd Thursday (or any day of the week for that matter) if you had a week and day of the week count.
def find_thursday
start_of_month = DateTime.now.beginning_of_month
month_day = nil
loop do
month_day = start_of_month += 1.day
break if month_day.wday == find_weekday("Thu")
end
return month_day
end
def find_weekday
d = default_weekdays.find { |d| d[:day] == start_date.strftime("%a") }
d[:count]
end
def default_weekdays
return [
{ day: 'Sun', count: 0 },
{ day: 'Mon', count: 1 },
{ day: 'Tue', count: 2 },
{ day: 'Wed', count: 3 },
{ day: 'Thu', count: 4 },
{ day: 'Fri', count: 5 },
{ day: 'Sat', count: 6 },
]
end