Static chart with timestamp on x-axis - zingchart

I want to create a static chart of values pulled out of a MySQL database.
The chart format would be (x axis : dd/mm/yy hh:mm:ss (corresponding to timestamp of mysql database)) and y-axis would be a double value. I am able to successfully retrieve these values from MySql database.I want help plotting them by ZingChart

Nikita.
Once you've retrieved your values from your MySQL database, you'll want to convert the MySQL date values in to Unix time in milliseconds. I've populated a $date array with the MySQL date values, and iterated over the array, calling strtotime to first convert to Unix time, and multiplying by 1000 to convert to milliseconds. In order to be able to directly modify array elements within the loop, I've also preceded $value with to assign by reference.
foreach ($date as &$value){
$value = strtotime( $value ) * 1000;
}
So now that the values in the $date array have been converted to the proper format, it's time to create a JavaScript array from the PHP array. This can be done using join():
var dateValues = [<?php echo join($date, ',') ?>];
The resulting array looks like this:
var dateValues = [1356994800000,1357081200000,1357167600000, ... ];
To use this array in ZingChart, use the dateValues variable with "values" in the scale-x object. To convert the Unix time values back to dates in ZingChart, add the "transform" object, and set it to "type":"date".
"scale-x":{
"values": dateValues,
"transform":{
"type":"date",
"item":{
"visible":false
}
}
},
...
That takes care of the scale. To get your other values in the chart, you do pretty much the same thing. Convert the PHP arrays into JavaScript arrays, and use the array variable in your chart JSON.
With the PHP $series array:
var seriesValues = [<?php echo join($series, ',') ?>];
In your chart JSON:
"series":[
{
"values":seriesValues
}
]
I've compiled all of this in to a Github Gist for you. Let me know if you have any questions!

Check out our demos repo on GitHub. We have a tutorial specifically about connecting to a MySQL database with PHP.
There's a step-by-step walkthrough on our site, too.
If you share your JSON or more details about it, I can help you with putting your chart together.
I'm on the ZingChart team. Please let me know if you have other questions.

Related

Trying to delete the date within a range if it matches a variable in Office scripts for Excel

