|
| 1 | +// |
| 2 | +// CustomAnyEventCallback.swift |
| 3 | +// AAInfographicsDemo |
| 4 | +// |
| 5 | +// Created by Admin on 2022/5/31. |
| 6 | +// Copyright © 2022 An An. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import AAInfographics |
| 11 | +import WebKit |
| 12 | + |
| 13 | +let kUserContentMessageNameChartTouchEnd = "touchEnd" |
| 14 | + |
| 15 | +class CustomTouchEndEventCallback: UIViewController, WKScriptMessageHandler { |
| 16 | + private var aaChartView: AAChartView! |
| 17 | + |
| 18 | + override func viewDidLoad() { |
| 19 | + super.viewDidLoad() |
| 20 | + |
| 21 | + view.backgroundColor = .white |
| 22 | + |
| 23 | + configureChartView() |
| 24 | + configureChartViewCustomEventMessageHandler() |
| 25 | + |
| 26 | + let aaOptions = topRoundedCornersStackingColumnChart() |
| 27 | + aaChartView.aa_drawChartWithChartOptions(aaOptions) |
| 28 | + } |
| 29 | + |
| 30 | + private func configureChartView() { |
| 31 | + aaChartView = AAChartView() |
| 32 | + let chartViewWidth = view.frame.size.width |
| 33 | + let chartViewHeight = view.frame.size.height - 220 |
| 34 | + aaChartView!.frame = CGRect(x: 0, |
| 35 | + y: 60, |
| 36 | + width: chartViewWidth, |
| 37 | + height: chartViewHeight) |
| 38 | + view.addSubview(aaChartView!) |
| 39 | + aaChartView!.isScrollEnabled = false//Disable chart content scrolling |
| 40 | + aaChartView!.isClearBackgroundColor = true |
| 41 | + aaChartView!.delegate = self as AAChartViewDelegate |
| 42 | + } |
| 43 | + |
| 44 | + private func configureChartViewCustomEventMessageHandler() { |
| 45 | + aaChartView!.configuration.userContentController.add(AALeakAvoider.init(delegate: self), name: kUserContentMessageNameChartTouchEnd) |
| 46 | + } |
| 47 | + |
| 48 | + private func topRoundedCornersStackingColumnChart() -> AAOptions { |
| 49 | + let aaChartModel = AAChartModel() |
| 50 | + .chartType(.column) |
| 51 | + .stacking(.normal) |
| 52 | + .title("Top Rounded Corners Stacking Column Chart") |
| 53 | + .colorsTheme(["#fe117c", "#ffc069", "#06caf4",]) |
| 54 | + .series([ |
| 55 | + AASeriesElement() |
| 56 | + .name("Tokyo Hot") |
| 57 | + .borderRadiusTopLeft("50%") |
| 58 | + .borderRadiusTopRight("50%") |
| 59 | + .data([2.10, 2.54, 2.78, 3.62, 4.41, 4.09, 3.83, 4.47, 4.20, 3.94, 3.80, 3.58, 3.19, 4.30, 3.69, 3.52, 3.02, 3.30]), |
| 60 | + ]) |
| 61 | + |
| 62 | + let aaOptions = aaChartModel.aa_toAAOptions() |
| 63 | + |
| 64 | + return aaOptions |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { |
| 69 | + if message.name == kUserContentMessageNameChartTouchEnd { |
| 70 | + let messageBody = message.body |
| 71 | + print(messageBody) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +extension CustomTouchEndEventCallback: AAChartViewDelegate { |
| 78 | + open func aaChartViewDidFinishLoad(_ aaChartView: AAChartView) { |
| 79 | + print("🚀🚀🚀, AAChartView Did Finished Load!!!") |
| 80 | + |
| 81 | + let jsStr = #""" |
| 82 | +function handleTouchEndEvent() { |
| 83 | + window.webkit.messageHandlers.touchEnd.postMessage("成功了, 真的成功了"); |
| 84 | +
|
| 85 | +} |
| 86 | + let container = document.getElementById('container'); |
| 87 | + container.addEventListener("touchend", handleTouchEndEvent, false); |
| 88 | +"""# |
| 89 | + |
| 90 | + aaChartView.evaluateJavaScript(jsStr) { (item, error) in |
| 91 | +#if DEBUG |
| 92 | + if error != nil { |
| 93 | + let objcError = error! as NSError |
| 94 | + let errorUserInfo = objcError.userInfo |
| 95 | + |
| 96 | + let errorInfo = |
| 97 | + """ |
| 98 | + |
| 99 | + ☠️☠️💀☠️☠️WARNING!!!!!!!!!!!!!!!!!!!! FBI WARNING !!!!!!!!!!!!!!!!!!!!WARNING☠️☠️💀☠️☠️ |
| 100 | + ========================================================================================== |
| 101 | + ------------------------------------------------------------------------------------------ |
| 102 | + code = \(objcError.code); |
| 103 | + domain = \(objcError.domain); |
| 104 | + userInfo = { |
| 105 | + NSLocalizedDescription = "A JavaScript exception occurred"; |
| 106 | + WKJavaScriptExceptionColumnNumber = \(errorUserInfo["WKJavaScriptExceptionColumnNumber"] ?? ""); |
| 107 | + WKJavaScriptExceptionLineNumber = \(errorUserInfo["WKJavaScriptExceptionLineNumber"] ?? ""); |
| 108 | + WKJavaScriptExceptionMessage = \(errorUserInfo["WKJavaScriptExceptionMessage"] ?? ""); |
| 109 | + WKJavaScriptExceptionSourceURL = \(errorUserInfo["WKJavaScriptExceptionSourceURL"] ?? ""); |
| 110 | + } |
| 111 | + ------------------------------------------------------------------------------------------ |
| 112 | + ========================================================================================== |
| 113 | + ☠️☠️💀☠️☠️WARNING!!!!!!!!!!!!!!!!!!!! FBI WARNING !!!!!!!!!!!!!!!!!!!!WARNING☠️☠️💀☠️☠️ |
| 114 | + |
| 115 | + """ |
| 116 | + print(errorInfo) |
| 117 | + } |
| 118 | +#endif |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + open func aaChartView(_ aaChartView: AAChartView, clickEventMessage: AAClickEventMessageModel) { |
| 125 | + print( |
| 126 | + """ |
| 127 | +
|
| 128 | + clicked point series element name: \(clickEventMessage.name ?? "") |
| 129 | + 🖱🖱🖱WARNING!!!!!!!!!!!!!!!!!!!! Click Event Message !!!!!!!!!!!!!!!!!!!! WARNING🖱🖱🖱 |
| 130 | + ========================================================================================== |
| 131 | + ------------------------------------------------------------------------------------------ |
| 132 | + user finger moved over!!!,get the move over event message: { |
| 133 | + category = \(String(describing: clickEventMessage.category)) |
| 134 | + index = \(String(describing: clickEventMessage.index)) |
| 135 | + name = \(String(describing: clickEventMessage.name)) |
| 136 | + offset = \(String(describing: clickEventMessage.offset)) |
| 137 | + x = \(String(describing: clickEventMessage.x)) |
| 138 | + y = \(String(describing: clickEventMessage.y)) |
| 139 | + } |
| 140 | + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 141 | + |
| 142 | + |
| 143 | + """ |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + open func aaChartView(_ aaChartView: AAChartView, moveOverEventMessage: AAMoveOverEventMessageModel) { |
| 148 | + print( |
| 149 | + """ |
| 150 | +
|
| 151 | + moved over point series element name: \(moveOverEventMessage.name ?? "") |
| 152 | + ✋🏻✋🏻✋🏻✋🏻✋🏻WARNING!!!!!!!!!!!!!! Move Over Event Message !!!!!!!!!!!!!! WARNING✋🏻✋🏻✋🏻✋🏻✋🏻 |
| 153 | + ========================================================================================== |
| 154 | + ------------------------------------------------------------------------------------------ |
| 155 | + user finger moved over!!!,get the move over event message: { |
| 156 | + category = \(String(describing: moveOverEventMessage.category)) |
| 157 | + index = \(String(describing: moveOverEventMessage.index)) |
| 158 | + name = \(String(describing: moveOverEventMessage.name)) |
| 159 | + offset = \(String(describing: moveOverEventMessage.offset)) |
| 160 | + x = \(String(describing: moveOverEventMessage.x)) |
| 161 | + y = \(String(describing: moveOverEventMessage.y)) |
| 162 | + } |
| 163 | + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 164 | + |
| 165 | + |
| 166 | + """ |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | + |
0 commit comments