Merge pull request #13198 from codescientist703/realtime-chart

feat: Added real time chart javascript API
This commit is contained in:
mergify[bot] 2021-06-22 06:21:26 +00:00 committed by GitHub
commit 916ec171fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
};
}
};