@@ -89,6 +89,8 @@ public class AAChartView: WKWebView {
8989
9090 private var clickEventEnabled : Bool ?
9191 private var touchEventEnabled : Bool ?
92+ private var beforeDrawChartJavaScript : String ?
93+ private var afterDrawChartJavaScript : String ?
9294
9395 private weak var _delegate : AAChartViewDelegate ?
9496 public weak var delegate : AAChartViewDelegate ? {
@@ -174,6 +176,10 @@ public class AAChartView: WKWebView {
174176
175177 private var optionsJson : String ?
176178
179+ #if DEBUG
180+ public var shouldPrintOptionsJSON : Bool = true
181+ #endif
182+
177183 // MARK: - Initialization
178184 override private init ( frame: CGRect , configuration: WKWebViewConfiguration ) {
179185 super. init ( frame: frame, configuration: configuration)
@@ -194,9 +200,25 @@ public class AAChartView: WKWebView {
194200
195201
196202 private func drawChart( ) {
203+ if beforeDrawChartJavaScript != nil {
204+ #if DEBUG
205+ print ( " 📝 \( beforeDrawChartJavaScript ?? " " ) " )
206+ #endif
207+ safeEvaluateJavaScriptString ( beforeDrawChartJavaScript!)
208+ beforeDrawChartJavaScript = nil
209+ }
210+
197211 //Add `frame.size.height` to solve the problem that the height of the new version of Highcharts chart will not adapt to the container
198- let jsStr = " loadTheHighChartView(' \( optionsJson ?? " " ) ',' \( contentWidth ?? 0 ) ',' \( contentHeight ?? frame . size . height ) ') "
212+ let jsStr = " loadTheHighChartView(' \( optionsJson ?? " " ) ',' \( contentWidth ?? 0 ) ',' \( contentHeight ?? 0 ) '); "
199213 safeEvaluateJavaScriptString ( jsStr)
214+
215+ if afterDrawChartJavaScript != nil {
216+ #if DEBUG
217+ print ( " 📝 \( afterDrawChartJavaScript ?? " " ) " )
218+ #endif
219+ safeEvaluateJavaScriptString ( afterDrawChartJavaScript!)
220+ afterDrawChartJavaScript = nil
221+ }
200222 }
201223
202224 private func safeEvaluateJavaScriptString ( _ jsString: String ) {
@@ -221,9 +243,9 @@ public class AAChartView: WKWebView {
221243 code = \( objcError. code) ;
222244 domain = \( objcError. domain) ;
223245 userInfo = {
224- NSLocalizedDescription = " A JavaScript exception occurred " ;
246+ NSLocalizedDescription = " \( errorUserInfo [ " NSLocalizedDescription " ] ?? " " ) " ;
225247 WKJavaScriptExceptionColumnNumber = \( errorUserInfo [ " WKJavaScriptExceptionColumnNumber " ] ?? " " ) ;
226- WKJavaScriptExceptionLineNumber = \( errorUserInfo [ " WKJavaScriptExceptionLineNumber " ] ?? " " ) ;
248+ WKJavaScriptExceptionLineNumber = \( errorUserInfo [ " WKJavaScriptExceptionLineNumber " ] ?? " " ) ;
227249 WKJavaScriptExceptionMessage = \( errorUserInfo [ " WKJavaScriptExceptionMessage " ] ?? " " ) ;
228250 WKJavaScriptExceptionSourceURL = \( errorUserInfo [ " WKJavaScriptExceptionSourceURL " ] ?? " " ) ;
229251 }
@@ -252,32 +274,43 @@ public class AAChartView: WKWebView {
252274 }
253275
254276 private func configureOptionsJsonStringWithAAOptions( _ aaOptions: AAOptions ) {
277+ if aaOptions. beforeDrawChartJavaScript != nil {
278+ beforeDrawChartJavaScript = aaOptions. beforeDrawChartJavaScript
279+ aaOptions. beforeDrawChartJavaScript = nil
280+ }
281+
282+ if aaOptions. afterDrawChartJavaScript != nil {
283+ afterDrawChartJavaScript = aaOptions. afterDrawChartJavaScript
284+ aaOptions. afterDrawChartJavaScript = nil
285+ }
286+
255287 if isClearBackgroundColor == true {
256288 aaOptions. chart? . backgroundColor = AAColor . clear
257289 }
258290
259291 if clickEventEnabled == true {
260292 aaOptions. clickEventEnabled = true
261- configurePlotOptionsSeriesPointEvents ( aaOptions)
262293 }
263294 if touchEventEnabled == true {
264295 aaOptions. touchEventEnabled = true
265- if clickEventEnabled != true { //Avoid duplicate invocation of configuration method
266- configurePlotOptionsSeriesPointEvents ( aaOptions )
267- }
296+ }
297+ if clickEventEnabled == true || touchEventEnabled == true {
298+ configurePlotOptionsSeriesPointEvents ( aaOptions )
268299 }
269300
270301 #if DEBUG
271- let modelJsonDic = aaOptions. toDic ( )
272- let data = try ? JSONSerialization . data ( withJSONObject: modelJsonDic, options: . prettyPrinted)
273- if data != nil {
274- let prettyPrintedModelJson = String ( data: data!, encoding: String . Encoding. utf8)
275- print ( """
302+ if shouldPrintOptionsJSON {
303+ let modelJsonDic = aaOptions. toDic ( )
304+ let data = try ? JSONSerialization . data ( withJSONObject: modelJsonDic, options: . prettyPrinted)
305+ if data != nil {
306+ let prettyPrintedModelJson = String ( data: data!, encoding: String . Encoding. utf8)
307+ print ( """
276308
277309 -----------🖨🖨🖨 console log AAOptions JSON information of AAChartView 🖨🖨🖨-----------:
278310 \( prettyPrintedModelJson!)
279311
280312 """ )
313+ }
281314 }
282315 #endif
283316
@@ -607,12 +640,12 @@ extension AAChartView {
607640 queue: nil ) { [ weak self] _ in
608641 //Delay execution by 0.01 seconds to prevent incorrect screen width and height obtained when the screen is rotated
609642 DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.01 ) {
610- self ? . handleDeviceOrientationChangeEventWithAnimation ( animation)
643+ self ? . aa_resizeChart ( animation : animation)
611644 }
612645 }
613646 }
614647
615- private func handleDeviceOrientationChangeEventWithAnimation ( _ animation: AAAnimation ) {
648+ public func aa_resizeChart ( animation: AAAnimation ) {
616649 let animationJsonStr = animation. toJSON ( )
617650 let jsFuncStr = " changeChartSize(' \( frame. size. width) ',' \( frame. size. height) ',' \( animationJsonStr) ') "
618651 safeEvaluateJavaScriptString ( jsFuncStr)
@@ -735,43 +768,69 @@ extension AAChartView {
735768extension AAChartView {
736769
737770 func getJSONStringFromDictionary( dictionary: [ String : Any ] ) -> String {
738- if ! JSONSerialization. isValidJSONObject ( dictionary) {
739- print ( " ❌ String object is not valid Dictionary JSON String " )
771+ guard JSONSerialization . isValidJSONObject ( dictionary) else {
772+ print ( " ❌ Dictionary object is not valid JSON " )
740773 return " "
741774 }
742775
743- let data : Data = try ! JSONSerialization . data ( withJSONObject: dictionary, options: [ ] )
744- let JSONString = String ( data: data, encoding: . utf8)
745- return JSONString! as String
776+ do {
777+ let data = try JSONSerialization . data ( withJSONObject: dictionary, options: [ ] )
778+ if let jsonString = String ( data: data, encoding: . utf8) {
779+ return jsonString
780+ }
781+ } catch {
782+ print ( " ❌ Error serializing dictionary to JSON: \( error. localizedDescription) " )
783+ }
784+ return " "
746785 }
747786
748787 func getJSONStringFromArray( array: [ Any ] ) -> String {
749- if ! JSONSerialization. isValidJSONObject ( array) {
750- print ( " ❌ String object is not valid Array JSON String " )
788+ guard JSONSerialization . isValidJSONObject ( array) else {
789+ print ( " ❌ Array object is not valid JSON " )
751790 return " "
752791 }
753792
754- let data : Data = try ! JSONSerialization . data ( withJSONObject: array, options: [ ] )
755- let JSONString = String ( data: data, encoding: . utf8)
756- return JSONString! as String
793+ do {
794+ let data = try JSONSerialization . data ( withJSONObject: array, options: [ ] )
795+ if let jsonString = String ( data: data, encoding: . utf8) {
796+ return jsonString
797+ }
798+ } catch {
799+ print ( " ❌ Error serializing array to JSON: \( error. localizedDescription) " )
800+ }
801+ return " "
757802 }
758803
759804 func getDictionaryFromJSONString( jsonString: String ) -> [ String : Any ] {
760- let jsonData : Data = jsonString. data ( using: . utf8) !
761- let dict = try ? JSONSerialization . jsonObject ( with: jsonData, options: . mutableContainers)
762- if dict != nil {
763- return dict as! [ String : Any ]
805+ guard let jsonData = jsonString. data ( using: . utf8) else {
806+ print ( " ❌ Failed to convert string to data " )
807+ return [ : ]
808+ }
809+
810+ do {
811+ if let dict = try JSONSerialization . jsonObject ( with: jsonData, options: . mutableContainers) as? [ String : Any ] {
812+ return dict
813+ }
814+ } catch {
815+ print ( " ❌ Error parsing JSON string to dictionary: \( error. localizedDescription) " )
764816 }
765- return [ String : Any ] ( )
817+ return [ : ]
766818 }
767819
768820 func getArrayFromJSONString( jsonString: String ) -> [ Any ] {
769- let jsonData : Data = jsonString. data ( using: . utf8) !
770- let array = try ? JSONSerialization . jsonObject ( with: jsonData, options: . mutableContainers)
771- if array != nil {
772- return array as! [ Any ]
821+ guard let jsonData = jsonString. data ( using: . utf8) else {
822+ print ( " ❌ Failed to convert string to data " )
823+ return [ ]
824+ }
825+
826+ do {
827+ if let array = try JSONSerialization . jsonObject ( with: jsonData, options: . mutableContainers) as? [ Any ] {
828+ return array
829+ }
830+ } catch {
831+ print ( " ❌ Error parsing JSON string to array: \( error. localizedDescription) " )
773832 }
774- return [ Any ] ( )
833+ return [ ]
775834 }
776835}
777836
0 commit comments