Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AAChartModel committed Aug 2, 2022
1 parent 1525606 commit 4214c18
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 35 deletions.
4 changes: 4 additions & 0 deletions AAInfographics-ProDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
84A5694D27FEC89F00883553 /* vectorData.json in Resources */ = {isa = PBXBuildFile; fileRef = 84A5692F27FEC89F00883553 /* vectorData.json */; };
84A5694E27FEC89F00883553 /* arcdiagram3Data.json in Resources */ = {isa = PBXBuildFile; fileRef = 84A5693027FEC89F00883553 /* arcdiagram3Data.json */; };
84B13EE92897A953007356C6 /* AAItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B13EE82897A953007356C6 /* AAItem.swift */; };
84B13EEB28990388007356C6 /* CustomClickEventCallbackMessageVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B13EEA28990388007356C6 /* CustomClickEventCallbackMessageVC.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -204,6 +205,7 @@
84A5692F27FEC89F00883553 /* vectorData.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = vectorData.json; sourceTree = "<group>"; };
84A5693027FEC89F00883553 /* arcdiagram3Data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = arcdiagram3Data.json; sourceTree = "<group>"; };
84B13EE82897A953007356C6 /* AAItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AAItem.swift; sourceTree = "<group>"; };
84B13EEA28990388007356C6 /* CustomClickEventCallbackMessageVC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomClickEventCallbackMessageVC.swift; sourceTree = "<group>"; };
84B2386B283F6D4C00193B9A /* PackageBundlePathLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PackageBundlePathLoader.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -345,6 +347,7 @@
children = (
44D27DA3255A64A700D0C636 /* MainVC.swift */,
44D27E10255ADF1300D0C636 /* ChartProVC.swift */,
84B13EEA28990388007356C6 /* CustomClickEventCallbackMessageVC.swift */,
44D27D7E255A631500D0C636 /* AABaseChartVC.swift */,
44D27E06255A793200D0C636 /* AAOptionsData.swift */,
44D27E0B255ADE1E00D0C636 /* AAOptionsSeries.swift */,
Expand Down Expand Up @@ -609,6 +612,7 @@
84B13EE92897A953007356C6 /* AAItem.swift in Sources */,
844615B6283F580300E26DA4 /* AALang.swift in Sources */,
844615A6283F580300E26DA4 /* AAMarker.swift in Sources */,
84B13EEB28990388007356C6 /* CustomClickEventCallbackMessageVC.swift in Sources */,
8441A76126C2568F007EAAA7 /* AAExtension.swift in Sources */,
844615B8283F580300E26DA4 /* AASubtitle.swift in Sources */,
44D27C77255A3CAF00D0C636 /* ViewController.swift in Sources */,
Expand Down
204 changes: 204 additions & 0 deletions AAInfographics-ProDemo/Demo/CustomClickEventCallbackMessageVC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//
// CustomAnyEventCallback.swift
// AAInfographicsDemo
//
// Created by Admin on 2022/8/2.
// Copyright © 2022 An An. All rights reserved.
//

import UIKit
import WebKit

let kUserContentMessageNameChartClicked = "click"

class CustomClickEventCallbackMessageVC: UIViewController {
private var aaChartView: AAChartView!
lazy var lineView: UIView = {
let lineView = UIView(frame: .zero)
lineView.backgroundColor = .red
self.view.addSubview(lineView)
return lineView
}()

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .white

configureChartView()
configureChartViewCustomEventMessageHandler()

let aaOptions = topRoundedCornersStackingColumnChart()
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)
view.addSubview(aaChartView!)
aaChartView!.isScrollEnabled = false//Disable chart content scrolling
}

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

