当前位置 : 主页 > 手机开发 > 其它 >

swift – 使用iOS-Charts的组合图表(线图和条形图)

来源:互联网 收集:自由互联 发布时间:2021-06-11
我试图在 swift(iOS图表)中组合一个线条图和条形图. x轴和y轴似乎都可以正确缩放,但数据未显示在图表上.我错过了什么? import UIKitimport Chartsclass CombinedChartsTest: UIViewController {@IBOutlet we
我试图在 swift(iOS图表)中组合一个线条图和条形图. x轴和y轴似乎都可以正确缩放,但数据未显示在图表上.我错过了什么? enter image description here

import UIKit
import Charts

class CombinedChartsTest: UIViewController {


@IBOutlet weak var combinedChart: CombinedChartView!


let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [2.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 17.0, 2.0, 4.0, 5.0, 4.0]

override func viewDidLoad() {
    super.viewDidLoad()

    setChart(months, yValuesLineChart:  unitsSold, yValuesBarChart: unitsSold)
}

func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double]) {
    combinedChart.noDataText = "Please provide data for the chart."

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    var yVals2 : [BarChartDataEntry] = [BarChartDataEntry]()

    for i in 0..<xValues.count {

        yVals1.append(ChartDataEntry(value: yValuesLineChart[i], xIndex: i))
        yVals2.append(BarChartDataEntry(value: yValuesBarChart[i] - 1, xIndex: i))

    }

    let lineChartSet = LineChartDataSet(yVals: yVals1, label: "Line Data")
    let barChartSet: BarChartDataSet = BarChartDataSet(yVals: yVals2, label: "Bar Data")

    let data: CombinedChartData = CombinedChartData(xVals: xValues, dataSets: [lineChartSet,barChartSet])

    combinedChart.data = data

}
}
我终于找到了遗漏的东西.

import UIKit
import Charts

class CombinedChartsTest: UIViewController {


@IBOutlet weak var combinedChartView: CombinedChartView!


let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [2.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 17.0, 2.0, 4.0, 5.0, 4.0]

override func viewDidLoad() {
    super.viewDidLoad()

    setChart(months, yValuesLineChart:  unitsSold, yValuesBarChart: unitsSold)
}

func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double]) {
    combinedChartView.noDataText = "Please provide data for the chart."

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    var yVals2 : [BarChartDataEntry] = [BarChartDataEntry]()

    for i in 0..<xValues.count {

        yVals1.append(ChartDataEntry(value: yValuesLineChart[i], xIndex: i))
        yVals2.append(BarChartDataEntry(value: yValuesBarChart[i] - 1, xIndex: i))

    }

    let lineChartSet = LineChartDataSet(yVals: yVals1, label: "Line Data")
    let barChartSet: BarChartDataSet = BarChartDataSet(yVals: yVals2, label: "Bar Data")


    let data: CombinedChartData = CombinedChartData(xVals: xValues)
    data.barData = BarChartData(xVals: xValues, dataSets: [barChartSet])
    data.lineData = LineChartData(xVals: xValues, dataSets: [lineChartSet])

    combinedChartView.data = data

}
}
网友评论