如何在条形图视图中快速显示xAxis标签?

时间:2018-07-03 12:58:40

标签: ios swift charts

我正在使用Charts存储库绘制条形图。我正在绘制分组的条形图,每个组有2个条形图。

我正在使用xaxis.valueFormatter = IndexAxisValueFormatter(values:self.months)将月份数组应用于xAxis标签。但是,我不能。

我可以成功绘制条形图分组的条形图。但是,我无法在X轴上显示标签。如何显示?

我正在使用以下代码

import UIKit
import Charts


class SalesDashboard: UIViewController,ChartViewDelegate{

    @IBOutlet var chartView: BarChartView!
    var months = ["Ap","Jun","Ap","Jun","Ap","Jun"]

    var unitsSold:[Float] = [12,123,12,123,12,123]
    var unitsBought:[Float] = [12,123,12,123,12,123]

   override func viewDidLoad() {
        super.viewDidLoad()

        self.chartView.delegate = self
        self.chartView.noDataText = "You need to provide data for the chart."
        self.chartView.chartDescription?.text = nil


        let legend = self.chartView.legend
        legend.enabled = true
        legend.horizontalAlignment = .right
        legend.verticalAlignment = .top
        legend.orientation = .horizontal
        legend.drawInside = true
        legend.yOffset = 0.0;
        legend.xOffset = 0.0;
        legend.yEntrySpace = 0.0;


        let xaxis = self.chartView.xAxis
//        xaxis.drawGridLinesEnabled = false
//        xaxis.labelPosition = .bottom
//        xaxis.centerAxisLabelsEnabled = true
        xaxis.valueFormatter = IndexAxisValueFormatter(values:self.months)
        xaxis.granularity = 1.0
        xaxis.granularityEnabled = true
        xaxis.decimals = 0


        let leftAxisFormatter = NumberFormatter()
        leftAxisFormatter.maximumFractionDigits = 1

        self.chartView.leftAxis.gridColor = .clear
        self.chartView.rightAxis.gridColor = .clear
}

  override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        self.setChart()
     }

    func setChart() {

        self.chartView.noDataText = "You need to provide data for the chart."
        var dataEntries: [BarChartDataEntry] = []
        var dataEntries1: [BarChartDataEntry] = []

        for i in 0..<self.months.count {

            let dataEntry = BarChartDataEntry(x: Double(i) , y: Double(self.unitsSold[i]))
            dataEntries.append(dataEntry)

            let dataEntry1 = BarChartDataEntry(x: Double(i) , y: Double(self.unitsBought[i]))
            dataEntries1.append(dataEntry1)
        }

        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Sales")
        let chartDataSet1 = BarChartDataSet(values: dataEntries1, label: "Profit")

        let dataSets: [BarChartDataSet] = [chartDataSet,chartDataSet1]
        chartDataSet.colors = [UIColor.init(red: 244/255, green: 125/255, blue: 49/255, alpha: 1.0)]
        chartDataSet1.colors = [UIColor.init(red: 37/255, green: 109/255, blue: 95/255, alpha: 1.0)]
        let chartData = BarChartData(dataSets: dataSets)


        let groupSpace = 0.3
        let barSpace = 0.05
        let barWidth = 0.3

        let groupCount = self.months.count
        let startYear = 1

        chartData.barWidth = barWidth;
        self.chartView.xAxis.axisMinimum = Double(startYear)
        let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)

        self.chartView.xAxis.axisMaximum = Double(startYear) + gg * Double(groupCount)

        chartData.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)
        self.chartView.notifyDataSetChanged()

        self.chartView.data = chartData

        //background color
        self.chartView.backgroundColor = .clear

        //chart animation
        self.chartView.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: .linear)

    }

}

Screenshot

0 个答案:

没有答案