private func topRoundedCornersStackingColumnChart() -> AAOptions {
func xrangeChart() -> AAOptions {
AAOptions()
.chart(AAChart()
.type(.xrange))
.colors([
"#7cb5ec","#434348","#90ed7d","#f7a35c","#8085e9",
"rgb(255,143,179)","rgb(255,117,153)",
"#f15c80","#e4d354","#2b908f","#f45b5b","#91e8e1","#7cb5ec","#434348","#f7a35c",
"rgb(119,212,100)","rgb(93,186,74)","rgb(68,161,49)"
])
.title(AATitle()
.text(""))
.yAxis(AAYAxis()
.visible(true)
.title(AATitle()
.text(""))
.reversed(true)
.categories(["原型","开发","测试","上线"])
.gridLineWidth(0))
.legend(AALegend()
.enabled(false))
.plotOptions(AAPlotOptions()
.series(AASeries()
.pointPadding(0)
.groupPadding(0)))
.series([
AASeriesElement()
.borderRadius(2)
.data(AAOptionsData.xrangeData)
])
}

let aaOptions = xrangeChart()

// 获取用户点击位置的代码逻辑, 参考:
// * https://www.highcharts.com/forum/viewtopic.php?t=11983
// * https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getBoundingClientRect

// JSON.stringify(), 参考:
// * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
aaOptions.plotOptions?.series?
.point(AAPoint()
.events(AAPointEvents()
.click("""
function() {
let svgElement = aaGlobalChart.series[0].data[this.index].graphic.element;
let rect = svgElement.getBoundingClientRect();
let messageBody = {
"name": this.series.name,
"y": this.y,
"x": this.x,
"category": this.category,
"index": this.index,
"DOMRect": JSON.stringify(rect),
};
window.webkit.messageHandlers.\(kUserContentMessageNameChartClicked).postMessage(messageBody);
}
""")))

return aaOptions
}

func convertJSValueToFloat(jsValue: Any?) -> Float {
var floatValue: Float = 0
if jsValue is String {
floatValue = Float(jsValue as! String)!
} else if jsValue is Int {
floatValue = Float(jsValue as! Int)
} else if jsValue is Float {
floatValue = (jsValue as! Float)
} else if jsValue is Double {
floatValue = Float(jsValue as! Double)
}
return floatValue
}

private func getEventMessageModel(DOMRectDic: [String: Any]) -> DOMRectModel {
let DOMRectModel = DOMRectModel()
DOMRectModel.x = convertJSValueToFloat(jsValue: DOMRectDic["x"])
DOMRectModel.y = convertJSValueToFloat(jsValue: DOMRectDic["y"])
DOMRectModel.width = convertJSValueToFloat(jsValue: DOMRectDic["width"])
DOMRectModel.height = convertJSValueToFloat(jsValue: DOMRectDic["height"])
DOMRectModel.top = convertJSValueToFloat(jsValue: DOMRectDic["top"])
DOMRectModel.right = convertJSValueToFloat(jsValue: DOMRectDic["right"])
DOMRectModel.bottom = convertJSValueToFloat(jsValue: DOMRectDic["bottom"])
DOMRectModel.left = convertJSValueToFloat(jsValue: DOMRectDic["left"])
return DOMRectModel
}
}

// MARK: 字符串转字典
func stringValueDic(_ str: String) -> [String : Any]?{
let data = str.data(using: String.Encoding.utf8)
if let dict = try? JSONSerialization.jsonObject(with: data!,
options: .mutableContainers) as? [String : Any] {
return dict
}

return nil
}

// 点击图表后, DOMRect 取值示例:
// DOMRect = "{\"x\":383.5,\"y\":608.5,\"width\":9,\"height\":32,\"top\":608.5,\"right\":392.5,\"bottom\":640.5,\"left\":383.5}";
class DOMRectModel {
public var x: Float?
public var y: Float?
public var width: Float?
public var height: Float?
public var top: Float?
public var right: Float?
public var bottom: Float?
public var left: Float?
}

