Merge pull request #13198 from codescientist703/realtime-chart
feat: Added real time chart javascript API
This commit is contained in:
commit
916ec171fa
1 changed files with 36 additions and 0 deletions
|
|
@ -1,3 +1,39 @@
|
|||
import { Chart } from "frappe-charts/dist/frappe-charts.esm";
|
||||
|
||||
frappe.provide("frappe.ui");
|
||||
frappe.Chart = Chart;
|
||||
|
||||
frappe.ui.RealtimeChart = class RealtimeChart extends frappe.Chart {
|
||||
constructor(element, socketEvent, maxLabelPoints = 8, data) {
|
||||
super(element, data);
|
||||
if (data.data.datasets[0].values.length > maxLabelPoints) {
|
||||
frappe.throw(
|
||||
__(
|
||||
"Length of passed data array is greater than value of maximum allowed label points!"
|
||||
)
|
||||
);
|
||||
}
|
||||
this.currentSize = data.data.datasets[0].values.length;
|
||||
this.socketEvent = socketEvent;
|
||||
this.maxLabelPoints = maxLabelPoints;
|
||||
|
||||
this.start_updating = function() {
|
||||
frappe.realtime.on(this.socketEvent, data => {
|
||||
this.update_chart(data.label, data.points);
|
||||
});
|
||||
};
|
||||
|
||||
this.stop_updating = function() {
|
||||
frappe.realtime.off(this.socketEvent);
|
||||
};
|
||||
|
||||
this.update_chart = function(label, data) {
|
||||
if (this.currentSize >= this.maxLabelPoints) {
|
||||
this.removeDataPoint(0);
|
||||
} else {
|
||||
this.currentSize++;
|
||||
}
|
||||
this.addDataPoint(label, data);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue