I came across this nice chart from Amcharts which animates a pie chart through years:
https://www.amcharts.com/demos/animated-time-line-pie-chart/#code
However I am looking to include months and years, any idea how to modify it to include months?
Thanks!
The current loops increases by 1 each year. Basically now the loop has to somehow go through an order which increases by month and year. I tried the following but it does not work:
// Animate chart data
var currentYear = 1995;
var currentMonth = 0;
function getCurrentData() {
var data = chartData[currentYear][currentMonth];
currentMonth++;
if (currentMonth > 11) {
currentMonth = 0;
currentYear++;
}
if (currentYear > 2014) {
currentYear = 1995;
}
return data;
}
function loop() {
label.set("text", currentYear + "-" + (currentMonth + 1));
var data = getCurrentData();
for (var i = 0; i < data.length; i++) {
series.data.setIndex(i, data[i]);
}
chart.setTimeout(loop, 4000);
}
loop();
Related
My javascript is horrible, I am a C# dev but here we are. So I need to loop through every month between a start year and month to an end year and month.
The startMonth and startYear will always be 1 and 2010 respectively.
I have the following solution at the moment:
//Get the current year and month
let currentTime = new Date();
let currentYear = currentTime.getFullYear();
let currentMonth = currentTime.getMonth() + 1;
//Set starting year and month
let startYear = 2010;
let startMonth = 1;
//Loop through the years and months
for (let i = startYear; i <= currentYear; i++){
if (i === currentYear ){
for (let k = 1; k <= currentMonth; k++){
//Do work
}
} else {
for (let j = startMonth; j <= 12; j++){
//Do work
}
}
}
Does someone have a better solution? I feel like this is really clunky. I don't mind using third party packages so if moment or something will work then I'll use it.
Here is what you want :
const start = moment.utc('2018-12');
const end = moment.utc('2020-02');
const year = end.diff(start, 'years');
const month = end.diff(start, 'months')
console.log(start, end);
console.log(year, month);// 1 14
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
First off, be aware that months run from 0 to 11 so January is 0 not 1!
JavaScript's Date class automatically handles "overflows". So for example a date
new Date(2010, 12, 1)
automatically becomes "January 1st, 2011".
This can be used to simply increment just the month of a date:
const currentDate = new Date(2010, 0, 1);
const endDate = new Date();
endMonth.setMonth(endMonth.getMonth() + 1);
while (currentDate.getFullYear() != endMonth.getFullYear() && currentDate.getMonth() != endMonth.getMonth()) {
// Do something
currentDate.setMonth(currentDate.getMonth() + 1);
}
(Watch out that is could result in an endless loop if the start date is already after the end date.)
Here is a slight improvement without adding a third party library:
//Get the current year and month
let currentTime = new Date();
let currentYear = currentTime.getFullYear();
let currentMonth = currentTime.getMonth() + 1;
//Set starting year and month
let startYear = 2010;
let startMonth = 1;
//Loop through the years and months
for (let i = startYear; i <= currentYear; i++){
for (let j = startMonth; j <= (i == currentYear ? currentMonth: 12); j++){
//Do work
}
}
In below code I can generate array of years but it's not dynamic. Year should start from 1901 to current year and in reverse order.
const theDates = [];
let cell = 1900;
for (let x = 0; x < 24; x += 1) {
const row = [];
for (let y = 0; y < 5; y += 1) {
row.push(cell += 1);
}
theDates.push(row.reverse());
}
console.log(JSON.stringify(theDates.reverse()).toString());
Currently, I am generating years till 2020 but I am hard-coding it. I need something like below, say that this is year 2023
[[2023,2022,2021,2020,2019], ... [1903,1902,1901,null,null]]
At any given point in time there should be 5 items in the inner array and last empty array values can be null
thanks in advance :)
Get the year with new Date().getFullYear(), then you can push chunks of 5 while the year number is greater than 1901:
const theDates = [];
for (let year = new Date().getFullYear(); year !== null;) {
const row = [];
for (let i = 0; i < 5; i++, year = (year > 1901 ? year - 1 : null)) {
row.push(year);
}
theDates.push(row);
}
console.log(theDates);
Example of year other than 2020:
const theDates = [];
for (let year = 2023; year !== null;) {
const row = [];
for (let i = 0; i < 5; i++, year = (year > 1901 ? year - 1 : null)) {
row.push(year);
}
theDates.push(row);
}
console.log(theDates);
I haven't tested it, but according to google you can do something like
var today = new Date();
var year = today.getFullYear();
since getFullYear() – Provides current year.
I am trying to calculate Month name and year using moment for a range.
for example if the start year and month is 2016-02, and end year is 2017-11,
I want Moment to create date in
Feb-16 , Mar-16 ..Jan-17, Nov-17
Can you provide some idea? This is what I ma trying to do:
const startYear = 2016;
const startMonth = 2;
const endYear = 2017;
const endMonth = 12;
var ind = startMonth;
var i=0;
while(i < 24){
if(ind == 13){
ind = 1;
startYear++;
}
header = moment(startYear + '-'+ ind, 'YYYY-M').format('MMM-YY');
i++;
ind++;
return header ;
}
The output should be like :
Feb-16 Mar-16 Apr-16 May-16......Jan-17....Nov-17
when momentjs a library, try to use the convenience methods that they have for date manipulation instead of just using it for parsing
var startDate = moment('2012-1');
var endDate = moment('2013-1');
var out = [startDate];
while (startDate.isBefore(endDate)) {
startDate = startDate.add(1, 'month');
out.push(startDate);
}
I would like to be able to loop through an XML file to find the earliest and latest date. Once those dates are found, I want to loop through the records again and compare the month and year from each record to the date that is being passed in. If the month and year match, then the count is increased by one. After the loop goes through all of the records, the date and count are added to an array, the date being passed in is then increased by one, and the loops runs again. The while loop continues to run until the date being increased is greater than the latest record date.
MY CODE:
var record=xmlDoc.getElementsByTagName("record");
var maxDate = 0;
// Loop through all of the records to find the latest date
for(var i=0; i<record.length; i++)
{
var tempMaxDate = record[i].getElementsByTagName("visit_date")[0].childNodes[0].nodeValue;
if(tempMaxDate > maxDate)
{
maxDate = tempMaxDate;
}
}
var minDate = 99999999999999;
// Loop through all of the records to find the earliest date
for(var i=0; i<record.length; i++)
{
var tempMinDate = record[i].getElementsByTagName("visit_date")[0].childNodes[0].nodeValue;
if(tempMinDate < minDate)
{
minDate = tempMinDate;
}
}
var minDateGraph = minDate;
var count = 0;
var data = [];
// Loop until the minimum date is greater than the maximum
while(minDate <= maxDate)
{
var tempMinDate2 = new Date(0);
tempMinDate2.setMilliseconds(minDate);
minDate = tempMinDate2;
// Loop through all of the records
// If the month and year of minDate match the record then the count is increased
for(var i=0; i<record.length; i++)
{
var tempVisitDate = record[i].getElementsByTagName("visit_date")[0].childNodes[0].nodeValue;
var visitDate = new Date(0);
visitDate.setMilliseconds(tempVisitDate);
if((visitDate.getMonth() == minDate.getMonth()) && (visitDate.getFullYear() == minDate.getFullYear()))
{
count += 1;
}
}
var tempData = [minDate, count];
data.push(tempData);
var month = minDate.getMonth();
var year = minDate.getFullYear();
// Increase the minDate by one month
if(month == 12)
{
year += 1;
month = 1;
}
else
{
month += 1;
}
var tempMinDate3 = new Date(year, month, 1).getTime();
minDate = tempMinDate3;
}
The problem is when I run my current code, the browser crashes. I think I am running into an infinite loop, but I cannot find what is causing it. I would appreciate any suggestions. Thanks!
Hopefully someone can help.
I have the following function to return the last 6 monthly names (I.E June, July, August etc), however, I cannot work out how I would then use each returned month name as separate variables.
I need this so I can feed each month name in to an sql query that populates a table for the last 6 months.
Any help much appreciated.
function getMonths()
{
var today = new Date();
var month = 1;
var currMonth = month-3;
var monthArray = new Array("January","February","March","April","May","June",
"July","August","September","October","November","December");
var menuMonths = new Array();
var count = 6;
var buffer = 10;
while(count >0)
{
if (currMonth < 0)
currMonth += 12;
if (currMonth >=12 )
currMonth -= 12;
var month = monthArray[currMonth];
menuMonths.push(month);
currMonth = currMonth -1;
count = count -1;
}
return (menuMonths.reverse());
}
console.log (getMonths());
You can use Array.slice()
FIDDLE http://jsfiddle.net/BeNdErR/NF5Qm/1/
CODE
var monthArray = new Array("January","February","March","April","May","June",
"July","August","September","October","November","December");
var currMonth = 3; // the current month index, from 1 to 12
var firstMonth = currMonth - 6;
if(firstMonth < 0){ //for example if currMonth is January - 0
var months = [];
months.push(monthArray.slice(12 - Math.abs(firstMonth), 12));
months.push(monthArray.slice(0, currMonth));
alert(months);
}else{
alert(monthArray.slice(firstMonth, currMonth))
}
As output, you still have an array, so you can pass it to the SQL Query as it is, for example (SQLite):
tx.executeSql("SELECT * FROM tablename WHERE month = ? OR month = ? month = ? OR month = ? month = ? OR month = ?;", [slicedMonthArray], successCB, errorCB);
Hope it helps
var months = getMonths();
for(var i=0, len=months.length; i<len; i++){
var currentMonth = months[i];
alert(currentMonth);
//do whatever you want here;
}