Google chart - remove rows from data.cache JSFiddle - charts

Task is to show only last 5 records in google chart. As you move slider, on end, data is adding.
Records are from some "slider", later will be replaced with some live senzor data.
hAxis is timestamp so i don't know how to use:
viewWindowMode: 'explicit',
// viewWindow: {
// min: 0,
// max: 4
// },
Sow question is how to remove rows from data cache?
JSFiddle: https://jsfiddle.net/ejovrh2/k59u6be3/17/

when using timestamp on the x-axis,
you can use a date object directly, no need to convert to a string.
var data = google.visualization.arrayToDataTable([
["Date", "level"],
[new Date(), level],
]);
and to control the range displayed on the x-axis,
viewWindow min & max should be the same data type.
in this case, a date.
viewWindow: {
min: new Date(2020, 10, 1),
max: new Date(2020, 10, 30)
},

As #WhiteHat said you can use data.getColumnRange and then extract min&max.
In my case I need to show just last few records no matter dates.
So I needed this part of code:
data.removeRow(0);
Answer
// when data is populated with 4 rows, remove first one [0]
if (data.cache.length>4){
data.removeRow(0);
// just to see range and how is removing first element
var dateRange = data.getColumnRange(0);
console.log(dateRange);
}
Finnish fiddle

Related

Can we input different date inputs while running the Automation script each time

I am using protractor 5.2.2. We have a requirement of creating a module with unique date so that i cannot create a module with already used date.So when i am running the script , i have to pass different date each time.How we can choose random date in automation.Thanks in advance.
I recommend using chancejs.
var Chance = require('chance'),
chance = new Chance();
console.log(chance.integer({ min: -2, max: 2 }));
would return either -2, -1, 0, 1, or 2.
Please take a look at the chancejs homepage http://chancejs.com/
Below example gives a data between these two years
var Chance = require('chance');
var chance = new Chance();
let bounds = {
min: chance.date({ year: 1983 }),
max: chance.date({ year: 1989 })
}
let date = chance.date(bounds)
console.log(date);
I got 1987-01-21T19:31:32.851Z
1.Random dates in JAVA
Generate random date of birth
if you are using the Excel as data provider
Use the Excel formula's like
=today();
=now();
if you are using java or other languages
use
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyyHH:mm:ss");
Date date = new Date();
C#
https://stackoverflow.com/questions/6817266/get-current-date-only-in-c-sharp
MomentJS is a good option that allows you to set time easily off the current time.
Examples:
moment.format('MM/DD/YYYY'); //gives you current date in mm/dd/yyyy format
moment.format('MM-DD-YYYY'); //current date in mm-dd-yyyy format
moment.add('5','days').format('MM/DD/YYYY'); gives you date 5 days from now

How to format axis real time amcharts hh:mm?

I use amchart, I want to create a real time chart, and set minDate, maxDate for x axis, and format it to hh:mm. I tried use minimumDate, maximumDate, "minPeriod" : "hh", but it fail.
My code:
demo1.
I want to use amchart to build a real time chart like: demo2 (use flot chart).
The labels xaxis is static not run, and to accumulate data.
Help me, please!
Thank you!
When updating the time, you need to set a new date instance or a separate value. Updating the startDate variable updates all the data points that share that date object as AmCharts doesn't clone the date objects in your dataProvider. A quick fix is to use the millisecond timestamp result from the setMinutes call, for example:
var newDate = startDate.setMinutes(startDate.getMinutes() + 10);
var visits = randomIntFromInterval(50, 100);
chartData.push({
date: newDate,
visits: visits
});
AmCharts will internally convert the millisecond values to a new Date object. This should be applied to your generate and update methods.
minPeriod should be set to the minimum period between your datapoints. As you're adding data in 10 minute increments, this should be "mm", not "hh".
By default, the categoryAxis does not support setting a minimumDate and maximumDate, however, you can use AmCharts' datePadding plugin to add this functionality. This plugin will allow you to set a minimumDate and maximumDate property in your categoryAxis when you add the following script tag after your AmCharts includes:
<script src="//www.amcharts.com/lib/3/plugins/tools/datePadding/datePadding.min.js"></script>
In order to maintain your date range after updating the chart, you have to call the plugin's AmCharts.datePaddingProcess method to re-update the range before redrawing the chart.
Here's what your updateChart method will look like after using the datePadding plugin:
function updateChart() {
var newDate = startDate.setMinutes(startDate.getMinutes() + 10);
var visits = randomIntFromInterval(50, 100);
chart.dataProvider.push({
date:newDate,
visits:visits
});
AmCharts.datePaddingProcess(chart, true);
chart.validateData();
}
And here's what your categoryAxis will look like:
"categoryAxis": {
"parseDates": true,
"gridAlpha": 0.15,
"axisColor": "#DADADA",
"minPeriod" : "mm",
"minimumDate": min,
"maximumDate": max
},
Updated fiddle

How to handle with date range in mathematica?

In google trends there is possibility to export data as CSV. Obtained CSV has the following structure:
Week,subject 1, subject 2
2004-01-04 - 2004-01-10,13,6
2004-01-11 - 2004-01-17,9,9
2004-01-18 - 2004-01-24,11,4
I know that there is DateObject[], but it contain only one date. I want to obtain stepped chart of subjects 1 and 2 in time domain, and calculate correlation of them in range between two given dates.
My problem is: how structure of data, should I use to represent time range?
As google trends calls the time variable "week" take
StringTake["2004-01-04 - 2004-01-10", 10]
to get the first day of the range, then use
DateList[{"2004-01-04", {"Year", "Month", "Day"}}]
to create a date list and
DateString[{2004, 1, 4, 0, 0, 0}, {"Week"}]
to express the time in terms of the calendar week of the year. So, the function
RangeToWeek[timerangestring_] := DateString[ DateList[{
StringTake[timerangestring, 10],
{"Year", "Month", "Day"}}], {"Week"}]
gives for the firs date in your list 01, because the time span from 04.01.2004 to 10.01.2004 corresponds to the first callendar week of that year.

Adding days to a date using std.datetime

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
}

date difference and match with value in javascript

Hi I have the Drop down with values as "One year ", "Two year",...etc.. Ok? also i have two ajax textbox with calender extender . I want to popup alert message if dropdown selected value is "One year" and duration between the both textbox value Means dates not matches. getting what i mean ? please help me.
How can i get this scenario in javascript ??
Algorithm :
1.Get the both date from the text box.
2. The find the epcoch time for each date. //
3. subtract the both dates.
4. subtract_result = 365*24*60*60 // Finding the 1 year timestamp values
5. So, it the difference exceed than above calculation , you could sure that the date is mis matching.
Javascript:
// This is for first date
first = new Date(2010, 03, 08, 15, 30, 10); // Get the first date epoch object
document.write((first.getTime())/1000); // get the actual epoch values
second = new Date(2012, 03, 08, 15, 30, 10); // Get the first date epoch object
document.write((second.getTime())/1000); // get the actual epoch values
diff= second - first ;
one_day_epoch = 24*60*60 ; // calculating one epoch
if ( diff/ one_day_epoch > 365 ) // check , is it exceei
{
alert( 'date is exceeding one year');
}