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

Don't display or load all Variables on the Left of the Bar (iOS - Swift 3) #2221

Closed
jaragorns opened this issue Mar 3, 2017 · 1 comment
Closed

Comments

@jaragorns
Copy link

jaragorns commented Mar 3, 2017

I use Horizontal Bar and the bars load fine. But don't load all variables name on the xAxis.

Display the variable in the Left of the Bar

This is my result:
captura de pantalla 2017-03-03 a las 12 45 10 p m

This is the code:

            var dataEntries: [BarChartDataEntry] = []
            for i in 0..<barValues.count {
                let dataEntry = BarChartDataEntry(x: Double(i), y: barValues.object(at: i) as! Double)
                dataEntries.append(dataEntry)
            }
            let barChartDataSet = BarChartDataSet(values: dataEntries, label: nil)
            let barChartData = BarChartData(dataSet: barChartDataSet)
            barChartData.setDrawValues(true)

            cell.barChart.legend.enabled = false
            cell.barChart.leftAxis.enabled = false
            cell.barChart.rightAxis.enabled = false
            cell.barChart.chartDescription?.enabled = false
            cell.barChart.drawValueAboveBarEnabled = true
            cell.barChart.leftAxis.axisMinimum = 0
            cell.barChart.isUserInteractionEnabled = false
            cell.barChart.noDataText = "NO DATA."
            
            //Left Side
            let xAxis = cell.barChart.xAxis
            xAxis.enabled = true
            xAxis.setLabelCount(barVariables.count, force: true)
            xAxis.centerAxisLabelsEnabled = true
            xAxis.drawLabelsEnabled = true
            xAxis.drawGridLinesEnabled = false
            xAxis.axisLineColor = UIColor.black
            xAxis.labelPosition = .bottom
            xAxis.labelFont = UIFont(name: "Avenir-Medium", size: 12.0)!

           //Here I send Array<String> for change values Double to String, but don't load all values.
            xAxis.valueFormatter = IndexAxisValueFormatter(values: arrayVariables)
            
            //Current Values
            let format = NumberFormatter()
            format.numberStyle = NumberFormatter.Style.percent
            format.maximumFractionDigits = 1
            format.multiplier = 1.0
            format.percentSymbol = " %"
            let formatter = DefaultValueFormatter(formatter: format)
            barChartData.setValueFormatter(formatter)
            
            barChartData.setValueFont(UIFont(name: "Avenir-Light", size: 8.0))
            barChartData.setValueTextColor(UIColor.black)
            
            cell.barChart.data = barChartData
            
            
            return cell
@jaragorns jaragorns changed the title Display the variable in the Left of the Bar (iOS - Swift 3) Display the variable String, not double value in the Left of the Bar (iOS - Swift 3) Mar 3, 2017
@jaragorns jaragorns changed the title Display the variable String, not double value in the Left of the Bar (iOS - Swift 3) Don't display or load all Variables on the Left of the Bar (iOS - Swift 3) Mar 3, 2017
@jaragorns
Copy link
Author

I Solved update the file :

IndexAxisValueFormatter.swift

In all cases I need the value on _values, this method is called and return _values on position [index]

  1. On First case need return _values[0] "Where index should be 0", and the index is -1. I forced to return _values[0] when index == -1
  2. Original sentence if is return "" because: index != Int(value) many times.
open func stringForValue(_ value: Double,
                             axis: AxisBase?) -> String
    {
        let index = Int(value.rounded())
      
        //--- 1. ---//
        if index == -1 {
            return _values[0]
        }

        //--- 2. ---//
        //if index < 0 || index >= _valueCount || index != Int(value)
        if index < 0 || index >= _valueCount
        {
            return ""
        }
        
        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