Skip to content

William-Weng/WWWebView_ChartJS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WWWebView+ChartJS

Swift-5.7 iOS-15.0 TAG Swift Package Manager-SUCCESS LICENSE

dependencies: [
    .package(url: "https://github.com/William-Weng/WWWebView_ChartJS.git", .upToNextMajor(from: "0.6.3"))
]
函式 功能
configure(delegate:chartType:defaultColor:) 相關設定
reloadData() 重新載入資料
reload() 重新載入網頁
resize() 重新設定畫面大小

WWWebView.ChartJS.Delegate

函式 功能
chartValuesArray(view:) 取得表格數據
chartViewStatus(_ view:result:) 表格狀態
chartViewEvent(_ view:result:) 表格事件
import UIKit
import WWWebView_ChartJS

class MyChartJS: WWWebView.ChartJS {}

final class ViewController: UIViewController {
    
    @IBOutlet weak var chartView: MyChartJS!
    
    private var chartValues: [WWWebView.ChartJS.ChartValue] = [
        (key: "Red", value: 15.0, color: .red.withAlphaComponent(0.8)),
        (key: "Blue", value: 10.0, color: nil),
        (key: "Yellow", value: 5.5, color: .yellow),
        (key: "Green", value: 8.0, color: .green.withAlphaComponent(0.3)),
        (key: "Purple", value: 12.0, color: .purple.withAlphaComponent(0.5)),
        (key: "Orange", value: 10.4, color: .orange.withAlphaComponent(0.7)),
    ]
    
    @IBAction func initBarChart(_ sender: UIBarButtonItem) {
        chartView.configure(delegate: self, chartType: .bar)
    }
    
    @IBAction func initPieChart(_ sender: UIBarButtonItem) {
        chartView.configure(delegate: self, chartType: .pie)
    }
    
    @IBAction func initDoughnutChart(_ sender: UIBarButtonItem) {
        chartView.configure(delegate: self, chartType: .doughnut)
    }
    
    @IBAction func reloadData(_ sender: UIBarButtonItem) {
        
        chartValues = [
            (key: "Red", value: 25.0, color: .red),
            (key: "Blue", value: 10.3, color: .blue),
            (key: "Yellow", value: 8.5, color: .yellow),
            (key: "Purple", value: 13.6, color: .purple),
        ]
        
        chartView.reloadData()
    }
}

extension ViewController: WWWebView.ChartJS.Delegate {
    
    func chartValuesArray(view: WWWebView.ChartJS) -> [[WWWebView.ChartJS.ChartValue]] {
        return [chartValues]
    }
        
    func chartViewStatus(_ view: WWWebView.ChartJS, result: Result<WWWebView.ChartJS.Status, Error>) {
        
        switch result {
        case .failure(let error): print(error)
        case .success(let status): print(status)
        }
    }
    
    func chartViewEvent(_ view: WWWebView.ChartJS, result: Result<WWWebView.ChartJS.Event, any Error>) {
        
        switch result {
        case .failure(let error): print(error)
        case .success(let event):
            switch event {
            case .itemTouched(let indexPath): title = chartValues[indexPath.row].key
            case .resize(let isLandscape): DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { view.resize(); view.isScrollEnabled = isLandscape }
            }
        }
    }
}

About

By loading Chart.js through WebView, the chart function can be quickly implemented.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors