开发者社区> 问答> 正文

如何使用图表(IOS)在X轴上每月显示

stringForValue 里
面我试着打印价值,结果显示:0 2 4 6 8 10 0 1 2 3 4 5 6 7 8 9 10 11 0 2 4 6 8 10 但
为什么它经过了三次,第一次和最后一次通过一次?我每个月都能做些什么?

import UIKit

import Charts

class BarChartViewController: UIViewController {

    var months = [String]()
    var unitsSold = [Double]()

    weak var axisFormatDelegate: IAxisValueFormatter?

    @IBOutlet weak var barChartView: BarChartView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        axisFormatDelegate = self

        months = ["Jan",
                  "Feb",
                  "Mar",
                  "Apr",
                  "May",
                  "Jun",
                  "Jul",
                  "Aug",
                  "Sep",
                  "Oct",
                  "Nov",
                  "Dec"
        ]

        unitsSold = [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]

        setChart(dataPoints: months, values: unitsSold)


    }

    func setChart(dataPoints: [String], values: [Double]) {
        barChartView.noDataText = "NO DATA"

        var dataEntries = [BarChartDataEntry]()

        for i in 0..<dataPoints.count {
            let dataEntry = BarChartDataEntry(x: Double(i), y: values[i], data: dataPoints)

            dataEntries.append(dataEntry)
        }

        let chartDataSet = BarChartDataSet(entries: dataEntries, label: "Units Sold")
        let chartData = BarChartData(dataSet: chartDataSet)

        barChartView.data = chartData

        barChartView.xAxis.valueFormatter = axisFormatDelegate
    }


}

extension BarChartViewController: IAxisValueFormatter {

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {

        print(Int(value))

        return months[Int(value)]
    }  
}

展开
收起
游客5akardh5cojhg 2019-12-05 22:09:19 408 0
0 条回答
写回答
取消 提交回答
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载

相关实验场景

更多