difficulties following “ How to Use iOS Charts API

2020-04-12 09:27发布

问题:

the AppCoda tutorial, dated 2015-06-23, guides you through finishing a starter project that doesnt draw its views to one that does. however, my project draws the example bar graph incorrectly; in the tutorial all the bars appear, and are labeled correctly. i placed my version of the project here IOChartsDemo.

this is what happens in my project

development environment:

  • macOS 12.5.5, Xcode 8.3.3 (8E3004b)
  • Charts 3.0.2

i downloaded the starter project indicated in the tutorial.

the starter project in the tutorial didnt build, there were api problems. after following the instructions in the tutorial, and correcting the errors, i end up with this (problem lines are commented out):

class BarChartViewController: UIViewController {
@IBOutlet weak var barChartView: BarChartView!

var months: [String]!

override func viewDidLoad() {
    super.viewDidLoad()
    // barChartView.noDataTextDescription = "Just because" type BarChartView doest have noDataTextDescription
    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

    // setChart(months, values: unitsSold) missing argument label dataPoints: in call
    setChart(dataPoints: months, values: unitsSold)
}

func setChart(dataPoints: [String], values: [Double]) {
    barChartView.noDataText = "You need to provide data for the chart."
    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<dataPoints.count {
        // let dataEntry = BarChartDataEntry(value: values[i], xIndex: i) // argument labeld dont match
        let dataEntry = BarChartDataEntry(x: values[i], y: Double(i), data: dataPoints[i] as AnyObject)

        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")

    // let chartData = BarChartData(xVals: months, dataSet: chartDataSet) cannot invoke initializer BarCharData with arg list of (xVals: [String]!, dataSet: BarCharDataSet
    let chartData    = BarChartData(dataSets: [chartDataSet])

    barChartView.data = chartData   
}

}

at this point, when i run the app, i get the result indicated earlier. i fixed those after going through the Charts api. i suppose this problem is caused by the changes from Charts 2.x to 3.x.

if youre a Charts expert, i kindly request your assistance.

回答1:

I had been working on your question, first of all, you have inverted the values of y and x

fixing this

this line let dataEntry = BarChartDataEntry(x: values[i], y: Double(i), data: dataPoints[i] as AnyObject) must be replaced by this one let dataEntry = BarChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)

and then you must use the IndexAxisValueFormatter class in order to show the legend in the bottom, I mean the months names

adding this lines

barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
barChartView.xAxis.labelCount = months.count
barChartView.xAxis.labelPosition = .bottom

this is the full code

import UIKit
import Charts

class BarChartViewController: UIViewController {
    @IBOutlet weak var barChartView: BarChartView!

    var months: [String]!

    override func viewDidLoad() {
        super.viewDidLoad()
        // barChartView.noDataTextDescription = "Just because" type BarChartView doest have noDataTextDescription
        months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
        let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

        //setChart(months, values: unitsSold) missing argument label dataPoints: in call
        setChart(dataPoints: months, values: unitsSold)
    }

    func setChart(dataPoints: [String], values: [Double]) {
        barChartView.noDataText = "You need to provide data for the chart."
        var dataEntries: [BarChartDataEntry] = []

        for i in 0..<dataPoints.count {
            // let dataEntry = BarChartDataEntry(value: values[i], xIndex: i) // argument labeld dont match
            let dataEntry = BarChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
            dataEntries.append(dataEntry)
        }

        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")

        // let chartData = BarChartData(xVals: months, dataSet: chartDataSet) cannot invoke initializer BarCharData with arg list of (xVals: [String]!, dataSet: BarCharDataSet
        let chartData    = BarChartData(dataSets: [chartDataSet])

        barChartView.data = chartData
         //barChartView.xAxis.ent
        barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
        barChartView.xAxis.labelCount = months.count
        barChartView.xAxis.labelPosition = .bottom

    }

}

And this is how it looks, Hope this helps you



标签: ios charts