Preprocessing sensor data with a time and value as x and y in Highcharts. Will an array within an array work? - highcharts

I would like to create a Highcharts line graph with temperature data from multiple sensors each posting results at different times. The data is stored in a MySQL database as id, location, temp, timestamp. Since each sensor will be generating data at unique times the x values will not line up. For this reason I am interested in supplying each Highcharts series with a x- value and y-value... something like:
series: [{ // Location 1
name: 'Main Bedroom',
data: time1, temperature1
I am starting with a single location (MainBDRm) to develop my code.
I am using the following query to get and store multiple rows of data.
$sql = "SELECT temp, reading_time FROM ASHP_SensorData WHERE location = 'MainBDRm' ORDER BY reading_time DESC LIMIT 5";
while ($data = $result->fetch_assoc()){
$sensor_data[] = $data;
{
$temperature1 = json_encode(array_reverse(array_column($sensor_data, 'temp')), JSON_NUMERIC_CHECK);
$time1 = json_encode(array_reverse(array_column($sensor_data, 'reading_time')), JSON_NUMERIC_CHECK);
echo $temperature1 yields an array as [76,75,73,71,70]
echo $time1 yields an array as ["2021-12-19 17:07:13","2021-12-19 17:07:43","2021-12-19 17:08:13","2021-12-19 17:08:44","2021-12-19 17:09:14"]
I have confirmed that I can successfully produce a chart with
series: [{ // Location 1
name: 'Main Bedroom',
//yAxis: 0,
//showInLegend: true,
//data: temperature1, // works
data:[ // this works too
["2021-12-19 17:07:13", 76],
["2021-12-19 17:07:43", 75],
["2021-12-19 17:08:13", 73],
["2021-12-19 17:09:14", 71],
],
I would like to know if it is possible to use Json to preprocess the query results as a array in an array and use that as an input for the data: element with both the x & y values. Something like: ["2021-12-19 17:07:13", 76], ... , ["2021-12-19 17:09:14", 70]
I have tried
$stringToReturn = array();
$result = $conn->query($sql);
while ($data = $result->fetch_assoc()){
$sensor_data[] = $data;
array_push($stringToReturn, $data);
}
echo json_encode($stringToReturn, $data);
which yields:
[{"reading_time":"2021-12-19 22:24:33","temp":"79.70"},{"reading_time":"2021-12-19 22:24:03","temp":"79.70"},{"reading_time":"2021-12-19 22:23:33","temp":"79.70"},{"reading_time":"2021-12-19 22:23:03","temp":"79.70"},{"reading_time":"2021-12-19 22:22:33","temp":"79.70"}]
but I am not sure how to use this in the Highcharts script, or if it is in a usable form.

Notice that Highcharts expects to get x and y values in the data array, not custom names as "reading_time" or "temp", so you need to parse your data into the required format.
Demo: https://jsfiddle.net/BlackLabel/oe3j9fcp/
Or you can do it a few steps earlier: https://jsfiddle.net/BlackLabel/pd32q7zo/
API: https://api.highcharts.com/highcharts/series.line.data

Related

Highcharts data module: filtering specific columns in referenced HTML table

Full example:
https://jsfiddle.net/gbeatty/4byg0p2t/
data: {
table: 'datatable',
startRow: 0,
endRow: 6,
startColumn: 0,
endColumn: 3,
parsed: function (columns) {
columns.forEach(column => {
column.splice(1, 2);
});
}
},
What I'd like the chart to reference is only column 0 "Year" and column 3 "Group C" while keeping the entire table displayed below. Challenge is disregarding the 2 columns in the middle.
I am trying the parsed option but it seems the rows and columns are mixed up. I even tried setting the switchRowsAndColumns value to true. (https://api.highcharts.com/highcharts/data.seriesMapping)
You can also use complete function to modify your data.
Example code based on your config:
complete: function(options) {
let series = [];
series.push(options.series[2]);
options.series = series;
}
Demo:
https://jsfiddle.net/BlackLabel/tfs4ubcL/
API Reference:
https://api.highcharts.com/highcharts/data.complete

Google Docs API - adjust width of table column

I am able to create a table using the Google Docs Api. By default, each column is of equal width.
How is it possible to adjust the width of the columns using the Google Docs API?
I can see the updateTextStyle and updateParagraphStyle request types (batchUpdate methods) but there's currently no updateTable method, or any similar method that looks like it could do this. I'm using the reference documentation, here:
https://developers.google.com/docs/api/reference/rest/v1/documents/request
I'm using NodeJS / Javascript to interact with the API currently.
This is possible, in fact all the table operation that you can do on UI are possible.
1) Create table with following request:
def create_loc_table(doc_id, r, c, idx):
"""
r = number of rows,
c = number of columns,
idx = Start index where table needs to be created.
"""
request = [{
'insertTable': {
'rows': r,
'columns': c,
'location': {
'segmentId':'',
'index': idx
}
},
}
]
result = self.docs_service.documents().batchUpdate(documentId=doc_id, body={'requests': request}).execute()
print('Result {0}'.format(json.dumps(result, indent=4)))
2) Modify/update the table:
def modify_table(self, t_idx):
"""
t_idx = Table start index.
"""
request = [{
'updateTableColumnProperties': {
'tableStartLocation': {'index': t_idx},
'columnIndices': [0],
'tableColumnProperties': {
'widthType': 'FIXED_WIDTH',
'width': {
'magnitude': 100,
'unit': 'PT'
}
},
'fields': '*'
}
}
]
result = self.docs_service.documents().batchUpdate(documentId=doc_id, body={'requests': request}).execute()
print('Result {0}'.format(json.dumps(result, indent=4)))
3) Output would be

Exporting Multiple HTML tables to excel such that each HTML table is in a new column

I'm trying to use alasql to export a set of HTML tables into an excel document.
The documentation has code that looks similar to this:
var data1 = alasql('SELECT * FROM HTML("#dev-table",{headers:false})');
var data2 = alasql('SELECT * FROM HTML("#dev2-table",{headers:false})');
var data3 = alasql('SELECT * FROM HTML("#dev3-table",{headers:false})');
//var data4 = alasql('SELECT * FROM HTML("#dev2-table",{headers:true})');
var data = data1.concat(data2, data3);
alasql('SELECT * INTO XLS("data.xls",{headers:false}) FROM ?', [data]);
The problem is that this code concatenates the data1 and data2 field so that all of the data is printed in the same column. This is not the result I desire. I want "data1" to go into column "A" and data2 to go into column "B".
I've looked through the documentation and am unsure how to get the desired result. I'm aware of the existence of "options" that include fields for specifying columns based on the data itself, but none of those examples are what I want. If this is not possible using alasql, I'm willing to use a different library or framework for this.
Based on this JSFiddle, http://jsfiddle.net/95j0txwx/7/
$scope.items = [{
name: "John Smith",
email: "j.smith#example.com",
dob: "1985-10-10"
}, {
name: "Jane Smith",
email: "jane.smith#example.com",
dob: "1988-12-22"
},
...
I would guess that your data is not formatted correctly to be inserted.
EDIT: JSFiddle is from documentation. https://github.com/agershun/alasql/wiki/XLSX

Is it possible to change the name of "x" for x data?

I receive data which I would like to chart. Unfortunately, it consist of records like
{
abcisse: 5,
ordonnee: 9
}
which I would like to feed into data.
The record above corresponds, in highcharts nomenclature, to
{
x: 5,
y: 9
}
Is it possible to inform highcharts that he should be looking for values of x in abcisse and for values of y in ordonnee? Or do I have to postprocess the data and create x and y entries?
You can use an array map function to convert your records to the format need my highcharts. For example, if your data is in an array called myData then you could feed your data to the series.data option using...
$('#container').highcharts({
....
series: [{
data: myData.map(function (item) {return { x: item.abcisse, y: item.ordonnee}; })
}]
...
});

highchart series push from ajax

ive try to graph date and a value, this is my code:
function getDaily(vsatid){
$.ajax({
type: "POST",
url: "daily_graph.php",
data: "id=" + vsatid,
success: function(obj1){
options.series[0].data = obj1.date;
options.series[1].data = obj1.ebno;
}
var chart = new Highcharts.Chart(options);
});}
can anyone tell me how i suppose to push the series so i can get x as datetime and y as value.
below is my data parsed from mysql database to json by using json_encode.
[{"date":"1363700566","ebno":8.04},{"date":"1363701638","ebno":8.02},{"date":"1363705226","ebno":7.93},{"date":"1363709087","ebno":7.65},{"date":"1363712661","ebno":7.69},{"date":"1363716221","ebno":7.44},{"date":"1363719708","ebno":7.19},{"date":"1363723254","ebno":6.97},{"date":"1363726853","ebno":6.99},{"date":"1363730481","ebno":7.04},{"date":"1363734045","ebno":6.92},{"date":"1363737697","ebno":7.07},{"date":"1363741201","ebno":7.27},{"date":"1363744878","ebno":7.35},{"date":"1363748625","ebno":7.48},{"date":"1363752211","ebno":0},{"date":"1363755741","ebno":7.69},{"date":"1363759347","ebno":7.76},{"date":"1363762894","ebno":7.83},{"date":"1363766640","ebno":7.82},{"date":"1363770121","ebno":7.82},{"date":"1363773789","ebno":7.69},{"date":"1363777209","ebno":6.78},{"date":"1363780874","ebno":8.15}]
all answer will appreciate..
regards
mahrus
You dates should be timestamp, so you should use parseFloat() functon to transorm string to number, then multiply by 1000 to achieve appropriate javascript timestamp format.
Each datapoint should be defined as:
Var mydata = [[1363700566000,8.04], [1363701638,8.02] ]
Alternatively, you can specify more point options using this form:
Var my data = [{x:1363700566000,y:8.04}, {x:1363701638,y:8.02} ]
Note, dates are specified in milliseconds, so you need to multiply yours by 1000.
Options.series[0].data = mydata
try
chart.series[0].addPoint(obj1.date);
chart.series[1].addpoint(obj1.ebno)
// options.series[0].data = obj1.date;
// options.series[1].data = obj1.ebno;
by using two series u will get 2 diff x values for two diff series
if u want to get x,y val for same series
parse the obj1 to json . or
add
type:"GET",
url: "delete",
// dataType:"json",
data:chart.series[0].data,
try
$.each(obj1,function(index,object){
chart.series[0].addPoint(object);
});
// will iterate all the objects and add it to series
where obj1 should contain
[{"x":"1363700566","y":8.04}

Resources