Skip to content

Commit

Permalink
feat: add chart list demos
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqingtangx committed Jun 9, 2024
1 parent 26859bc commit 9d50b91
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 11 deletions.
4 changes: 4 additions & 0 deletions demos/ios/Demos-Swift/Demos-Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
CD2657002C1622C3002D70CC /* HistogramBarChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2656FF2C1622C3002D70CC /* HistogramBarChart.swift */; };
CD2657022C162334002D70CC /* BasePointChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2657012C162334002D70CC /* BasePointChart.swift */; };
CD2657042C1623D4002D70CC /* RadarAreaChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2657032C1623D4002D70CC /* RadarAreaChart.swift */; };
CD2657062C162533002D70CC /* ChartListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2657052C162533002D70CC /* ChartListViewController.swift */; };
D1093B56324A12388FFA958C /* libPods-Demos-Swift.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B6B0DAB8161155F20647AC22 /* libPods-Demos-Swift.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -71,6 +72,7 @@
CD2656FF2C1622C3002D70CC /* HistogramBarChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistogramBarChart.swift; sourceTree = "<group>"; };
CD2657012C162334002D70CC /* BasePointChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasePointChart.swift; sourceTree = "<group>"; };
CD2657032C1623D4002D70CC /* RadarAreaChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RadarAreaChart.swift; sourceTree = "<group>"; };
CD2657052C162533002D70CC /* ChartListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChartListViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -119,6 +121,7 @@
CC79F203285AB18100A5623C /* AppDelegate.swift */,
CC79F207285AB18100A5623C /* ViewController.swift */,
CC79F251285ADA2300A5623C /* DemoViewController.swift */,
CD2657052C162533002D70CC /* ChartListViewController.swift */,
CC79F20C285AB18300A5623C /* Assets.xcassets */,
CC79F20E285AB18300A5623C /* LaunchScreen.storyboard */,
CC79F211285AB18300A5623C /* Info.plist */,
Expand Down Expand Up @@ -281,6 +284,7 @@
CD2656F02C161B98002D70CC /* ContrastLineChart.swift in Sources */,
CD2656EC2C161310002D70CC /* CandleChart.swift in Sources */,
CD2656E82C160C2A002D70CC /* BaseAreaChart.swift in Sources */,
CD2657062C162533002D70CC /* ChartListViewController.swift in Sources */,
CD2656FA2C16212C002D70CC /* GroupBarChart.swift in Sources */,
CD2656FE2C162228002D70CC /* GroupStackBarChart.swift in Sources */,
CC79F252285ADA2300A5623C /* DemoViewController.swift in Sources */,
Expand Down
96 changes: 96 additions & 0 deletions demos/ios/Demos-Swift/Demos-Swift/ChartListViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// ChartListViewController.swift
// F2Native
//
// Created by Westin on 2024-06-09.
//

import UIKit

class ChartListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

private lazy var demoTable: UITableView = {
let table = UITableView(frame: self.view.bounds)
table.delegate = self
table.dataSource = self
table.backgroundColor = .white

#if targetEnvironment(macCatalyst)
let pan = UIPanGestureRecognizer(target: self, action: #selector(onPan(_:)))
table.addGestureRecognizer(pan)
#endif

return table
}()

private var s0: CGPoint = .zero

override func viewDidLoad() {
super.viewDidLoad()

self.edgesForExtendedLayout = UIRectEdge.init(rawValue:0);
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance.init()
appearance.configureWithOpaqueBackground()
self.navigationController?.navigationBar.standardAppearance = appearance
self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
}
self.navigationController?.navigationBar.backgroundColor = .white
self.title = "ChartList"

self.view.addSubview(self.demoTable)
}

@objc func onPan(_ pan: UIPanGestureRecognizer) {
let point = pan.translation(in: self.demoTable)
if pan.state == .began {
s0 = demoTable.contentOffset
} else if pan.state == .changed {
demoTable.contentOffset = CGPoint(x: s0.x, y: max(s0.y - point.y, 0))
}
}

// MARK: - UITableViewDelegate & UITableViewDataSource

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ViewController.demoInfo().count
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "demoCell"
var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
if cell == nil {
cell = UITableViewCell(style: .value1, reuseIdentifier: identifier)
cell?.selectionStyle = .none
} else {
cell?.contentView.viewWithTag(100)?.removeFromSuperview()
}
cell?.backgroundColor = .white
cell?.textLabel?.textColor = .black
cell?.contentView.addSubview(demoViewWithIndex(indexPath.row))
return cell!
}

func demoViewWithIndex(_ index: Int) -> UIView {
guard let dict = ViewController.demoInfo()[index] as? [String: Any],
let viewName = dict["view"] as? String else {
return UIView() // Return an empty view if class not found
}
var appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
appName = appName.replacingOccurrences(of: "-", with: "_")
let className = appName + "." + viewName;
let cls = NSClassFromString(className) as! UIView.Type
let view = cls.init(frame:CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 280))
view.tag = 100
return view
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 350.0
}
}

