Skip to content

Commit

Permalink
Merge branch '#178-graph-resizing' into #172-charging-screen-improvem…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
bracyw committed Sep 14, 2024
2 parents 470e6b0 + f0cd163 commit e54a09b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 39 deletions.
68 changes: 38 additions & 30 deletions angular-client/src/components/graph/graph.component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { Component, Input, OnInit } from '@angular/core';
import * as ApexCharts from 'apexcharts';
import {
ApexAxisChartSeries,
ApexXAxis,
ApexDataLabels,
ApexChart,
ApexMarkers,
ApexGrid,
ApexTooltip,
ApexFill
} from 'ng-apexcharts';
import { ApexXAxis, ApexDataLabels, ApexChart, ApexMarkers, ApexGrid, ApexTooltip, ApexFill } from 'ng-apexcharts';
import { DialogService } from 'primeng/dynamicdialog';
import { GraphDialog } from '../graph-dialog/graph-dialog.component';
import { GraphData } from 'src/utils/types.utils';

type ChartOptions = {
series: ApexAxisChartSeries;
chart: ApexChart;
xaxis: ApexXAxis;
yaxis: ApexYAxis;
Expand All @@ -38,14 +28,12 @@ export class GraphComponent implements OnInit {
@Input() color!: string; // Must be hex
@Input() title?: string;
@Input() graphContainerId!: string;
@Input({ required: false }) timeRangeSec!: number;
options!: ChartOptions;
chart!: ApexCharts;
series: ApexAxisChartSeries = [
{
name: this.title,
data: []
}
];
timeDiffMs: number = 0;
isSliding: boolean = false;
timeRangeMs: number = 120000; // 2 minutes in ms

constructor(public dialogService: DialogService) {}

Expand All @@ -61,23 +49,37 @@ export class GraphComponent implements OnInit {
};

updateChart = () => {
this.series[0].data = this.data;
this.chart.updateSeries(this.series);
this.chart.updateSeries([
{
name: 'Data Series',
data: Array.from(this.data)
}
]);

if (!this.isSliding && this.data.length > 2) {
this.timeDiffMs = this.data[this.data.length - 1].x - this.data[0].x;
}

if (!this.isSliding && this.timeDiffMs > this.timeRangeMs) {
this.isSliding = true;
this.chart.updateOptions({
...this.options,
xaxis: {
...this.options.xaxis,
range: this.timeRangeMs
}
});
}

setTimeout(() => {
this.updateChart();
}, 1000);
}, 800);
};

ngOnInit(): void {
this.series = [
{
name: this.title,
data: this.data
}
];
this.timeRangeMs = (this.timeRangeSec ?? 120) * 1000;

this.options = {
series: [],
chart: {
id: 'graph',
type: 'line',
Expand Down Expand Up @@ -110,14 +112,20 @@ export class GraphComponent implements OnInit {
xaxis: {
type: 'category',
tickAmount: 2,
range: 120000,
labels: {
show: true,
style: {
colors: '#FFFFFF'
},
formatter: (value) => {
return '' + new Date(value).getHours() + ':' + new Date(value).getMinutes() + ':' + new Date(value).getSeconds();
return (
'' +
new Date(value).getHours() +
':' +
((new Date(value).getMinutes() < 10 ? '0' : '') + new Date(value).getMinutes()) +
':' +
((new Date(value).getSeconds() < 10 ? '0' : '') + new Date(value).getSeconds())
);
}
},
axisBorder: {
Expand Down Expand Up @@ -164,7 +172,7 @@ export class GraphComponent implements OnInit {
return;
}

this.chart = new ApexCharts(chartContainer, this.options);
this.chart = new ApexCharts(chartContainer, { series: [{ data: [] }], ...this.options });

this.chart.render();
this.updateChart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class GraphPage implements OnInit {

this.setSelectedDataType = (dataType: DataType) => {
this.selectedDataType.next(dataType);
this.selectedDataTypeValuesSubject.next([]);
this.selectedDataTypeValuesSubject = new BehaviorSubject<GraphData[]>([]);
if (this.realTime) {
if (this.subscription) this.subscription.unsubscribe();
const key = dataType.name;
Expand Down
35 changes: 27 additions & 8 deletions angular-client/src/pages/graph-page/graph/graph.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import * as ApexCharts from 'apexcharts';
import { ApexXAxis, ApexDataLabels, ApexChart, ApexMarkers, ApexGrid, ApexTooltip, ApexFill } from 'ng-apexcharts';
import { BehaviorSubject } from 'rxjs';
import { convertUTCtoLocal } from 'src/utils/pipes.utils';
import { GraphData } from 'src/utils/types.utils';

type ChartOptions = {
Expand Down Expand Up @@ -30,6 +29,7 @@ export default class Graph implements OnChanges {
data!: Map<number, number>;
timeDiffMs: number = 0;
isSliding: boolean = false;
timeRangeMs = 120000;

updateChart = () => {
if (this.previousDataLength !== Array.from(this.data).length) {
Expand All @@ -41,13 +41,13 @@ export default class Graph implements OnChanges {
}
]);

if (!this.isSliding && this.timeDiffMs > 120000) {
if (!this.isSliding && this.timeDiffMs > this.timeRangeMs) {
this.isSliding = true;
this.chart.updateOptions({
...this.options,
xaxis: {
...this.options.xaxis,
range: 120000
range: this.timeRangeMs
}
});
}
Expand All @@ -61,8 +61,8 @@ export default class Graph implements OnChanges {
this.data = new Map();
this.valuesSubject.subscribe((values: GraphData[]) => {
values.forEach((value) => {
if (!this.data.has(convertUTCtoLocal(value.x))) {
this.data.set(convertUTCtoLocal(value.x), +value.y.toFixed(3));
if (!this.data.has(value.x)) {
this.data.set(value.x, +value.y.toFixed(3));
}
});

Expand Down Expand Up @@ -104,11 +104,21 @@ export default class Graph implements OnChanges {
size: 0
},
xaxis: {
type: 'datetime',
type: 'category',
tickAmount: 6,
labels: {
style: {
colors: '#fff'
},
formatter: (value) => {
return (
'' +
new Date(value).getHours() +
':' +
((new Date(value).getMinutes() < 10 ? '0' : '') + new Date(value).getMinutes()) +
':' +
((new Date(value).getSeconds() < 10 ? '0' : '') + new Date(value).getSeconds())
);
}
}
},
Expand Down Expand Up @@ -152,10 +162,19 @@ export default class Graph implements OnChanges {
this.data = new Map();
this.isSliding = false;

//set range to undefined
this.chart.updateOptions({
...this.options,
xaxis: {
...this.options.xaxis,
range: undefined
}
});

this.valuesSubject.subscribe((values: GraphData[]) => {
values.forEach((value) => {
if (!this.data.has(convertUTCtoLocal(value.x))) {
this.data.set(convertUTCtoLocal(value.x), +value.y.toFixed(3));
if (!this.data.has(value.x)) {
this.data.set(value.x, +value.y.toFixed(3));
}
});

Expand Down

0 comments on commit e54a09b

Please sign in to comment.