swift -charts框架雷达图

参考资料

import UIKit
import Charts

class ViewController: UIViewController {
    
    let activities = ["力量", "敏捷", "生命", "智力", "魔法"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let values1: [Double] = [58, 61, 81, 93, 52];
        
        var entries1: [ChartDataEntry] = Array()
 
        
        for (_, value) in values1.enumerated()
        {
            entries1.append(RadarChartDataEntry(value: value))
        }
        

        
        let dataSet = RadarChartDataSet(values: entries1, label: "50")
        // 线条以及填充颜色
        dataSet.setColor(NSUIColor.green)
        dataSet.lineWidth = 2
        dataSet.fillColor = NSUIColor.green
        dataSet.fillAlpha = 0.2
        dataSet.drawFilledEnabled = true
        
        let chart = RadarChartView(frame: CGRect(x: 0, y: 200, width: 320, height: 280))
        chart.backgroundColor = NSUIColor.clear
        // 上边距
        chart.extraTopOffset = 50;
        // 可以设置多个dataset
        chart.data = RadarChartData(dataSets: [dataSet])
        chart.xAxis.labelPosition = .bottom
        // 下面的提示按钮不显示 none 为不显示下面的提示
        // circle 圆圈
        chart.legend.form = .circle
        // 显示的位置
        chart.legend.horizontalAlignment = .center;
        // 重绘边边角角的文本
        chart.xAxis.valueFormatter = self
        self.view.addSubview(chart)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController: IAxisValueFormatter {
    //维度标签文字(x轴文字)
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return activities[Int(value) % activities.count]
    }
}

swift -charts框架雷达图_第1张图片

你可能感兴趣的:(Swift,奇淫技巧,Swift,快速开发技巧)