extension CustomClickEventCallbackMessageVC: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == kUserContentMessageNameChartClicked {
let clickEventMessage = message.body as! [String: Any]
let DOMRectDic = stringValueDic(clickEventMessage["DOMRect"] as! String)!
let DOMRectModel = getEventMessageModel(DOMRectDic: DOMRectDic )

let frameX = DOMRectModel.x! + (DOMRectModel.width! / 2)
print("✋🏻✋🏻✋🏻✋🏻✋🏻点击图表后, 获取的 SVG 元素的水平中心点的坐标为:\(frameX)")
self.lineView.frame = CGRect(x: CGFloat(frameX), y: 0, width: 2, height: self.view.frame.size.height)

print(
"""
clicked point series element name: \(clickEventMessage["name"] ?? "")
🖱🖱🖱WARNING!!!!!!!!!!!!!!!!!!!! Click Event Message !!!!!!!!!!!!!!!!!!!! WARNING🖱🖱🖱
==========================================================================================
------------------------------------------------------------------------------------------
user finger CLICKED!!!,get the custom click event message: {
category = \(String(describing: clickEventMessage["category"]))
index = \(String(describing: clickEventMessage["index"]))
name = \(String(describing: clickEventMessage["name"]))
x = \(String(describing: clickEventMessage["x"]))
y = \(String(describing: clickEventMessage["y"]))
DOMRect = \(String(describing: DOMRectDic))
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
)

}
}
}



78 changes: 43 additions & 35 deletions AAInfographics-ProDemo/Demo/MainVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,49 @@ class MainVC: UIViewController {

sectionTitleArr = [
"Pro Type Chart---高级类型图表",
"Custom Event---自定义交互事件",
]

chartTypeTitleArr = [
[ "sankeyChart---桑基图",
"variablepieChart---可变宽度的饼图🍪",
"treemapChart---树形图🌲",
"variwideChart---可变宽度的柱形图📊",
"sunburstChart---旭日图🌞",
"dependencywheelChart---和弦图🎸",
"heatmapChart---热力图🔥",
"packedbubbleChart---气泡填充图🎈",
"packedbubbleSplitChart---圆堆积图🎈",
"vennChart---韦恩图",
"dumbbellChart---哑铃图🏋",
"lollipopChart---棒棒糖图🍭",
"streamgraphChart---流图🌊",
"columnpyramidChart---角锥柱形图△",
"tilemapChart---砖块图🧱||蜂巢图🐝",
"simpleTreemapChart---简单矩形树图🌲",
"drilldownTreemapChart---可下钻的矩形树图🌲",
"xrangeChart---X轴范围图||甘特图||条码图☰☲☱☴☵☶☳☷",
"vectorChart---向量图🏹",
"bellcurveChart---贝尔曲线图",
"timelineChart---时序图⌚️",
"itemChart---议会项目图",
"windbarbChart---风羽图",
"networkgraphChart---力导关系图✢✣✤✥",
"wordcloudChart---词云️图☁️",
"eulerChart---欧拉图",
"organizationChart---组织结构图",
"arcdiagramChart1---弧形图1",
"arcdiagramChart2---弧形图2",
"arcdiagramChart3---弧形图3",
"flameChart---火焰🔥图",
"packedbubbleSpiralChart---渐进变化的气泡🎈图",
"itemChart2---议会项目图2",
"itemChart3---议会项目图3",
[
"sankeyChart---桑基图",
"variablepieChart---可变宽度的饼图🍪",
"treemapChart---树形图🌲",
"variwideChart---可变宽度的柱形图📊",
"sunburstChart---旭日图🌞",
"dependencywheelChart---和弦图🎸",
"heatmapChart---热力图🔥",
"packedbubbleChart---气泡填充图🎈",
"packedbubbleSplitChart---圆堆积图🎈",
"vennChart---韦恩图",
"dumbbellChart---哑铃图🏋",
"lollipopChart---棒棒糖图🍭",
"streamgraphChart---流图🌊",
"columnpyramidChart---角锥柱形图△",
"tilemapChart---砖块图🧱||蜂巢图🐝",
"simpleTreemapChart---简单矩形树图🌲",
"drilldownTreemapChart---可下钻的矩形树图🌲",
"xrangeChart---X轴范围图||甘特图||条码图☰☲☱☴☵☶☳☷",
"vectorChart---向量图🏹",
"bellcurveChart---贝尔曲线图",
"timelineChart---时序图⌚️",
"itemChart---议会项目图",
"windbarbChart---风羽图",
"networkgraphChart---力导关系图✢✣✤✥",
"wordcloudChart---词云️图☁️",
"eulerChart---欧拉图",
"organizationChart---组织结构图",
"arcdiagramChart1---弧形图1",
"arcdiagramChart2---弧形图2",
"arcdiagramChart3---弧形图3",
"flameChart---火焰🔥图",
"packedbubbleSpiralChart---渐进变化的气泡🎈图",
"itemChart2---议会项目图2",
"itemChart3---议会项目图3",
],
[
"Custom Chart Click Event Message---自定义用户点击事件回调内容",
]
]

chartTypeArr = [
Expand Down Expand Up @@ -172,7 +177,10 @@ extension MainVC: UITableViewDelegate, UITableViewDataSource {
vc.selectedIndex = indexPath.row
vc.navigationItemTitleArr = self.chartTypeTitleArr[0]
navigationController?.pushViewController(vc, animated: true)

case 1:
let vc = CustomClickEventCallbackMessageVC()
navigationController?.pushViewController(vc, animated: true)

default:
break
}
Expand Down

0 comments on commit 4214c18

Please sign in to comment.