I have a column with dates stored that need to be cleared if they match a variable.
I've tried a ton of different ways, but this is my most recent attempt:
let dateRange = selectedTable
.getColumnByName("Date")
.getRangeBetweenHeaderAndTotal()
.getTexts();
let date: string = "12/2/2022"
console.log(dateRange);
dateRange.forEach(dates => {
if (dates === date){
ExcelScript.ClearApplyTo.contents
}
})
This one won't work as 'dates' is an array and can't be compared to the 'date' variable as far as I can tell.
I think there are two issues you might be running into here:
First: getTexts() returns a 2D array to preserve the row/column structure of the grid. So even though dateRange is a column, it's still a 2D array - something like [['value1'], ['value2'], ...]. You can get a single cell with the expression dateRange[rowIndex][0].
Second: ExcelScript.ClearApplyTo.contents is simply an enum member and does not do anything on its own. To clear the contents of a specific cell/range, you need to call the clear() method on the corresponding Range object.
Putting this together, you get the following script (assuming you've defined selectedTable elsewhere):
let dateRange = selectedTable
.getColumnByName("Date")
.getRangeBetweenHeaderAndTotal();
let texts = dateRange.getTexts();
let date: string = "12/2/2022"
texts.forEach((text, row) => {
if (text[0] === date) {
dateRange.getCell(row, 0).clear();
}
})
Additionally, as pointed out in the comments, you should be careful about date formatting. Since you're comparing strings, this script will fail to clear cells that contain 12/02/2022, December 2, 2022, etc. even though the underlying date is the same.
Hopefully that helps!

Reformatting date values when using them as URL parameters in a PowerQuery API request

I have two dates in my Excel table with the following format: "dd-mm-yyyy". These dates need to be sent as URL query parameters to an API endpoint for getting some data using PowerQuery. However, the API endpoint does not accept dates in that format. Therefore, I need to convert them to the format "mm-dd-yyyy" instead for it to work.
For getting the values from my table, I use the following code:
let GetNamedRange=(NamedRange) =>
let
name = Excel.CurrentWorkbook(){[Name=NamedRange]}[Content],
value = name{0}[Column1]
in
value
in
GetNamedRange
This function, called "GetValue", is then called when inserting URL query parameters in my GET request:
Csv.Document(Web.Contents("my.api/leave/leavecsv", [Query = [periodStart = GetValue("periodStart"), periodEnd = GetValue("periodEnd"), department = GetValue("department")]]),[Delimiter=";", Columns=14, Encoding=1252, QuoteStyle=QuoteStyle.None])
Currently the cells for my dates are in Text format. I tried using Date.FromText(...) to format the dates, but I get an error saying the datetime format is invalid.
https://learn.microsoft.com/en-us/powerquery-m/date-fromtext
How can I propertly format my date values before inserting them as URL query parameters using PowerQuery?
Ensure your dates are real dates and set to type date. then you can use the Date.ToText function:
let
theDate = #date(2022,12,7),
output = Date.ToText(theDate,"MM-dd-yyyy")
in
output
If, for some reason, you must maintain your dates as text strings (I'd like to know why, if that's the case), you can convert them first to a "real" date, and then create the string:
let
theDate = "13-12-2022",
output = Date.ToText(Date.FromText(theDate, "en-150"),"MM-dd-yyyy")
in
output
Make sure you pass in a culture and format. i.e.
Date.FromText([Column1], [Format="dd-MM-yyyy", Culture="en-UK"])

Nodejs - Read values of array as number instead of string

I created an array inside my own class in nodejs to store real values.
this.values = new Array();
and almost every second a new value is pushed to this array.
this.values.push(Number(dataset.c).toFixed(4));
Finaly I have a function where I need to provide the array in the argument list to do bollinger band calculation. This function need an array filled with real or integer values but in my array every value is stored as an string.
Is it possible to change this storage behaviour or how can I 'typecast' all the values in this array?
Best regards
Found a solution using the map-Function.
this.values.map(Number)
did the job.

How to convert a string array into floats in PHP

I'm selecting some Strings from my SQLite DB and store them in an Array. Now I want to plot the values with the framework "Razorflow". I think it is only possible if the values of the array are floats, am I wrong?
In my DB I'm storing temperature and humidity of 12 different sensor nodes, in this form:
id|temperature|humidity|...|...
1 | 22.50C| 47.50%|...|...
..
I heared something about the floatval()-function, I also heared that this function is not made for objects like Arrays. Is there a simple solution? I'm very new to PHP :-D
Depends on how you're getting the values back from the database.
id|temperature|humidity|...|...
Sounds like a string to me, so i would first explode it into an array and then iterate the array casting floatval into every element.
What you do after that process depends on you.
EDIT:
If your query is:
$humi = $database->query((SELECT humidity FROM Measurement WHERE topic_hum='WSN9/humi'
You will get back only 1 column (humidity) and 0 or more rows depending on your database. Let's say it's only 1 for now:
$resul = mysql_query($humi,$link);
$rows = mysql_fetch_array($resul);
$myarray = explode("|", $rows["humidity"]);
This should give us an array called myarray containing X elements each with a "single" string part value. So we can iterate over it and parse it. There is also the shorthand "array_map" to iterate over an array using a callback function over each element and returning the value as an array:
$myparsedarray = array_map('floatval', $myarray)
Now you have an array with only float values (and errors maybe, check your data!)

Mongoose - Search dates stored as strings by date range

I have a mongo db with a paymentDate field stored as strings YYYY-MM-DD (I can't change how these are unfortunately), and I need to do a date range search on them.
I found a post suggesting something like this:
coll.find({paymentDate: new RegExp("^(2016-02-18|2016-06-19)", "i")});
But I can't seem to get that to work at all.
Any help is appreciated, I've hit a dead end here.
Storing dates as strings in YYYY-MM-DD format works fine in this case as the string ordering matches the date ordering so you can do:
coll.find({paymentDate: {$gte: "2016-02-18", $lte: "2016-06-19"}});
Your first objective should be changing the schema and use Date instead of String. But if it's really not possible and you can't think of a really complicated RegEx, you can do it with Javascript using $where, but it's performance will be much slower:
db.test.find({
$where: function () {
var docDate = new Date(this.paymentDate);
return (docDate >= new Date('2016-02-18') && docDate <= new Date('2016-02-19'))
}
})

Resources