Skip to content

Commit 434c92c

Browse files
committed
Fix #1531
1 parent d8404bf commit 434c92c

File tree

4 files changed

+114
-15
lines changed

4 files changed

+114
-15
lines changed

AAChartKitDemo/Demo/AAOptionsWithJS/JSFunctionForAAChartEventsComposer2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ NS_ASSUME_NONNULL_BEGIN
1414

1515
@interface JSFunctionForAAChartEventsComposer2 : NSObject
1616

17+
+ (AAOptions *)addClickEventToXAxisLabelAndAccessData;
1718
+ (AAOptions *)defaultSelectedAPointForLineChart;
19+
+ (AAOptions *)configureBlinkMarkerChart;
1820

1921
@end
2022

AAChartKitDemo/Demo/AAOptionsWithJS/JSFunctionForAAChartEventsComposer2.m

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,52 @@
1212

1313
@implementation JSFunctionForAAChartEventsComposer2
1414

15+
//How to add click event to X-axis label and access data ?
16+
//https://www.highcharts.com/forum/viewtopic.php?t=40590
17+
//https://codepen.io/anon/pen/LmObxY?editors=1010
18+
//https://github.com/AAChartModel/AAChartKit/issues/1531
19+
+ (AAOptions *)addClickEventToXAxisLabelAndAccessData {
20+
AAOptions *options = AAOptions.new
21+
.chartSet(AAChart.new
22+
.typeSet(AAChartTypeColumn)
23+
.eventsSet(AAChartEvents.new
24+
.loadSet(@AAJSFunc(function() {
25+
var axis = this.xAxis[0];
26+
var ticks = axis.ticks;
27+
var points = this.series[0].points;
28+
var tooltip = this.tooltip;
29+
30+
points.forEach(function(point, i) {
31+
if (ticks[i]) {
32+
var label = ticks[i].label.element;
33+
34+
label.onclick = function() {
35+
tooltip.getPosition(null, null, point);
36+
tooltip.refresh(point);
37+
};
38+
}
39+
});
40+
}))))
41+
.xAxisSet(AAXAxis.new
42+
.categoriesSet(@[@"Africa", @"America", @"Asia", @"Europe", @"Oceania"]))
43+
.yAxisSet(AAYAxis.new
44+
.minSet(@0))
45+
.tooltipSet(AATooltip.new
46+
.valueSuffixSet(@" millions"))
47+
.plotOptionsSet(AAPlotOptions.new
48+
.seriesSet(AASeries.new
49+
.dataLabelsSet(AADataLabels.new
50+
.enabledSet(YES))))
51+
.seriesSet(@[
52+
AASeriesElement.new
53+
.nameSet(@"Year 1800")
54+
.dataSet(@[@107, @31, @635, @203, @2])
55+
]);
56+
57+
return options;
58+
}
59+
60+
1561
/*
1662
Highcharts.chart('container', {
1763
xAxis: {
@@ -105,4 +151,36 @@ + (AAOptions *)defaultSelectedAPointForLineChart {
105151
return options;
106152
}
107153

154+
//https://github.com/AAChartModel/AAChartKit/issues/1531
155+
//https://stackoverflow.com/questions/42062016/in-high-chart-how-to-add-event-for-label-click
156+
+ (AAOptions *)configureBlinkMarkerChart {
157+
AAOptions *options = AAOptions.new
158+
.chartSet(AAChart.new
159+
.typeSet(AAChartTypeAreaspline)
160+
.eventsSet(AAChartEvents.new
161+
.loadSet(@AAJSFunc(function() {
162+
const childNodes = this.xAxis[0].labelGroup.element.childNodes;
163+
childNodes
164+
.forEach(function(label, index) {
165+
label.style.cursor = "pointer";
166+
label.onclick = function() {
167+
alert('You clicked on ' + this.textContent + ', index: ' + index);
168+
}
169+
});
170+
}))))
171+
.xAxisSet(AAXAxis.new
172+
.categoriesSet(@[@"一月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月", @"十月", @"十一月", @"十二月"]))
173+
.seriesSet(@[
174+
AASeriesElement.new
175+
.dataSet(@[@7.0, @6.9, @2.5, @14.5, @18.2, @21.5, @5.2, @26.5, @23.3, @45.3, @13.9, @9.6])
176+
.markerSet(AAMarker.new
177+
.lineColorSet(AAColor.redColor)
178+
.lineWidthSet(@3)
179+
.radiusSet(@10))
180+
]);
181+
182+
return options;
183+
}
184+
185+
108186
@end

AAChartKitDemo/Demo/AAOptionsWithJS/JSFunctionForAAChartEventsVC.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ - (id)chartConfigurationWithSelectedIndex:(NSUInteger)selectedIndex {
6262
case 9: return [self configureECGStyleChart]; //配置心电图样式的图表
6363
case 10: return [self configureTheSizeOfTheSliceOfDonutAndPieChart]; //配置环形图和饼图的扇区大小
6464
// case 11: return [self configurePlotBackgroundClickEvent]; //配置绘图区的点击事件
65-
case 11: return [JSFunctionForAAChartEventsComposer2 defaultSelectedAPointForLineChart];
65+
// case 11: return [JSFunctionForAAChartEventsComposer2 defaultSelectedAPointForLineChart];
66+
case 11: return [JSFunctionForAAChartEventsComposer2 configureBlinkMarkerChart];
67+
6668

6769

6870
}

AAChartKitLib/AAChartCreator/AAChartView.m

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -438,34 +438,51 @@ - (void)configureTheOptionsJsonStringWithAAOptions:(AAOptions *)aaOptions {
438438

439439
#pragma mark - WKUIDelegate
440440
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
441-
#if TARGET_OS_IPHONE
442-
UIAlertController *alertController =
443-
[UIAlertController alertControllerWithTitle:@"JS WARNING"
444-
message:message
445-
preferredStyle:UIAlertControllerStyleAlert];
441+
#if TARGET_OS_IOS
442+
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"JS WARNING" message:message preferredStyle:UIAlertControllerStyleAlert];
446443

447-
UIAlertAction *alertAction =
448-
[UIAlertAction actionWithTitle:@"Okay"
449-
style:UIAlertActionStyleDefault
450-
handler:^(UIAlertAction * _Nonnull action) {
444+
UIAlertAction *okayAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
451445
completionHandler();
452446
}];
453-
[alertController addAction:alertAction];
447+
[alertController addAction:okayAction];
454448

455-
UIViewController *alertHelperController = [[UIViewController alloc]init];
456-
[self addSubview:alertHelperController.view];
449+
UIViewController *presentingViewController = [self nextUIViewController];
450+
if (!presentingViewController) {
451+
AADetailLog(@"Unable to present UIAlertController from AAChartView. Completing JavaScript alert handler.");
452+
completionHandler();
453+
return;
454+
}
455+
456+
[presentingViewController presentViewController:alertController animated:YES completion:nil];
457457

458-
[alertHelperController presentViewController:alertController animated:YES completion:nil];
459458
#elif TARGET_OS_MAC
460459
NSAlert *alert = [[NSAlert alloc] init];
461460
alert.alertStyle = NSAlertStyleWarning;
462461
alert.messageText = @"JS WARNING";
463462
alert.informativeText = message;
464463
[alert addButtonWithTitle:@"Okay"];
465-
[alert beginSheetModalForWindow:[self window] completionHandler:nil];
464+
465+
[alert beginSheetModalForWindow:[NSApplication sharedApplication].mainWindow completionHandler:^(NSModalResponse response) {
466+
if (response == NSModalResponseOK) {
467+
completionHandler();
468+
}
469+
}];
466470
#endif
467471
}
468472

473+
#if TARGET_OS_IOS
474+
- (UIViewController *)nextUIViewController {
475+
UIResponder *responder = self;
476+
while (responder != nil) {
477+
if ([responder isKindOfClass:[UIViewController class]]) {
478+
return (UIViewController *)responder;
479+
}
480+
responder = [responder nextResponder];
481+
}
482+
return nil;
483+
}
484+
#endif
485+
469486
#pragma mark - AAChartView Event Handler
470487
- (void)didFinishLoadHandler:(AADidFinishLoadBlock)handler {
471488
self.didFinishLoadBlock = handler;

0 commit comments

Comments
 (0)