在 RadarChartData 设置标签

Set labels at RadarChartData

我正在使用 danielgindi 的图表 v3.2.2 框架为 iOS 和 macOS 绘制 RadarChartView。有一个 github repository 提供了包含 Playgrounds 的示例 xcode 项目。一个是 RadarChartView.

按照示例,我可以用两个不同的 RadarChartDataSets

设置 RadarChartData
var chartView = RadarChartView(frame: rect)
let data = RadarChartData(dataSets: [set1, set2])
chartView.data = data

它显示了这样的图表:

x轴设置的绿色标签是0.0到4.0之间的数字,但应该是字符串标签。

我不知道如何设置这些标签,这些标签应该画在每行网页末尾的 RadarChart 周围。我想应该是这样的:

data.setLabels("London", "Paris", "Berlin", "New York", "Tokio")

但这不起作用,尽管它是 class RadarChartData 的一项功能,可以设置所需的标签。

有人可以帮我解决这个问题吗?

编辑:完整代码示例

import Cocoa
import Charts
import PlaygroundSupport

let r = CGRect(x: 0, y: 0, width: 400, height: 400)
var chartView = RadarChartView(frame: r)
// General settings
chartView.webColor = NSUIColor.lightGray
chartView.innerWebColor = NSUIColor.lightGray
chartView.webAlpha = 1.0
// xAxis settings
let xAxis = chartView.xAxis
xAxis.xOffset = 0.0
xAxis.yOffset = 0.0
xAxis.labelTextColor = NSUIColor.green
xAxis.drawLabelsEnabled = true

// yAxis settings
let yAxis = chartView.yAxis
yAxis.labelCount = 5
yAxis.axisMinimum = 0.0
yAxis.axisMaximum = 80.0
yAxis.drawLabelsEnabled = true
// Legend settings
let legend = chartView.legend
// ... (irrelevant)

// Description
chartView.chartDescription?.enabled = true
chartView.chartDescription?.text = "Radar demo"
chartView.chartDescription?.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)

// RadarChartDataEntry
let mult = 80.0
let min = 20.0
let cnt = 5

var entries1 = [RadarChartDataEntry]()
var entries2 = [RadarChartDataEntry]()

for i in 1...cnt
{
  let values1 = (Double(arc4random_uniform(UInt32(mult))) + min)
  entries1.append(RadarChartDataEntry(value: values1, data: "a" as AnyObject))
  let values2 = (Double(arc4random_uniform(UInt32(mult))) + min)
  entries2.append(RadarChartDataEntry(value: values2, data: "b" as AnyObject))
}

// RadarChartDataSet
let set1 = RadarChartDataSet(entries: entries1, label: "Last Week")
set1.drawFilledEnabled = true
set1.fillAlpha = 0.7
set1.lineWidth = 2.0
set1.drawHighlightCircleEnabled = true
set1.setDrawHighlightIndicators(false)

let set2 = RadarChartDataSet(entries: entries2, label: "This Week")
set2.drawFilledEnabled = true
set2.fillAlpha = 0.7
set2.lineWidth = 2.0
set2.drawHighlightCircleEnabled = true
set2.setDrawHighlightIndicators(false)

// RadarChartData
let data = RadarChartData(dataSets: [set1, set2])
data.setLabels("London", "Paris", "Berlin", "New York", "Tokio")
data.setDrawValues ( true )
data.setValueTextColor(  NSUIColor.white )
chartView.data = data
chartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)

// show chartView
PlaygroundPage.current.liveView = chartView

您需要覆盖 IAxisValueFormatter func stringForValue(_ value: Double, axis: AxisBase?) -> String {} 函数。

如下所示:

第 1 步:使用自定义格式化程序自定义 xAxis。

let xValues = ["X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10"]
let chartFormatter = RadarChartFormatter(labels: xValues)
let xAxis = XAxis()
xAxis.valueFormatter = chartFormatter
self.xAxis.valueFormatter = xAxis.valueFormatter

第 2 步:使用以下方法实现自定义格式化程序。

private class RadarChartFormatter: NSObject, IAxisValueFormatter {
    
    var labels: [String] = []
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        if Int(value) < labels.count {
            return labels[Int(value)]
        }else{
            return String("")
        }
    }
    
    init(labels: [String]) {
        super.init()
        self.labels = labels
    }
}

您将在 RadarCharView 中获得以下输出:

希望这能帮助您在雷达图上获得自定义标签!