1
votes

I would like to draw radar charts with iOS Charts library by danielgindi, but I cannot figure out how to use options, especially the options regarding the axis.

It does work usually, but when I set the list of variables (array) as [100.0, 100.0, 100.0, 100.0, 100.0], it shows too zoomed a chart like this:

Instead, when I set array as [90.0, 80.0, 90.0, 70.0, 20.0], the interval of the grid lines is not 20 in spite of my code:

Judging from these outputs, the cause of the problem seems to be the options for the axis. However, I could not to solve this problem myself.
How do you fix the size of the chart and the range of the axis, and draw grid lines just as you like?

I use Xcode 8.1 / swift 2.3 / Charts 2.3.1.

import UIKit
import Charts

class ViewController: UIViewController {

@IBOutlet weak var radarChartView: RadarChartView!

//Label
let subjects = ["English", "Math", "Physics", "Chemistry", "Biology"]
//Points
let array = [100.0, 100.0, 100.0, 100.0, 100.0]

override func viewDidLoad() {
    super.viewDidLoad()
    setChart(subjects, values: array)
    print("array:")
    print(array)
}

func setChart(dataPoints: [String], values: [Double]) {

    radarChartView.noDataText = "You need to provide data for the chart."
    var dataEntries: [ChartDataEntry] = []
    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }
    let chartDataSet = RadarChartDataSet(yVals: dataEntries, label: "Units Sold")
    let chartData = RadarChartData(xVals: subjects, dataSet: chartDataSet)
    radarChartView.data = chartData

    //Options of radarChart
    radarChartView.sizeToFit()
    radarChartView.descriptionText = ""

    //Options for the axis from here. The range is 0-100, the interval is 20
    radarChartView.yAxis.labelCount = 6
    radarChartView.yAxis.axisMinValue = 0.0
    radarChartView.yAxis.axisMaxValue = 100.0

    radarChartView.rotationEnabled = false
    chartDataSet.drawFilledEnabled = true

    //Present the number as integer
    let numberFormatter = NSNumberFormatter()
    numberFormatter.generatesDecimalNumbers = false
    chartDataSet.valueFormatter = numberFormatter
    radarChartView.yAxis.valueFormatter = numberFormatter

    //Other options
    radarChartView.legend.enabled = false
    radarChartView.yAxis.gridAntialiasEnabled = true
    radarChartView.animate(yAxisDuration: 2.0)
}
1

1 Answers

1
votes

I just had to set data AFTER options. picture

func setChart(dataPoints: [String], values: [Double]) {
    radarChartView.noDataText = "You need to provide data for the chart."
    var dataEntries: [ChartDataEntry] = []
    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }
    let chartDataSet = RadarChartDataSet(yVals: dataEntries, label: "Units Sold")

    //Set options...

    //Then set data
    let chartData = RadarChartData(xVals: subjects, dataSet: chartDataSet)
}