From 0b521272bf0c62a8049ec2ac03f8e97ba529c461 Mon Sep 17 00:00:00 2001 From: Travis Cox Date: Tue, 24 Oct 2023 15:08:39 -0700 Subject: [PATCH] Added new scripting functions, updated to 1.0.16 Added addPointAnnotation, clearAnnotations, and updateSeries. --- README.md | 26 +++++++++++ build.gradle | 2 +- .../gateway/ApexChartModelDelegate.java | 46 +++++++++++++++++++ .../src/main/resources/mounted/Components.js | 17 +++++-- .../typescript/components/ApexChart.tsx | 17 ++++--- 5 files changed, 96 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index de17a32..de12fb2 100644 --- a/README.md +++ b/README.md @@ -243,4 +243,30 @@ self.getSibling("apexchart").showSeries("SeriesA") This method allows you to hide the visible series. If the series is already hidden, this method doesn’t affect it. You can learn more about the toggleSeries function [here](https://apexcharts.com/docs/methods/#hideSeries). ``` self.getSibling("apexchart").hideSeries("SeriesA") +``` + +### `addPointAnnotation` +This method can be used to draw annotations after chart is rendered. You can learn more about the addPointAnnotation function [here](https://apexcharts.com/docs/methods/#addpointannotation). +``` +self.getSibling("apexchart").addPointAnnotation({ + "x": 1698098270040, + "y": 40, + "label": { + "text": "Lorem Ipsum" + }, +}, True) +``` + +### `clearAnnotations` +This method is used to delete all annotation elements which are added dynamically using the method stated above. You can learn more about the clearAnnotations function [here](https://apexcharts.com/docs/methods/#clearAnnotations). +``` +self.getSibling("apexchart").clearAnnotations() +``` + +### `updateSeries` +Allows you to update the series array overriding the existing one. If you want to append series to existing series. You can learn more about the updateSeries function [here](https://apexcharts.com/docs/methods/#updateSeries). +``` +self.getSibling("apexchart").updateSeries([{ + "data": [32, 44, 31, 41, 22] +}], True) ``` \ No newline at end of file diff --git a/build.gradle b/build.gradle index 03db70e..848df46 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ plugins { id "base" } -version "1.0.15" +version "1.0.16" group "com.kyvislabs" diff --git a/gateway/src/main/java/com/kyvislabs/apexcharts/gateway/ApexChartModelDelegate.java b/gateway/src/main/java/com/kyvislabs/apexcharts/gateway/ApexChartModelDelegate.java index fa611a8..df7e7a7 100644 --- a/gateway/src/main/java/com/kyvislabs/apexcharts/gateway/ApexChartModelDelegate.java +++ b/gateway/src/main/java/com/kyvislabs/apexcharts/gateway/ApexChartModelDelegate.java @@ -1,5 +1,6 @@ package com.kyvislabs.apexcharts.gateway; +import com.inductiveautomation.ignition.common.gson.Gson; import com.inductiveautomation.ignition.common.gson.JsonObject; import com.inductiveautomation.ignition.common.script.builtin.KeywordArgs; import com.inductiveautomation.ignition.common.script.builtin.PyArgumentMap; @@ -8,8 +9,11 @@ import com.inductiveautomation.perspective.gateway.api.ScriptCallable; import com.inductiveautomation.perspective.gateway.messages.EventFiredMsg; import org.python.core.Py; +import org.python.core.PyArray; +import org.python.core.PyDictionary; import org.python.core.PyObject; +import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; public class ApexChartModelDelegate extends ComponentModelDelegate { @@ -105,6 +109,48 @@ public void hideSeries(PyObject[] pyArgs, String[] keywords) throws Exception { fireEvent(OUTBOUND_EVENT_NAME, payload); } + @ScriptCallable + @KeywordArgs(names = {"options", "pushToMemory"}, types = {PyDictionary.class, Boolean.class}) + public void addPointAnnotation(PyObject[] pyArgs, String[] keywords) throws Exception { + PyArgumentMap argumentMap = + PyArgumentMap.interpretPyArgs(pyArgs, keywords, ApexChartModelDelegate.class, "addPointAnnotation"); + PyDictionary options = (PyDictionary) argumentMap.get("options"); + Boolean pushToMemory = argumentMap.getBooleanArg("pushToMemory", true); + + Gson gson = new Gson(); + log.debug("Calling addPointAnnotation"); + JsonObject payload = new JsonObject(); + payload.addProperty("functionToCall", "addPointAnnotation"); + payload.add("options", gson.toJsonTree(options)); + payload.addProperty("pushToMemory", pushToMemory); + fireEvent(OUTBOUND_EVENT_NAME, payload); + } + + @ScriptCallable + public void clearAnnotations() throws Exception { + log.debug("Calling clearAnnotations"); + JsonObject payload = new JsonObject(); + payload.addProperty("functionToCall", "clearAnnotations"); + fireEvent(OUTBOUND_EVENT_NAME, payload); + } + + @ScriptCallable + @KeywordArgs(names = {"newSeries", "animate"}, types = {List.class, Boolean.class}) + public void updateSeries(PyObject[] pyArgs, String[] keywords) throws Exception { + PyArgumentMap argumentMap = + PyArgumentMap.interpretPyArgs(pyArgs, keywords, ApexChartModelDelegate.class, "updateSeries"); + List newSeries = (List) argumentMap.get("newSeries"); + Boolean animate = argumentMap.getBooleanArg("animate", true); + + Gson gson = new Gson(); + log.debug("Calling updateSeries"); + JsonObject payload = new JsonObject(); + payload.addProperty("functionToCall", "updateSeries"); + payload.add("newSeries", gson.toJsonTree(newSeries)); + payload.addProperty("animate", animate); + fireEvent(OUTBOUND_EVENT_NAME, payload); + } + // when a ComponentStoreDelegate event is fired from the client side, it comes through this method. @Override public void handleEvent(EventFiredMsg message) { diff --git a/gateway/src/main/resources/mounted/Components.js b/gateway/src/main/resources/mounted/Components.js index 2f9336a..2409023 100644 --- a/gateway/src/main/resources/mounted/Components.js +++ b/gateway/src/main/resources/mounted/Components.js @@ -6249,17 +6249,26 @@ class ApexChartGatewayDelegate extends perspective_client_1.ComponentStoreDelega if (this.chart) { logger.debug(() => `Received '${eventName}' event!`); const { MESSAGE_RESPONSE_EVENT, MESSAGE_REQUEST_EVENT } = MessageEvents; - const { functionToCall, seriesName } = eventObject; + const functionToCall = eventObject.functionToCall; switch (eventName) { case MESSAGE_RESPONSE_EVENT: if (functionToCall == "toggleSeries") { - this.fireEvent(MESSAGE_REQUEST_EVENT, { result: this.chart.toggleSeries(seriesName) }); + this.fireEvent(MESSAGE_REQUEST_EVENT, { result: this.chart.toggleSeries(eventObject.seriesName) }); } else if (functionToCall == "showSeries") { - this.chart.showSeries(seriesName); + this.chart.showSeries(eventObject.seriesName); } else if (functionToCall == "hideSeries") { - this.chart.hideSeries(seriesName); + this.chart.hideSeries(eventObject.seriesName); + } + else if (functionToCall == "addPointAnnotation") { + this.chart.addPointAnnotation(eventObject.options, eventObject.pushToMemory); + } + else if (functionToCall == "clearAnnotations") { + this.chart.clearAnnotations(); + } + else if (functionToCall == "updateSeries") { + this.chart.updateSeries(eventObject.newSeries, eventObject.animate); } break; default: diff --git a/web/packages/client/typescript/components/ApexChart.tsx b/web/packages/client/typescript/components/ApexChart.tsx index f76d3b4..f2a508e 100644 --- a/web/packages/client/typescript/components/ApexChart.tsx +++ b/web/packages/client/typescript/components/ApexChart.tsx @@ -63,19 +63,22 @@ export class ApexChartGatewayDelegate extends ComponentStoreDelegate { MESSAGE_REQUEST_EVENT } = MessageEvents; - const { - functionToCall, - seriesName - } = eventObject; + const functionToCall = eventObject.functionToCall; switch (eventName) { case MESSAGE_RESPONSE_EVENT: if (functionToCall == "toggleSeries") { - this.fireEvent(MESSAGE_REQUEST_EVENT, { result: this.chart.toggleSeries(seriesName) }); + this.fireEvent(MESSAGE_REQUEST_EVENT, { result: this.chart.toggleSeries(eventObject.seriesName) }); } else if (functionToCall == "showSeries") { - this.chart.showSeries(seriesName); + this.chart.showSeries(eventObject.seriesName); } else if (functionToCall == "hideSeries") { - this.chart.hideSeries(seriesName); + this.chart.hideSeries(eventObject.seriesName); + } else if (functionToCall == "addPointAnnotation") { + this.chart.addPointAnnotation(eventObject.options, eventObject.pushToMemory); + } else if (functionToCall == "clearAnnotations") { + this.chart.clearAnnotations(); + } else if (functionToCall == "updateSeries") { + this.chart.updateSeries(eventObject.newSeries, eventObject.animate); } break; default: