Skip to content

Commit

Permalink
Added new scripting functions, updated to 1.0.16
Browse files Browse the repository at this point in the history
Added addPointAnnotation, clearAnnotations, and updateSeries.
  • Loading branch information
iatraviscox committed Oct 24, 2023
1 parent a13cf48 commit 0b52127
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 12 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ plugins {
id "base"
}

version "1.0.15"
version "1.0.16"
group "com.kyvislabs"


Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions gateway/src/main/resources/mounted/Components.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions web/packages/client/typescript/components/ApexChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0b52127

Please sign in to comment.