I am using Laravel + Backpack for admin panel and I am trying to display a chart with 12 labels (for each month) and integer data corresponding for each month. My problem is that all datasets go under the first label and I cannot figure out how to put each dataset under the correct label.
Code (keep it short):
$array; // array of eloquent models
$array2; // array of eloquent models
$this->chart->labels([
'January',
'February',
]);
$this->chart->dataset('January Exp', 'bar', count($array))
->color('rgba(205, 32, 31, 1)')
->backgroundColor('rgba(205, 32, 31, 0.4)');
$this->chart->dataset('February Exp', 'bar', count($array2))
->color('rgba(205, 32, 31, 1)')
->backgroundColor('rgba(205, 32, 31, 0.4)');
I have tried looking up on Google of a way I can bind each dataset to a particular label, but I was not able to find a solution. Perhaps I am going at it from the wrong direction.
Following Backpack's docs and Laravel Charts' docs I should be able to declare the labels and the datasets and those should show up as expected.
I have also tried declaring the datasets in the setup() function as well as the data() function. Both ways lead to the same result.
Found a solution that seems to work for my use case.
Declare the labels then one dataset with an array for the values.
Code:
$array; // array of eloquent models
$array2; // array of eloquent models
$this->chart->labels([
'January',
'February',
]);
$this->chart->dataset('Exp', 'bar', [count($array), count($array2)])
->color('rgba(205, 32, 31, 1)')
->backgroundColor('rgba(205, 32, 31, 0.4)');
I hope someone finds this useful in the future.
Related
I have a list of DateTime items, I want to get the earliest and latest items in that list for initializing a date range picker with min/max values based on the available data in db.
I have a tough time tracking the "behind the scenes" in loops and the reduce operator. So I'm looking for any help in making this happen efficiently, cause I'm sure I can make this happen inefficiently with 10 loops :P
What I've tried so far, random stuff that obviously is way off. Double loops and looping with reduce and .isbefore or .isAfter. Can't figure this out. My code below is a hodgepodge but it's where I'm at now. I guess another option is to simple order the list by date and take first and last, but I want to see the best solution and I think this is the right place to ask.
This is what I'm trying to apply.
List<DateTime> dateList = [
DateTime(2021, 7, 30),
DateTime(2021, 6, 25),
DateTime(2021, 5, 14),
DateTime(2021, 4, 2),
DateTime(2021, 3, 12),
DateTime(2021, 3, 21)
];
List<DateTime> databaseDateRange() {
var databaseDateRange = <DateTime>[];
for (var item in dateList) {
dateList.reduce((value, element) {
if (value.isAfter(item)) {
databaseDateRange.add(value.xAxis);
}
});
}
return databaseDateRange;
}
print(dateList.reduce((min, e) => e.isBefore(min)? e : min));
In order to get the minimum in the list (i.e earliest date) you can do as #the38amorim said.
I.e
print(dateList.reduce((min, e) => e.isBefore(min) ? e : min));
The above iterates through each one and checks whether the new value is before the saved one, and replaces it if that is the case, otherwise keeps the old one.
For maximum value in the list (i.e latest date), you can conversely do:
print(dateList.reduce((min, e) => e.isAfter(min) ? e : min));
More on reduce here: https://api.dart.dev/stable/2.18.1/dart-core/Iterable/reduce.html
I have the file "global power plants" with a column "capacity_in_mw" (with numbers 30, 100, 45, ...) and another column is "primary_fuel" (Coal, Hydro, Oil, Solar, Nuclear, Wind, Coal).
I can generate a map in function of "capacity_in_mw" by setting the condition
plotdata = data.query('capacity_in_mw > 50')
Now, I would like to generate a map in function of "primary_fuel". Because data is alphanumeric, how do I set up the condition?
Furthermore, when making the map, to assign color='black' for Coal, color='green' for Wind, color='yellow' for Solar, ... etc.
Python.
I am a novice, but I think I found the solution. It is more of an issue of syntax, to use the == in the query to identify the alphanumeric value.
plotdata = data.query('primary_fuel == "Hydro"')
Also, lesson learned in the future to dig more before posting a question.
I have the following spreadsheet I am looking to solve a somewhat simple problem for:
Spreadsheet
Data tab contains the irregular data (as seen by the dates)
Chart 1 tab contains a working chart as I wish it to be presented
Chart 2 tab contains the new array structure I wish to populate
I cannot for the life of me figure out how to populate cell x,y on the Chart 2 array when matching x & y from the Data tab. ie - replicating 322 from cell Data - F3 to cell Chart 2 - H75 etc
INDEX - MATCH doesn't work as it references the cell location, not the cell contents - and as the array, I am graphing from is dynamic, this doesn't work.
The intended outcome is to be able to then QUERY the resultant data and plot an Age vs Weight chart instead of current Date vs Weight chart.
paste this into H4 cell and drag to the right:
=ARRAYFORMULA(IFERROR(VLOOKUP($G$4:$G, Data!$A$2:$U,
IFS(H2="Bear", 2,
H2="Bwuce", 3,
H2="Calcifer", 4,
H2="Capt. Kwazy Sox", 5,
H2="Chooie", 6,
H2="Lil Fibbs", 7,
H2="Howl", 8,
H2="JC", 9,
H2="Jim Rat", 10,
H2="Milo Jr", 11,
H2="Sgt. Simba Sox", 12,
H2="Angel", 14,
H2="Fern", 15,
H2="Fuzznut", 16,
H2="Houdini", 17,
H2="Kami", 18,
H2="Mumma Bear", 19,
H2="Rosie J", 20,
H2="Sophie", 21), 0), ))
Right now I have a datetime object, but it doesn't contain all the fields. It's missing minutes, seconds, and microseconds. I want to use this to fetch data from MongoDB through Python. i'm just wondering if pymongo will automatically fill in the missing fields and convert it into ISODATE, or it's going to produce an error?
EDIT:
an example:
time1='2015-12-17 23'
#assuming already imported all necessary libs
time2=datetime.strptime(time1, '%Y-%m-%d %H')
#for mongo
sq = {'$and': [something.some, {'field1':{'ne':1}}]}
sq['$and'].append({'field2': {'$gt': time2}})
In python, there's no such thing as a datetime object with missing fields. All the datetime fields (relevant to this question) always have values.
The way you created time2, the fields you didn't specify get the value 0, as demostrated here:
% time2
=> datetime.datetime(2015, 12, 17, 23, 0)
% time2.minute, time2.second, time2.microsecond
=> (0, 0, 0)
time2 == datetime(2015, 12, 17, 23, 0,0,0)
=> True
As demostrated above, your time2 object is identical to the object you would have gotten if were manually filling the values with zeros.
Now that you see your datetime object is not missing anything, it should be clear how mongodb treats it.
Why are you not allowed to add days to a date in std.datetime? You can add months and years, but not days.
Recently I had to calculate a date for Easter Sunday, then I had to calculate related holidays (Ascension, Pentecost, Trinity, Corpus) by adding a certain number of days (39, 10, 7, 4) to the last date.
I ended up using dayOfYear:
date.dayOfYear(date.dayOfYear + offset);
This worked well, but only because I remained within the same year. What if I have to add 50 days to, say, dec 28?
Is there an easy way of doing this that I have overlooked?
You can use Duration from core.time.
Importing std.datetime will import core.time, so you can use it directly as follows.
import std.stdio, std.datetime;
void main() {
auto date = Date(2013, 12, 28);
writefln("%s + %s = %s", date, 10.days(), date + 10.days());
}
BTW, days() is an alias to dur!"days"() which constructs a Duration struct.
Check the documentation of core.time http://dlang.org/phobos/core_time.html for more information.
If you haven't read this article on std.datetime yet, then you probably should, as it will probably answer most basic questions that you have for how to use it.
But in general, core.time.Duration is what you should be using for adding and subtracting units from any of the time point types in std.datetime (SysTime, DateTime, Date, or TimeOfDay). So, you get code like
auto date = Date(2012, 12, 21);
date += dur!"days"(50);
or
auto date = Date(2012, 12, 21);
date += days(50);
(The templated dur function is the generic way to generate a Duration, but it has aliases for each of the units that it supports, so stuff like seconds(5) or 22.minutes() will work as well).
The add function exists for "months" and "years", because a Duration can't hold months or years (because you can't convert between them and smaller units without a specific date), and there needs to be a way to add months or years to a time point. Also, there's the question of what to do when you add or subtract a month or year to/from a date, and the month that it moves to doesn't include that day, so add accepts AllowDayOverflow in order to control that (which wouldn't be necessary with smaller units).
auto d3 = Date(2000, 2, 29);
d3.add!"years"(1);
assert(d3 == Date(2001, 3, 1));
auto d4 = Date(2000, 2, 29);
d4.add!"years"(1, AllowDayOverflow.no);
assert(d4 == Date(2001, 2, 28));
But add doesn't accept any other units, because you can simply use the normal arithmetic operations with a Duration. Also, subtracting two time points will result in a Duration.
assert(Date(2012, 12, 5) - Date(2002, 11, 17) == dur!"days"(3671));
assert(Date(2012, 12, 5) - dur!"days"(3671) == Date(2002, 11, 17));
Unlike add, roll accepts all of the units in the type rather than just "months" and "years", but that's because it's doing a different operation from +, and so adding or subtracting a Duration won't work (as that already adds or subtracts). Rather roll adds to a specific unit without adding to the others.
auto d = Date(2010, 1, 1);
d.roll!"days"(33);
assert(d == Date(2010, 1, 3));
You can useroll method:
date.roll!"days"(50);
I did overlook it: you can use dayOfGregorianCal:
import std.stdio, std.datetime;
void main() {
auto d = Date(2012, 12, 28);
writeln(d); // 2012-Dec-28
d.dayOfGregorianCal(d.dayOfGregorianCal + 50);
writeln(d); // 2013-Feb-16
}