Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AAChartModel committed Jan 30, 2024
1 parent 888d067 commit 0163b4e
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 2 deletions.
4 changes: 4 additions & 0 deletions AAInfographicsDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
8418479928B8755900094106 /* JSFunctionForAAChartEventsVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8418479828B8755900094106 /* JSFunctionForAAChartEventsVC.swift */; };
844614D7283C822E00E26DA4 /* ProjectBundlePathLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = D01D701A2587EE730093C01F /* ProjectBundlePathLoader.swift */; };
844F11402B635C9900ADEB1C /* JSFunctionForAAChartEventsVC2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844F113F2B635C9900ADEB1C /* JSFunctionForAAChartEventsVC2.swift */; };
844F11462B689B8600ADEB1C /* CustomXAxisLabelsClickEventCallbackVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844F11452B689B8600ADEB1C /* CustomXAxisLabelsClickEventCallbackVC.swift */; };
84595DE82B4698D600F69273 /* PolarChartOptionsVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84595DE72B4698D600F69273 /* PolarChartOptionsVC.swift */; };
84595DEA2B46A94C00F69273 /* AAOptionsListVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84595DE92B46A94C00F69273 /* AAOptionsListVC.swift */; };
84595DEC2B46B34E00F69273 /* AAOptionsWithJSListVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84595DEB2B46B34E00F69273 /* AAOptionsWithJSListVC.swift */; };
Expand Down Expand Up @@ -242,6 +243,7 @@
8418479828B8755900094106 /* JSFunctionForAAChartEventsVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSFunctionForAAChartEventsVC.swift; sourceTree = "<group>"; };
844614D3283C818D00E26DA4 /* PackageBundlePathLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PackageBundlePathLoader.swift; sourceTree = "<group>"; };
844F113F2B635C9900ADEB1C /* JSFunctionForAAChartEventsVC2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSFunctionForAAChartEventsVC2.swift; sourceTree = "<group>"; };
844F11452B689B8600ADEB1C /* CustomXAxisLabelsClickEventCallbackVC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CustomXAxisLabelsClickEventCallbackVC.swift; path = AAInfographicsDemo/Demo/BackUpContent/CustomXAxisLabelsClickEventCallbackVC.swift; sourceTree = SOURCE_ROOT; };
84595DE72B4698D600F69273 /* PolarChartOptionsVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PolarChartOptionsVC.swift; sourceTree = "<group>"; };
84595DE92B46A94C00F69273 /* AAOptionsListVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AAOptionsListVC.swift; sourceTree = "<group>"; };
84595DEB2B46B34E00F69273 /* AAOptionsWithJSListVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AAOptionsWithJSListVC.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -313,6 +315,7 @@
2763384424A7070F00143B9B /* DataSortingWithAnimationChartVC.swift */,
448330B724E270FE00368A30 /* AdvancedUpdatingFeatureVC.swift */,
84DEA0D72846013F00D0206F /* CustomTouchEndEventCallbackVC.swift */,
844F11452B689B8600ADEB1C /* CustomXAxisLabelsClickEventCallbackVC.swift */,
);
path = AdditionalContent1;
sourceTree = "<group>";
Expand Down Expand Up @@ -768,6 +771,7 @@
84F21858292DF373007AA726 /* OfficialAreaChartVC.swift in Sources */,
278C54B21EE9472300770B97 /* AppDelegate.swift in Sources */,
4409BED021B8CC4900D79278 /* EvaluateJSStringFunctionVC.swift in Sources */,
844F11462B689B8600ADEB1C /* CustomXAxisLabelsClickEventCallbackVC.swift in Sources */,
84DEB8A028F7A58900BF9423 /* JSFunctionForAATooltipVC2.swift in Sources */,
84086B2128E6F499006BD30D /* JSFunctionForAAOptionsVC.swift in Sources */,
84086B1B28E45127006BD30D /* JSFunctionForAAAxisVC.swift in Sources */,
Expand Down
15 changes: 13 additions & 2 deletions AAInfographicsDemo/Demo/AAChartListVC/AdvancedFeaturesListVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class AdvancedFeaturesListVC: UIViewController {
"Chart Options Advanced Updating | 图表高级更新",
"XIB AAChartView | 在 XIB 中创建 AAChartView",
"Custom Chart Event Callback | 自定义交互事件回调",
"Custom X Axis Labels Click Event Callback | 自定义 X 轴文字点击事件回调",
]