20 changes: 15 additions & 5 deletions demos/ios/Demos-Swift/Demos-Swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var demoTable: UITableView?
// Lazy initialization of the UIBarButtonItem in Swift
lazy var rightButton: UIBarButtonItem = {
let button = UIBarButtonItem(title: "ChartList", style: .done, target: self, action: #selector(onMultiChart))
return button
}()

required init() {
self.demoTable = nil;
Expand All @@ -29,10 +34,10 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
self.demoTable?.backgroundColor = UIColor.white;
self.demoTable?.separatorColor = UIColor.gray;
self.view.addSubview(self.demoTable!)

self.navigationItem.rightBarButtonItem = rightButton
}

func demoInfo() -> NSArray {
static func demoInfo() -> NSArray {
return [["type": "baseLine", "name": "Line Chart", "view": "BaseLineChart"],
["type": "multiAxiesLine", "name": "Line Chart(Dual Y-axis)", "view": "BaseLineChart2"],
["type": "multiLine", "name": "ContrastLineChart", "view": "ContrastLineChart"],
Expand All @@ -54,7 +59,7 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.demoInfo().count;
return ViewController.demoInfo().count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
Expand All @@ -66,15 +71,20 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
cell!.textLabel?.textColor = UIColor.black;
cell!.selectionStyle = UITableViewCell.SelectionStyle.none
}
let info: NSDictionary = self.demoInfo().object(at: indexPath.row) as! NSDictionary
let info: NSDictionary = ViewController.demoInfo().object(at: indexPath.row) as! NSDictionary
cell!.textLabel?.text = info.object(forKey: "name") as? String
return cell!;
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let info: NSDictionary = self.demoInfo().object(at: indexPath.row) as! NSDictionary
let info: NSDictionary = ViewController.demoInfo().object(at: indexPath.row) as! NSDictionary
let demoVC = DemoViewController.init(info: info)
self.navigationController?.pushViewController(demoVC, animated: true)
}

@objc func onMultiChart() {
let listCtr = ChartListViewController() // Assuming ChartListViewController is correctly imported
navigationController?.pushViewController(listCtr, animated: true)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class BaseAreaChart: BaseLineChart {

let height = canvasView.frame.size.height * UIScreen.main.scale
let jsonData = F2Utils.toJsonArray(jsonString)
chart.clear()
chart.canvas()(canvasView)
self.chart!.source()(jsonData)
chart.padding()(10, 20, 10, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BaseAreaChart2: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)

chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 0).source()(jsonData)
chart.axis()("tem", ["grid": ["stroke": "#000"]])
chart.line()().position()("month*value")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BaseAreaChart3: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)

chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 0).source()(jsonData)
chart.axis()("tem", ["grid": ["stroke": "#000"]])
chart.line()().position()("month*value")
Expand Down
1 change: 1 addition & 0 deletions demos/ios/Demos-Swift/Demos-Swift/Views/BaseBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BaseBarChart: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)
chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 10)
chart.source()(jsonData)
chart.axis()("year", ["grid": false])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BaseBarChart2: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonString)
self.chart!.clear()
self.chart!.canvas()(self.canvasView!)
self.chart!.padding()(10, 20, 10, 0)
self.chart!.source()(jsonData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BaseLineChart: UIView {
return
}
let jsonData = F2Utils.toJsonArray(jsonString)
self.chart?.clear()
self.chart!.canvas()(self.canvasView!)
self.chart?.padding()(10, 20, 10, 0)
self.chart!.source()(jsonData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BaseLineChart2: BaseLineChart {
}

let jsonData = F2Utils.toJsonArray(jsonDataString)

chart.clear()
chart.syncYScale()(false)
chart.canvas()(canvasView).padding()(20, 10, 20, 0).source()(jsonData)
chart.scale()("date", ["tickCount": 5])
Expand Down
3 changes: 2 additions & 1 deletion demos/ios/Demos-Swift/Demos-Swift/Views/BasePieChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class BasePieChart: BaseLineChart {
}

let jsonData = F2Utils.toJsonArray(jsonString)
chart.clear()
chart.canvas()(canvasView)
self.chart!.source()(jsonData)
chart.source()(jsonData)
chart.padding()(10, 20, 10, 0)
chart.axis()("percent", ["line": false, "label": false, "grid": false])
chart.axis()("a", ["line": false, "label": false, "grid": false])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BasePointChart: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)
chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 0)
chart.source()(jsonData)
chart.point()().position()("x*y").size()("z", [3, 10]).fixedShape()("circle")
Expand Down
2 changes: 2 additions & 0 deletions demos/ios/Demos-Swift/Demos-Swift/Views/CandleChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class CandleChart: UIView {
let jsonData = F2Utils.toJsonArray(jsonDataString)

// Setup for the main candlestick chart
candleChart.clear()
candleChart.canvas()(canvasView)
candleChart.source()(jsonData)
candleChart.padding()(15, 10, 15, 0)
Expand All @@ -64,6 +65,7 @@ class CandleChart: UIView {
candleChart.render()()

// Setup for the volume chart
subChart.clear()
subChart.adjustScale()(false);
subChart.canvas()(canvasView)
subChart.source()(jsonData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ContrastLineChart: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)

chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 0)
chart.source()(jsonData)
chart.scale()("value", ["nice": true])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GroupBarChart: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)
chart.clear()
chart.canvas()(canvasView).padding()(15, 15, 15, 15)
chart.source()(jsonData)
chart.axis()("month", ["grid": false])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GroupBarChart2: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)
chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 0)
chart.source()(jsonData)
chart.axis()("tem", ["grid": ["stroke": "#000"]])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GroupStackBarChart: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)
chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 0)
chart.source()(jsonData)
chart.axis()("month", ["grid": false])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class HistogramBarChart: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)
chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 0)
chart.source()(jsonData)
chart.axis()("x", ["grid": false])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class RadarAreaChart: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)
chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 0)
chart.source()(jsonData)
chart.line()().position()("item*score").color()("user", [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StackedAreaChart: BaseLineChart {
return
}
let jsonData = F2Utils.toJsonArray(jsonDataString)

chart.clear()
chart.canvas()(canvasView).padding()(20, 10, 20, 0)
chart.source()(jsonData)
chart.scale()("date", ["tickCount": 5])
Expand Down

0 comments on commit 9d50b91

Please sign in to comment.