Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to show all labels in xAxis ? #3337

Closed
Charlotte3240 opened this issue Mar 13, 2018 · 2 comments
Closed

How to show all labels in xAxis ? #3337

Charlotte3240 opened this issue Mar 13, 2018 · 2 comments

Comments

@Charlotte3240
Copy link

image

    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 3.0]
    let unitsSold2 = [10.0, 3.0, 5.0, 2.0, 2.0, 1.0]

    var combineChartView = BarChartView()

        combineChartView.chartDescription?.enabled = false
        combineChartView.scaleYEnabled = false
        combineChartView.scaleXEnabled = false
        combineChartView.noDataText = "暂时没有任何数据"
        
        let xAxis = combineChartView.xAxis
        xAxis.labelPosition = .bottom
        xAxis.drawGridLinesEnabled = false
        xAxis.drawAxisLineEnabled = true
        xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
        xAxis.forceLabelsEnabled = true
        xAxis.granularity = 1
        xAxis.granularityEnabled = true
        xAxis.labelTextColor = UIColor.black
        
        
        combineChartView.rightAxis.enabled = false
        let yAxis = combineChartView.leftAxis
        yAxis.drawGridLinesEnabled = true
        
        var entrys : [BarChartDataEntry] = []
        

        for i in 0..<unitsSold.count {
            let entry = BarChartDataEntry(x: Double(i), y: Double(unitsSold[i]))
            entrys.append(entry)
        }
        
        let set1 = BarChartDataSet(values: entrys, label: "老顾客")
        set1.colors = [UIColor.yellow]
        set1.drawValuesEnabled = false

        entrys = []
        for i in 0..<unitsSold2.count {
            let entry = BarChartDataEntry(x: Double(i), y: Double(unitsSold2[i]))
            entrys.append(entry)
        }
        
        let set2 = BarChartDataSet(values: entrys, label: "新顾客")
        set2.colors = [UIColor.red]
        set2.drawValuesEnabled = false
        
        
        
        let chartData = BarChartData(dataSets: [set1,set2])
        
        combineChartView.xAxis.axisMinimum = chartData.xMin - 1.0
        combineChartView.xAxis.axisMaximum = chartData.xMax + 1.0

        
        
        combineChartView.data = chartData
@Charlotte3240
Copy link
Author

I think Writing like this ,it's will work. Is there any more attention?

        xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
        xAxis.forceLabelsEnabled = true
        xAxis.granularity = 1
        xAxis.granularityEnabled = true

@Charlotte3240
Copy link
Author

NOW ,i change this file "IndexAxisValueFormatter.swift"
it works

    open func stringForValue(_ value: Double,
                             axis: AxisBase?) -> String
    {
        let index = Int(value.rounded())
        guard index>=0, index <= _values.count-1 else {
            return ""
        }
        print(_values[index])

        return _values[index]
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant