How to format axis real time amcharts hh:mm? - real-time

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

Related

Problem with understanding date formats in googleScripts

I made a few functions with GoogleSheets using AppsScripts for simple task a few times in previous years. I always had problems when taking dates from cells/ranges and processing them, but somehow alwaays found a workaround, so that I did not have to deal with it. Well, this time I can not find a workaround, so I will try to explain my problems with the following code:
function getDates(){
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Dates');
var date = sht.getRange(2,1).getValues();
Logger.log(date[0][0]); //output is Tue Jun 08 18:00:00 GMT-04:00 2021
var datumFilter= Utilities.formatDate(date[0][0], "GMT+1", "dd/mm/yy");
Logger.log(datumFilter); //output is 08/00/21
var outrng = sht.getRange(25,1);
outrng.setValue(date);
}
The first targeted cell ('var date') has a value of "9.6.21" in the spreadsheet. The cell is formatted as a date and it opens a calendar when double-clicked. When I set the new cells values (with 'outrng.setValue(date);'), the result is OK, with the same date as in the original cell.
But I do not need to simply transfer the values, I want to implement them in some loops and I have no idea how to simply get the date in the same format or at least the same date in the script as it is in the cell. As you can see from the logger, the values there are different. A simple d/m/yy format would be sufficient.
My spreadsheet settings are set to my local time (Slovenia, GMT+1).
I am guessing that I am missing some basics here. I have spent many hours trying to understand it, so any help is highly appreciated!
Cooper already answered all your questions in the comment. I'd like to add on and show you an example on what it would like and add some modifications.
Code:
function getDates() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Dates');
// get last row of the sheet
var lastRow = sht.getLastRow();
// get your sheet's timezone
var timezone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var output = [];
// getValues is mostly used for multiple cells returning a 2D array
// use getValue for single cells to return its actual value
// but since function name is getDates, I assume column A is all dates
// so we fetch the whole column (A2:A[lastRow]) except the header
var dates = sht.getRange("A2:A" + lastRow).getValues();
// for each date on that column, we format the date to d/M/yy
// m/mm = minute
// M/MM = month
dates.forEach(function ([date]){
Logger.log(date);
var datumFilter= Utilities.formatDate(new Date(date), timezone, "d/M/yy");
Logger.log(datumFilter);
// collect all dates in an array
output.push([datumFilter]);
});
// assign all the dates in the array onto range B2:B
sht.getRange(2, 2, output.length, 1).setValues(output);
}
Sample data:
Logs:
Output:
Note:
The output on sheets is not equal to logs due to the formatting of my sheet.

How to get corresponding price of the smallest date chosen in date slicer in Power BI

I am quite new to working with DAX and Power BI so please don't judge. My problem seems (and might be) simple. Anyways, here we go:
I have a dataset that contains 3 colulmns: Date (date), Price (float), Performance (%)
Attribute descriptions:
Date and Price are constants that are pulled from an external data source. Performance is a variable of the price change over time in percent. It is the percentage change of the price of the current date to the first date in the time-series selection (Selected "from date" of date slicer visual).
I want to create a dynamic line chart that shows performance over time. Difficulty here is when I change the "from date" I want the performance to be variable. Meaning, the price of the chosen "from date" is the new base price and should be calculated accordingly.
Formula:
Date = t, price at date t = pt, performance at date t = pert
Date range:
1.1.2000 to 31.12.2010
Initial situation when "date from" in the date slicer visual = 1.1.2000:
t0 = 1.1.2000
pt0 = 5,00
pert0 = 0%
t5 = 6.1.2000
pt5 = 5,054
pert5 = (pt5-pt0)/pt0 = 1.08%
After changing date slicer so that "from date" is now 10.10.2009:
t0new = 10.10.2009
pt0new = 9,938
pert0new = 0%
t5new = 15.10.2009
pt5new = 9,832
pert5new = (pt5-pt0)/pt0 = -1,05%
As described, I want whatever is selected as starting point from the date slicer as the new base value for the performance calculation and the line chart should adjust accordingly.
I know how to do the dynamic line chart but I cannot figure out the measures and calculated columns I need to do so.
Any help is very much appreciated!
Cheers,
MLU
Calculate the benchmark as the price associated to the first date in
the period. SELECTEDVALUE assumes you have one price per Date,
otherwise use an aggregator (e.g. MIN, MAX, AVERAGE). I use ALLSELECTED so the Benchmark is affected only by Filter Context (slicers) and you can easily use it in visualizations that change the context.
Save our benchmark in a variable for later use
Divide each price by the benchmark. Here we need to apply an aggregator to the Price,
I used AVERAGE assuming you have only one Price per day, therefore, the result is the
price itself.
Here is the measure:
Price vs Dynamic Benchmark :=
VAR vbenchmark = CALCULATE(SELECTEDVALUE(Dataset[Price]),FILTER(ALL( Dataset[Date]), Dataset[Date] = CALCULATE(min(Dataset[Date])), ALLSELECTED(Dataset))
return
AVERAGE(Price) / vbenchmark

React vis - Formatting time tick on the x axis

I have added the xType="time" to the chart to show the time scale on the x-axis. I am displaying data only for 25 seconds range. Currently, the x-axis is showing the time format as :SS.
So the x-axis shows time in the following format (data showing each second):
:23, :24, :25
What I am getting from the database is time string in the following format:
2019-07-01T10:42:38.621Z
I have tried the following:
new Date(item.date) // shows ':023'
new Date(item.date).getTime() // shows ':023'
new Date(item.date.substring(19, 0)).getTime() // shows ':023'
oscilloscope.oscilloscope.map((item, index) => {
return {
x: new Date(item.date.substring(19, 0)).getTime(),
y: item.data.fuelInjection
}
})
Always getting the same result.
I would like the x-axis to be formatted in HH:MM:SS format.
So the x-axis showing data like:
11:42:05, 11:42:06, 11:42:07
I am showing a range of 25 seconds apart. This seems to be set by the chart automatically as if I change the range to the extent that a couple of minutes are included the time display on x-axis changes to MM:SS format. I still need the HH:MM:SS format thou. Can this be done at all?
To answer my own question week later, I found an answer in different question about react-vis here on the Stack Overflow:
show date in( MM-DD) format in x-axis using react-vis
In my case the solution was:
<XAxis
tickFormat={function tickFormat(d){
const date = new Date(d)
return date.toISOString().substr(11, 8)
}}
/>
That did the job. Hope it will save someone else time.

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 can I show daily grid lines in HighStock? Currently seems buggy

I am using Highstock to create a GANTT style workflow, but I'm using the X-Range Highcharts plugin to allow for start and end dates on the X-axis. I am trying to get grid lines to display on every day, so I set the following on the X-axis:
tickInterval: 24 * 36e5
However, the grid lines only appear on every week. I've searched all over but can't seem to find a solution specific to this problem, any help would be greatly appreciated.
Here's a jsfiddle showing the problem.
Highcharts prevents from rendering too many ticks on the chart. Maybe the current logic is a bit harsh, but you can always use xAxis.tickPositioner, take a look:
tickPositioner: function() {
console.log(this.tickPositions);
var tp = this.tickPositions,
first = tp[0],
last = tp[tp.length - 1],
ticks = [],
interval = 24 * 36e5;
while (first <= last) {
ticks.push(first);
first += interval;
}
return ticks;
},
Demo: http://jsfiddle.net/LeL28nqr/6/
For a better readability, I suggest to change labels.step option - for example set to every 7th day: http://jsfiddle.net/LeL28nqr/7/