chartTypeTitleArr = [
Expand Down Expand Up @@ -150,7 +151,11 @@ class AdvancedFeaturesListVC: UIViewController {
],
/*Custom event callback*/
[
"自定义监听触摸结束事件---CustomTouchEndEventCallbackVC"
"自定义监听触摸结束事件回调---CustomTouchEndEventCallbackVC"
],
/*CustomXAxisLabelsClickEventCallbackVC*/
[
"自定义X轴文字点击事件回调---CustomXAxisLabelsClickEventCallbackVC"
],


Expand Down Expand Up @@ -469,8 +474,14 @@ extension AdvancedFeaturesListVC: UITableViewDelegate, UITableViewDataSource {
vc.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(vc, animated: true)

case 11:
/*CustomXAxisLabelsClickEventCallbackVC*/
let vc = CustomXAxisLabelsClickEventCallbackVC()
vc.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(vc, animated: true)




default:
break
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//
// CustomAnyEventCallback.swift
// AAInfographicsDemo
//
// Created by Admin on 2022/5/31.
// Copyright © 2022 An An. All rights reserved.
//

import AAInfographics
import WebKit

//https://github.com/AAChartModel/AAChartKit/issues/1531
//https://stackoverflow.com/questions/42062016/in-high-chart-how-to-add-event-for-label-click
class CustomXAxisLabelsClickEventCallbackVC: UIViewController {
let kUserContentMessageNameXAxisLabelsClick = "XAxisLabelsClick"

private var aaChartView: AAChartView!

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .white

configureChartView()
configureChartViewCustomEventMessageHandler()

let aaOptions = configureChartOptions()
aaChartView.aa_drawChartWithChartOptions(aaOptions)
}

private func configureChartView() {
aaChartView = AAChartView()
let chartViewWidth = view.frame.size.width
let chartViewHeight = view.frame.size.height - 220
aaChartView!.frame = CGRect(x: 0, y: 60, width: chartViewWidth, height: chartViewHeight)
aaChartView!.isScrollEnabled = false//Disable chart content scrolling
aaChartView!.isClearBackgroundColor = true
aaChartView!.delegate = self as AAChartViewDelegate
view.addSubview(aaChartView!)
}

private func configureChartViewCustomEventMessageHandler() {
aaChartView!.configuration.userContentController.add(AALeakAvoider.init(delegate: self), name: kUserContentMessageNameXAxisLabelsClick)
}

private func configureChartOptions() -> AAOptions {
AAOptions()
.chart(AAChart()
.type(.areaspline)
.events(AAChartEvents()
.load("""
function() {
const childNodes = this.xAxis[0].labelGroup.element.childNodes;
childNodes
.forEach(function(label, index) {
label.style.cursor = "pointer";
label.onclick = function() {
const message = {
category: this.textContent,
index: index,
};
window.webkit.messageHandlers.\(kUserContentMessageNameXAxisLabelsClick).postMessage(message);
}
});
}
""")))
.xAxis(AAXAxis()
.categories(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]))
.series([
AASeriesElement()
.data([7.0, 6.9, 2.5, 14.5, 18.2, 21.5, 5.2, 26.5, 23.3, 45.3, 13.9, 9.6])
.marker(AAMarker()
.lineColor(AAColor.red)
.lineWidth(3)
.radius(10))
])
}
}


extension CustomXAxisLabelsClickEventCallbackVC: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == kUserContentMessageNameXAxisLabelsClick {
let messageBody = message.body as! [String: Any]
let category = messageBody["category"] as? String
let index = messageBody["index"] as? Int

print(
"""
👇🏻👇🏻👇🏻------------------------------------------------------------------------------------------
clicked X axis label category: \(category ?? "")
message = {
category: \(category ?? ""),
index: \(index ?? 0),
};
👆👆👆------------------------------------------------------------------------------------------
"""
)
}
}
}


extension CustomXAxisLabelsClickEventCallbackVC: AAChartViewDelegate {
open func aaChartViewDidFinishLoad(_ aaChartView: AAChartView) {
print("🚀🚀🚀, AAChartView Did Finished Load!!!")
}

open func aaChartView(_ aaChartView: AAChartView, clickEventMessage: AAClickEventMessageModel) {
print(
"""
clicked point series element name: \(clickEventMessage.name ?? "")
🖱🖱🖱WARNING!!!!!!!!!!!!!!!!!!!! Click Event Message !!!!!!!!!!!!!!!!!!!! WARNING🖱🖱🖱
------------------------------------------------------------------------------------------
user finger moved over!!!,get the move over event message: {
category = \(String(describing: clickEventMessage.category))
index = \(String(describing: clickEventMessage.index))
name = \(String(describing: clickEventMessage.name))
offset = \(String(describing: clickEventMessage.offset))
x = \(String(describing: clickEventMessage.x))
y = \(String(describing: clickEventMessage.y))
}
------------------------------------------------------------------------------------------
"""
)
}

}



0 comments on commit 0163b4e

Please sign in to comment.