Skip to content

Commit 7f8d6ef

Browse files
committed
Create single AAChartView+ScreenRotation.swift
1 parent 1a218d9 commit 7f8d6ef

File tree

4 files changed

+103
-48
lines changed

4 files changed

+103
-48
lines changed

AAInfographics/AAChartCreator/AAChartView+API.swift

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
*/
3232

33-
import UIKit
33+
import WebKit
3434

3535
// MARK: - Configure Chart View Content With AAChartModel
3636
@available(iOS 10.0, macCatalyst 13.1, macOS 10.13, *)
@@ -314,38 +314,6 @@ extension AAChartView {
314314
safeEvaluateJavaScriptString(jsStr)
315315
}
316316

317-
#if os(iOS)
318-
/// Set the chart view content be adaptive to screen rotation with default animation effect
319-
public func aa_adaptiveScreenRotation() {
320-
let aaAnimation = AAAnimation()
321-
.duration(800)
322-
.easing(.easeOutQuart)
323-
aa_adaptiveScreenRotationWithAnimation(aaAnimation)
324-
}
325-
326-
/// Set the chart view content be adaptive to screen rotation with custom animation effect
327-
/// Refer to https://api.highcharts.com/highcharts#Chart.setSize
328-
///
329-
/// - Parameter animation: The instance object of AAAnimation
330-
public func aa_adaptiveScreenRotationWithAnimation(_ animation: AAAnimation) {
331-
NotificationCenter.default.addObserver(
332-
forName: UIDevice.orientationDidChangeNotification,
333-
object: nil,
334-
queue: nil) { [weak self] _ in
335-
//Delay execution by 0.01 seconds to prevent incorrect screen width and height obtained when the screen is rotated
336-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
337-
self?.aa_resizeChart(animation: animation)
338-
}
339-
}
340-
}
341-
342-
public func aa_resizeChart(animation: AAAnimation) {
343-
let animationJsonStr = animation.toJSON()
344-
let jsFuncStr = "changeChartSize('\(frame.size.width)','\(frame.size.height)','\(animationJsonStr)')"
345-
safeEvaluateJavaScriptString(jsFuncStr)
346-
}
347-
#endif
348-
349317
}
350318

351319

AAInfographics/AAJSFiles.bundle/AAChartView.html

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,6 @@
196196
function evaluateTheJavaScriptStringFunction(jsStringFunction) {
197197
eval(jsStringFunction);
198198
}
199-
200-
function changeChartSize(receivedWidth, receivedHeight, receivedAnimation) {
201-
let container = document.getElementById('container');
202-
container.style.width = receivedWidth;
203-
container.style.height = receivedHeight;
204-
205-
let aaAnimation;
206-
if (receivedAnimation) {
207-
aaAnimation = JSON.parse(receivedAnimation);
208-
let animationEasingType = aaAnimation.easing;
209-
aaAnimation.easing = configureTheChartAnimationEasingType(animationEasingType);
210-
}
211-
212-
aaGlobalChart.setSize(receivedWidth, receivedHeight, aaAnimation);
213-
}
214199

215200
</script>
216201
</body>

AAInfographicsDemo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
848FEAF8265DF5D3006E4F25 /* TestAAChartViewForXibVC.xib in Resources */ = {isa = PBXBuildFile; fileRef = 848FEAF6265DF5D3006E4F25 /* TestAAChartViewForXibVC.xib */; };
119119
84903C8E2DB8D6A40029FE5A /* EmojiParticleAnimationVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84903C8D2DB8D6A40029FE5A /* EmojiParticleAnimationVC.swift */; };
120120
84903C9B2DB9F0680029FE5A /* BubbleChartOptionsComposer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84903C9A2DB9F0680029FE5A /* BubbleChartOptionsComposer.swift */; };
121+
84903C9D2DBA0BD20029FE5A /* AAChartView+ScreenRotation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84903C9C2DBA0BD20029FE5A /* AAChartView+ScreenRotation.swift */; };
121122
84A9FB7E28C9BC0E00240C11 /* AAScatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A9FB7C28C9BC0E00240C11 /* AAScatter.swift */; };
122123
84AE6B1F26A58380001F0755 /* AADateUTCTool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84AE6B1E26A58380001F0755 /* AADateUTCTool.swift */; };
123124
84B68246266DB09100957FC5 /* AAChartSymbolConstant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B68245266DB09100957FC5 /* AAChartSymbolConstant.swift */; };
@@ -367,6 +368,7 @@
367368
848FEAF6265DF5D3006E4F25 /* TestAAChartViewForXibVC.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TestAAChartViewForXibVC.xib; sourceTree = "<group>"; };
368369
84903C8D2DB8D6A40029FE5A /* EmojiParticleAnimationVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiParticleAnimationVC.swift; sourceTree = "<group>"; };
369370
84903C9A2DB9F0680029FE5A /* BubbleChartOptionsComposer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BubbleChartOptionsComposer.swift; sourceTree = "<group>"; };
371+
84903C9C2DBA0BD20029FE5A /* AAChartView+ScreenRotation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AAChartView+ScreenRotation.swift"; sourceTree = "<group>"; };
370372
84A9FB7C28C9BC0E00240C11 /* AAScatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AAScatter.swift; sourceTree = "<group>"; };
371373
84AE6B1E26A58380001F0755 /* AADateUTCTool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AADateUTCTool.swift; sourceTree = "<group>"; };
372374
84B68245266DB09100957FC5 /* AAChartSymbolConstant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AAChartSymbolConstant.swift; sourceTree = "<group>"; };
@@ -432,6 +434,7 @@
432434
275BB8D720CFA6DB00DE1991 /* AdditionalContent1 */ = {
433435
isa = PBXGroup;
434436
children = (
437+
84903C9C2DBA0BD20029FE5A /* AAChartView+ScreenRotation.swift */,
435438
8466E3962DB1FD8600DBF03B /* ChartGalleryList */,
436439
2770FBBE1F93673700091C75 /* OnlyRefreshChartDataVC.swift */,
437440
276A56FE22C77228007F53E7 /* DoubleChartsLinkedWorkVC.swift */,
@@ -1098,6 +1101,7 @@
10981101
58603B0D623EDFE55CFFC66B /* CustomStyleForColumnChartComposer.swift in Sources */,
10991102
844007702B6F37180072FB66 /* CustomStyleForBubbleChartComposer.swift in Sources */,
11001103
586031CD65041F30252BD805 /* CustomStyleForBarChartComposer.swift in Sources */,
1104+
84903C9D2DBA0BD20029FE5A /* AAChartView+ScreenRotation.swift in Sources */,
11011105
84526E2F2CF414F500EB2563 /* JSFunctionBeforeAndAfterRenderingComposer4.swift in Sources */,
11021106
84903C9B2DB9F0680029FE5A /* BubbleChartOptionsComposer.swift in Sources */,
11031107
586034DF506E8FFB33BFE95A /* CustomStyleForBarChartVC.swift in Sources */,
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)