Skip to content

Commit

Permalink
try two data ars
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Sep 6, 2024
1 parent d8c90dd commit 02d2d9b
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/driver/drv_test_charts.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
// startDriver TestCharts
void DRV_Test_Charts_AddToHtmlPage(http_request_t *request) {
time_t timeStamps[16];
float samples[16];
float temperature[16];
float humidity[16];
char buffer[64];

for (size_t i = 0; i < 16; i++) {
timeStamps[i] = 1725606094 + i * 60;
samples[i] = 20.0 + (i % 5);
temperature[i] = 20.0 + (i % 5);
humidity[i] = 80.0 + (i % 3);
}
poststr(request, "<canvas id=\"myChart\" width=\"400\" height=\"200\"></canvas>");
poststr(request, "<script src=\"https://cdn.jsdelivr.net/npm/chart.js\"></script>");
Expand All @@ -29,7 +31,6 @@ void DRV_Test_Charts_AddToHtmlPage(http_request_t *request) {
poststr(request, "}");
poststr(request, "var ctx = document.getElementById('myChart').getContext('2d');");

// Pre-format the labels manually using JavaScript's Date object
poststr(request, "var labels = [");
for (size_t i = 0; i < 16; i++) {
snprintf(buffer, sizeof(buffer), "new Date(%ld * 1000).toLocaleTimeString()", (long)(timeStamps[i]));
Expand All @@ -43,26 +44,41 @@ void DRV_Test_Charts_AddToHtmlPage(http_request_t *request) {
poststr(request, "window.myChartInstance = new Chart(ctx, {");
poststr(request, " type: 'line',");
poststr(request, " data: {");
poststr(request, " labels: labels,"); // Use the pre-formatted labels
poststr(request, " datasets: [{");
poststr(request, " label: 'Temperature',");
poststr(request, " data: [");
poststr(request, " labels: labels,");
poststr(request, " datasets: [");
poststr(request, " {");
poststr(request, " label: 'Temperature',");
poststr(request, " data: [");
for (size_t i = 0; i < 16; i++) {
snprintf(buffer, sizeof(buffer), "%.2f", samples[i]);
snprintf(buffer, sizeof(buffer), "%.2f", temperature[i]);
poststr(request, buffer);
if (i < 15) {
poststr(request, ",");
}
}
poststr(request, "],");
poststr(request, " borderColor: 'rgba(75, 192, 192, 1)',");
poststr(request, " borderColor: 'rgba(75, 192, 192, 1)',");
poststr(request, " fill: false");
poststr(request, " },");
poststr(request, " {");
poststr(request, " label: 'Humidity',");
poststr(request, " data: [");
for (size_t i = 0; i < 16; i++) {
snprintf(buffer, sizeof(buffer), "%.2f", humidity[i]);
poststr(request, buffer);
if (i < 15) {
poststr(request, ",");
}
}
poststr(request, "],");
poststr(request, " borderColor: 'rgba(232, 122, 432, 1)',");
poststr(request, " fill: false");
poststr(request, " }]");
poststr(request, " },");
poststr(request, " options: {");
poststr(request, " scales: {");
poststr(request, " x: {");
poststr(request, " type: 'category',"); // Use category scale instead of time scale
poststr(request, " type: 'category',");
poststr(request, " },");
poststr(request, " y: {");
poststr(request, " beginAtZero: false");
Expand Down

0 comments on commit 02d2d9b

Please sign in to comment.