๊ฐœ์š”

Radar ์ฐจํŠธ๋ฅผ  Charts ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“ค์–ด ๋ณด๋ คํ•œ๋‹ค.

 

Charts์—์„œ๋Š” ์ฐจํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„์ฒ˜๋Ÿผ ๋ฌถ๋Š”๋‹ค.

 

๋ณธ๋ก 

// 1
let greenDataSet = RadarChartDataSet(
    entries: [
        RadarChartDataEntry(value: 210),
        RadarChartDataEntry(value: 60.0),
        RadarChartDataEntry(value: 150.0),
        RadarChartDataEntry(value: 150.0),
        RadarChartDataEntry(value: 160.0),
        RadarChartDataEntry(value: 150.0),
        RadarChartDataEntry(value: 110.0),
        RadarChartDataEntry(value: 190.0),
        RadarChartDataEntry(value: 200.0)
    ]
)
let redDataSet = RadarChartDataSet(
    entries: [
        RadarChartDataEntry(value: 120.0),
        RadarChartDataEntry(value: 160.0),
        RadarChartDataEntry(value: 110.0),
        RadarChartDataEntry(value: 110.0),
        RadarChartDataEntry(value: 210.0),
        RadarChartDataEntry(value: 120.0),
        RadarChartDataEntry(value: 210.0),
        RadarChartDataEntry(value: 100.0),
        RadarChartDataEntry(value: 210.0)
    ]
)

// 2
let data = RadarChartData(dataSets: [greenDataSet, redDataSet])

// 3
radarChart.data = data

// 1
๋จผ์ € ์ฐจํŠธ์— ํ‘œ์‹œํ•  ๋ฐ์ดํ„ฐํ•ญ๋ชฉ์„ ํฌํ•จํ•˜๋Š” ๋‘๊ฐ€์ง€ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ ๋‹ค.

์ฒซ๋ฒˆ์งธ๋Š” ๋…น์ƒ‰ ์˜์—ญ์— ๋Œ€ํ•œ ํฌ์ธํŠธ๋ฅผ ํฌํ•จํ•˜๊ณ  ๋‘๋ฒˆ์งธ๋Š” ๋นจ๊ฐ„์ƒ‰ ์˜์—ญ์— ๋Œ€ํ•œ ํฌ์ธํŠธ๋ฅผ ํฌํ•จํ•œ๋‹ค.

์ด ์˜ˆ์‹œ์—์„œ ๊ฐ๊ฐ์€ Double๊ฐ’์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์ง€๋งŒ ์•„์ด์ฝ˜์„ ์ถ”๊ฐ€ํ•˜๊ณ  ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์ž„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

// 2

๋‘ dataSet์„ ์ƒ์„ฑ์ž๋กœ ์ „๋‹ฌํ•˜์—ฌ ๋ž˜ํ•‘ํ•œ๋‹ค.

 

// 3
๋งˆ์ง€๋ง‰์œผ๋กœ ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด์— ์†์„ฑ์„ ์„ค์ •ํ•œ๋‹ค.

 

DataSet ๋ ˆ์ด์•„์›ƒ ์‚ฌ์šฉ์ž์ง€์ •

// 1
redDataSet.lineWidth = 2

// 2
let redColor = UIColor(red: 247/255, green: 67/255, blue: 115/255, alpha: 1)
let redFillColor = UIColor(red: 247/255, green: 67/255, blue: 115/255, alpha: 0.6)
redDataSet.colors = [redColor]
redDataSet.fillColor = redFillColor
redDataSet.drawFilledEnabled = true

// 3
redDataSet.valueFormatter = DataSetValueFormatter()

// 1

๋จผ์ € ์†์„ฑ์„ lineWidth ๊ธฐ๋ณธ๊ฐ’์—์„œ 2๋กœ ๋Š˜๋ฆฐ๋‹ค.

์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์„ ์˜ Width๋ฅผ ๋Š˜๋ ธ๋‹ค.

 

// 2

๋นจ๊ฐ„์ƒ‰์œผ๋กœ ์ปค์Šคํ…€ํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋“ค

drawFilledEnabled ์†์„ฑ์„ true๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฐฉ์‚ฌํ˜• ์ฐจํŠธ ๋‚ด๋ถ€๊ฐ€ fillColor๋กœ ์น ํ•ด์ง„๋‹ค.

 

// 3

valueFormatter๋Š” ์•„๋ž˜์—์„œ ์„ค๋ช… ์˜ˆ์ •

 

 

์ฐจํŠธ ๋ทฐ ์†์„ฑ ๊ตฌ์„ฑ

// 1
@IBOutlet weak var radarChart: RadarChartView!

// 2
radarChart.webLineWidth = 1.5
radarChart.innerWebLineWidth = 1.5
radarChart.webColor = .lightGray
radarChart.innerWebColor = .lightGray

// 3
let xAxis = radarChart.xAxis
xAxis.labelFont = .systemFont(ofSize: 9, weight: .bold)
xAxis.labelTextColor = .black
xAxis.xOffset = 10
xAxis.yOffset = 10
xAxis.valueFormatter = XAxisFormatter()

// 4
let yAxis = radarChart.yAxis
yAxis.labelFont = .systemFont(ofSize: 9, weight: .light)
yAxis.labelCount = 6
yAxis.drawTopYLabelEntryEnabled = false
yAxis.axisMinimum = 0
yAxis.valueFormatter = YAxisFormatter()

// 5
radarChart.rotationEnabled = false
radarChart.legend.enabled = false

 

Formatter

// 1
class DataSetValueFormatter: IValueFormatter {
    
    func stringForValue(_ value: Double,
                        entry: ChartDataEntry,
                        dataSetIndex: Int,
                        viewPortHandler: ViewPortHandler?) -> String {
        ""
    }
}

// 2
class XAxisFormatter: IAxisValueFormatter {
    
    let titles = "ABCDEFGHI".map { "Party \($0)" }
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        titles[Int(value) % titles.count]
    }
}

// 3
class YAxisFormatter: IAxisValueFormatter {
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        "\(Int(value)) $"
    }
}

 

 

SwiftUI์—์„œ์˜ ์‚ฌ์šฉ

// 1
struct RadarView: UIViewRepresentable {
        
    // 2
    typealias UIViewType = RadarChartView
        
    // 3
    func makeUIView(context: UIViewRepresentableContext<RadarView>) -> RadarChartView {
        let radarChart = RadarChartView()
        // Setup radar Chart
        return radarChart
    }
    
    // 4
    @State var data: RadarChartData
    func updateUIView(_ uiView: RadarChartView, context: UIViewRepresentableContext<RadarView>) {
        uiView.data = data
    }
}

 

Ref

https://betterprogramming.pub/creating-a-radar-chart-in-swift-5791afcf92f0

 

Create a Radar Chart in Swift

Plotting data with the framework, Charts

betterprogramming.pub

https://github.com/danielgindi/Charts

 

GitHub - danielgindi/Charts: Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart. - GitHub - danielgindi/Charts: Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroi...

github.com

 

๋ฐ˜์‘ํ˜•
JerryiOS