Thanks to Highcharts iOS wrapper you can now assign native iOS closures to events for specific chart elements. We will show you a small taste of such usage. For this purpose we will let appear a simple alert with point coordinates when it's clicked but keep in mind that you can achieve much more with HIFunction
mechanism!
First of all, you need to create a plotOptions
object for your series type:
HIPlotOptions *plotOptions = [[HIPlotOptions alloc] init]; plotOptions.series = [[HISeries alloc] init];
Now, you can refer to the point event and add on click closure like this:
plotOptions.series.point = [[HIPoint alloc] init]; plotOptions.series.point.events = [[HIEvents alloc] init]; HIClosure closure = ^(HIChartContext *context) { NSString *alertMessage = [NSString stringWithFormat:@"Category: %@, value: %@", [context getProperty:@"this.category"], [context getProperty:@"this.y"]]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:alertMessage preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; }; plotOptions.series.point.events.click = [[HIFunction alloc] initWithClosure:closure properties:@[@"this.category", @"this.y"]];
As you can see in the above code snippet first argument of the HIFunction
is the actual closure. The second argument is a simple string array of chart elements. We need to put them here to let wrapper pull them for us during HIFunction
instantiation. Thanks to this, we can refer to these elements corresponding values by getProperty:
method. You can pull any data from the chart like this. Depending on the current needs you can just run some code, withdraw data from the chart, return a String to the chart (e.g. in HITooltip formatter) and even put pure JavaScript function in the constructor in the String format.
For more information feel free to check the API documentation.
Original info at: the Highcharts iOS GitHub repository
In case you are using Swift then the same code will look like this:
let plotOptions = HIPlotOptions() let chartSeries = HISeries() plotOptions.series = chartSeries let chartPoint = HIPoint() plotOptions.series.point = chartPoint let chartEvents = HIEvents() plotOptions.series.point.events = chartEvents let chartFunction = HIFunction(closure: { context in guard let context = context else { return } let alertMessage = "Category: \(context.getProperty("this.category") ?? "unknown"), value: \(context.getProperty("this.y") ?? "unknown")" let alert = UIAlertController(title: nil, message: alertMessage, preferredStyle: .alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alert.addAction(defaultAction) self.present(alert, animated: true, completion: nil) }, properties: ["this.category", "this.y"]) plotOptions.series.point.events.click = chartFunction