dear hubert.k,
Thank you very much for your help !
I've tried to adapt your code to mine but I still have difficulties to make it work the way I need to. This is entirely my fault, here's is some context to better explain what i'm trying to do :
I have defined an empty scatter chart. I would like to click somewhere on the scatter chart and 1) display a point where I clicked, 2) retrieve the x,y coordinates in a static variable I've defined elsewhere :
Code: Select all
static var ScatterPointsAdded:[[Int]] = [[]]
When the point as been placed in the scatter chart, I would like to store the [x,y] in my variable
ScatterPointsAdded
Here's is my code for the moment that doesn't work.
Code: Select all
class AddPointViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let chartView = HIChartView(frame: view.bounds)
let options = HIOptions()
let chart = HIChart()
chart.type = "scatter"
chart.margin = [50, 50, 50, 50]
chart.events = HIEvents()
chart.events.click = HIFunction(jsFunction: "function (e) { var x = Math.round(e.xAxis[0].value), y = Math.round(e.yAxis[0].value), series = this.series[0]; series.addPoint([x, y]);}")
let ImageURL = createLocalUrl(forImageNamed: "Image")
chart.plotBackgroundImage = ImageURL?.absoluteString
options.chart = chart
let title = HITitle()
title.text = "Live random data"
options.title = title
let subtitle = HISubtitle()
subtitle.text = "Click the plot area to add a point. Click a point to remove it."
options.subtitle = subtitle
let xAxis = HIXAxis()
xAxis.title = HITitle()
xAxis.title.text = ""
xAxis.gridLineWidth = 0
xAxis.minPadding = 0.2
xAxis.maxPadding = 0.2
xAxis.min = 0
xAxis.max = 60
// xAxis.maxZoom = 60
let yAxis = HIYAxis()
yAxis.title = HITitle()
yAxis.title.text = ""
yAxis.gridLineWidth = 0
yAxis.minPadding = 0.2
yAxis.maxPadding = 0.2
yAxis.min = 0
yAxis.max = 100
// yAxis.maxZoom = 60
let plotLines = HIPlotLines()
plotLines.value = 0
plotLines.width = 0
plotLines.color = HIColor(hexValue: "808080")
xAxis.plotLines = [plotLines]
yAxis.plotLines = [plotLines]
options.xAxis = [xAxis]
options.yAxis = [yAxis]
let legend = HILegend()
legend.enabled = false
options.legend = legend
let exporting = HIExporting()
exporting.enabled = false
options.exporting = exporting
let plotOptions = HIPlotOptions()
plotOptions.scatter = HIScatter()
plotOptions.scatter.lineWidth = 0
plotOptions.scatter.point = HIPoint()
plotOptions.scatter.point.events = HIEvents()
// original function
//plotOptions.scatter.point.events.click = HIFunction(jsFunction: "function () { if (this.series.data.length > 1000) { this.remove(); } }")
// new function
plotOptions.series.point.events.click = HIFunction(closure: { [weak self] context in guard let self = self, let context = context else { return }; let x = context.getProperty("this.x") ?? ""; let y = context.getProperty("this.y") ?? ""; let alertMessage = "x: \(x), y: \(y)"; let alertController = UIAlertController(title: nil,message: alertMessage,preferredStyle: .alert); let okAction = UIAlertAction(title: "OK", style: .default, handler: nil); alertController.addAction(okAction); self.present(alertController, animated: true, completion: nil) }, properties: ["this.x", "this.y"])
options.plotOptions = plotOptions
let scatter = HIScatter()
// scatter.data = [[20, 20], [80, 80]]
// store x,y
ScatterPointsAdded.append([x,y])
options.series = [scatter]
chartView.options = options
self.FieldChartView.addSubview(chartView)
}
Could you help me a little bit more ?
Best regards