|
| 1 | +// |
| 2 | +// AAChartView+ScreenRotation.swift |
| 3 | +// AAInfographicsDemo |
| 4 | +// |
| 5 | +// Created by AnAn on 2025/4/24. |
| 6 | +// Copyright © 2025 An An. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import AAInfographics |
| 10 | +import UIKit |
| 11 | + |
| 12 | +extension AAChartView { |
| 13 | + |
| 14 | +#if os(iOS) |
| 15 | + /// Set the chart view content be adaptive to screen rotation with default animation effect |
| 16 | + public func aa_adaptiveScreenRotation() { |
| 17 | + let aaAnimation = AAAnimation() |
| 18 | + .duration(800) |
| 19 | + .easing(.easeOutQuart) |
| 20 | + aa_adaptiveScreenRotationWithAnimation(aaAnimation) |
| 21 | + } |
| 22 | + |
| 23 | + /// Set the chart view content be adaptive to screen rotation with custom animation effect |
| 24 | + /// Refer to https://api.highcharts.com/highcharts#Chart.setSize |
| 25 | + /// |
| 26 | + /// - Parameter animation: The instance object of AAAnimation |
| 27 | + public func aa_adaptiveScreenRotationWithAnimation(_ animation: AAAnimation) { |
| 28 | + NotificationCenter.default.addObserver( |
| 29 | + forName: UIDevice.orientationDidChangeNotification, |
| 30 | + object: nil, |
| 31 | + queue: nil) { [weak self] _ in |
| 32 | + //Delay execution by 0.01 seconds to prevent incorrect screen width and height obtained when the screen is rotated |
| 33 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { |
| 34 | + self?.aa_resizeChart(animation: animation) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public func aa_resizeChart(animation: AAAnimation) { |
| 40 | + let animationJsonStr = animation.toJSON() |
| 41 | + // let jsFuncStr = "changeChartSize('\(frame.size.width)','\(frame.size.height)','\(animationJsonStr)')" |
| 42 | + let jsFuncStr = |
| 43 | + """ |
| 44 | + function changeChartSize(receivedWidth, receivedHeight, receivedAnimation) { |
| 45 | + let container = document.getElementById('container'); |
| 46 | + container.style.width = receivedWidth; |
| 47 | + container.style.height = receivedHeight; |
| 48 | + |
| 49 | + let aaAnimation; |
| 50 | + if (receivedAnimation) { |
| 51 | + // Parse the received JSON string |
| 52 | + aaAnimation = JSON.parse(receivedAnimation); |
| 53 | + let animationEasingType = aaAnimation.easing; |
| 54 | + aaAnimation.easing = configureTheChartAnimationEasingType(animationEasingType); |
| 55 | + } |
| 56 | + |
| 57 | + aaGlobalChart.setSize(receivedWidth, receivedHeight, aaAnimation); |
| 58 | + } |
| 59 | + |
| 60 | + // Pass animationJsonStr as a string literal using single quotes |
| 61 | + changeChartSize(\(frame.size.width), \(frame.size.height), \(animationJsonStr)) |
| 62 | + """ |
| 63 | + safeEvaluateJavaScriptString(jsFuncStr) |
| 64 | + } |
| 65 | + |
| 66 | + private func safeEvaluateJavaScriptString (_ jsString: String) { |
| 67 | + evaluateJavaScript(jsString, completionHandler: { (item, error) in |
| 68 | +#if DEBUG |
| 69 | + if error != nil { |
| 70 | + let objcError = error! as NSError |
| 71 | + let errorUserInfo = objcError.userInfo |
| 72 | + |
| 73 | + let errorInfo = |
| 74 | + """ |
| 75 | + |
| 76 | + ☠️☠️💀☠️☠️WARNING!!!!!!!!!!!!!!!!!!!! JS WARNING !!!!!!!!!!!!!!!!!!!!WARNING☠️☠️💀☠️☠️ |
| 77 | + ------------------------------------------------------------------------------------------ |
| 78 | + code = \(objcError.code); |
| 79 | + domain = \(objcError.domain); |
| 80 | + userInfo = { |
| 81 | + NSLocalizedDescription = "\(errorUserInfo["NSLocalizedDescription"] ?? "")"; |
| 82 | + WKJavaScriptExceptionColumnNumber = \(errorUserInfo["WKJavaScriptExceptionColumnNumber"] ?? ""); |
| 83 | + WKJavaScriptExceptionLineNumber = \(errorUserInfo["WKJavaScriptExceptionLineNumber"] ?? ""); |
| 84 | + WKJavaScriptExceptionMessage = \(errorUserInfo["WKJavaScriptExceptionMessage"] ?? ""); |
| 85 | + WKJavaScriptExceptionSourceURL = \(errorUserInfo["WKJavaScriptExceptionSourceURL"] ?? ""); |
| 86 | + } |
| 87 | + ------------------------------------------------------------------------------------------ |
| 88 | + ☠️☠️💀☠️☠️WARNING!!!!!!!!!!!!!!!!!!!! JS WARNING !!!!!!!!!!!!!!!!!!!!WARNING☠️☠️💀☠️☠️ |
| 89 | + |
| 90 | + """ |
| 91 | + print(errorInfo) |
| 92 | + } |
| 93 | +#endif |
| 94 | + }) |
| 95 | + } |
| 96 | +#endif |
| 97 | + |
| 98 | +} |
0 commit comments