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

horizontal bar graph value different decimal places each time #1260

Closed
hiralnaik opened this issue Jul 21, 2016 · 12 comments
Closed

horizontal bar graph value different decimal places each time #1260

hiralnaik opened this issue Jul 21, 2016 · 12 comments

Comments

@hiralnaik
Copy link

hiralnaik commented Jul 21, 2016

Hi There

I am always seeing different decimal places on the bar. I want to stick with 1 decimal only. Can someone please help with it ?

screen shot 2016-07-21 at 4 40 37 pm

var yVals = [ChartDataEntry]() yVals.append(BarChartDataEntry(value: previousYear, xIndex: 0)) yVals.append(BarChartDataEntry(value: currentYear, xIndex: 1))

Thanks
Hiral

@dxclancy
Copy link

Valueformatter

@hiralnaik
Copy link
Author

I tried with it but it is still showing random decimal places.

Here is the code

`self.setupBarLineChartView(self.chartView)
self.chartView.delegate = self
self.chartView.descriptionText = ""
self.chartView.noDataTextDescription = "Data not available"
self.chartView.drawBarShadowEnabled = false
self.chartView.drawValueAboveBarEnabled = true

    let xAxis = self.chartView.xAxis
    xAxis.labelPosition = ChartXAxis.LabelPosition.Bottom
    xAxis.labelFont = UIFont(name: "HelveticaNeue", size: 8)!
    xAxis.drawAxisLineEnabled = true
    xAxis.drawGridLinesEnabled = false
    xAxis.gridLineWidth = 0.3
    xAxis.wordWrapEnabled = true

    let leftAxis = self.chartView.leftAxis
    leftAxis.enabled = false
    leftAxis.labelFont = UIFont(name: "HelveticaNeue", size: 8)!
    leftAxis.drawAxisLineEnabled = true
    leftAxis.drawGridLinesEnabled = false
    leftAxis.gridLineWidth = 0.3
    leftAxis.axisMinValue = 0.0 // this replaces startAtZero = YES

    let rightAxis = self.chartView.rightAxis
    rightAxis.enabled = false
    rightAxis.labelFont = UIFont(name: "HelveticaNeue", size: 7)!
    rightAxis.drawAxisLineEnabled = true
    rightAxis.drawGridLinesEnabled = false
    rightAxis.axisMinValue = 0.0; // this replaces startAtZero = YES

    self.chartView.legend.horizontalAlignment = ChartLegend.HorizontalAlignment.Left
    self.chartView.legend.form = ChartLegend.Form.Circle
    self.chartView.legend.formSize = 8.0
    self.chartView.legend.font = UIFont(name: "HelveticaNeue", size: 8)!
    self.chartView.legend.xEntrySpace = 4.0
    self.chartView.animate(yAxisDuration: 1.5)

    self.chartView.extraRightOffset = CGFloat(30.0)

`

`func setDataCount(currentYear : Double, previousYear : Double) {

    var xVals = [NSObject]()

    xVals.append("Previous Year")
    xVals.append("Current Year")

    var yVals = [ChartDataEntry]()
    yVals.append(BarChartDataEntry(value: previousYear, xIndex: 0))
    yVals.append(BarChartDataEntry(value: currentYear, xIndex: 1))

    var set1 : BarChartDataSet? = nil

    if self.chartView.data?.dataSetCount > 0 {
        let temp = self.chartView.data?.dataSets[0] as! BarChartDataSet
        set1 = temp
        set1!.yVals = yVals
        self.chartView.data?.xValsObjc = xVals
        self.chartView.notifyDataSetChanged()

    } else {
        set1 = BarChartDataSet(yVals: yVals, label: "Dataet")
        set1?.colors = [UIColor.cbiBlueColor(), UIColor.orangeColor()]
        set1?.barSpace = 0.25
        var dataSets = [BarChartDataSet]()
        dataSets .append(set1!)

        let data : BarChartData = BarChartData(xVals: xVals, dataSets: dataSets)
        data.setValueFont(UIFont(name: "HelveticaNeue", size: 8)!)
        data.setValueTextColor(UIColor.blackColor())
        self.chartView.data = data
    }

    let percentage = self.calculatePercentageChangeForDisplay(currentYear, comparedValue: previousYear)
    let value = CBIFormattingUtility.sharedInstance.percentChangeFormatter.stringFromNumber(percentage)
    self.growthLabel.text = value

    self.growthLabel.textColor = UIColor.cbiYellowColor()
    if percentage != 0 {
        self.growthLabel.textColor = percentage < 0 ? UIColor.cbiRedColor() : UIColor.cbiGreenColor()
    }
}

`

@liuxuan30
Copy link
Member

use maximumFractionDigits = 1

@hiralnaik
Copy link
Author

I tried maximum fraction digit to 1 but it is still not happening. I don't know what happen ?

@liuxuan30
Copy link
Member

where do you set it?

@hiralnaik
Copy link
Author

I mean I put it in my code and run it but it is still not doing it.

I set it right after rightAxis.rightAxisMinVal = 0.0.

I set it maximum fraction digit to 1 but still not doing it.

I don't know if I miss anything in the code.

@liuxuan30
Copy link
Member

it should be used in your data set's value formatter.

@liuxuan30
Copy link
Member

also, are you saying two digits after decimal? Not quite clear what are you asking, so I took a guess you want to show only one digit. Like 5.00 -> 5.0

@hiralnaik
Copy link
Author

Yes I want to display 5.0. But it is displaying 5.234, 5.23, 5.2 etc.

@liuxuan30
Copy link
Member

then use maximumFractionDigits = 1.

@hiralnaik
Copy link
Author

hiralnaik commented Jul 26, 2016

Thanks for look into it but I am 100% sure it is not happening with my Swift code. Below is the view controller code, if you can please take a look what am i doing wrong ?

`import UIKit
import Charts
class GraphViewController: UIViewController, ChartViewDelegate {
@IBOutlet var chartView: HorizontalBarChartView!
@IBOutlet weak var growthLabel: UILabel!
@IBOutlet weak var volumeDepletionLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

}

/**
 Setup UI Elments for the view controller
 */
func setupUI() -> Void {
    self.chartView.delegate = self
    self.chartView.descriptionText = ""
    self.chartView.noDataTextDescription = "Data not available"
    self.chartView.drawBarShadowEnabled = false
    self.chartView.drawValueAboveBarEnabled = true

    let xAxis = self.chartView.xAxis
    xAxis.labelPosition = ChartXAxis.LabelPosition.Bottom
    xAxis.labelFont = UIFont(name: "HelveticaNeue", size: 8)!
    xAxis.drawAxisLineEnabled = true
    xAxis.drawGridLinesEnabled = false
    xAxis.gridLineWidth = 0.3
    xAxis.wordWrapEnabled = true

    let leftAxis = self.chartView.leftAxis
    leftAxis.enabled = false
    leftAxis.labelFont = UIFont(name: "HelveticaNeue", size: 8)!
    leftAxis.drawAxisLineEnabled = true
    leftAxis.drawGridLinesEnabled = false
    leftAxis.gridLineWidth = 0.3
    leftAxis.axisMinValue = 0.0 // this replaces startAtZero = YES

    let rightAxis = self.chartView.rightAxis
    rightAxis.enabled = false
    rightAxis.labelFont = UIFont(name: "HelveticaNeue", size: 7)!
    rightAxis.drawAxisLineEnabled = true
    rightAxis.drawGridLinesEnabled = false
    rightAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
    rightAxis.valueFormatter = NSNumberFormatter()
    rightAxis.valueFormatter?.maximumFractionDigits = 1

    self.chartView.legend.horizontalAlignment = ChartLegend.HorizontalAlignment.Left
    self.chartView.legend.form = ChartLegend.Form.Circle
    self.chartView.legend.formSize = 8.0
    self.chartView.legend.font = UIFont(name: "HelveticaNeue", size: 8)!
    self.chartView.legend.xEntrySpace = 4.0
    self.chartView.animate(yAxisDuration: 1.5)

    self.chartView.extraRightOffset = CGFloat(30.0)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

/**
 Set Data for the graph

 - parameter currentYear:  current year 
 - parameter previousYear: previous year 
 */
func setDataCount(currentYear : Double, previousYear : Double) {

    var xVals = [NSObject]()

    xVals.append("Previous Year")
    xVals.append("Current Year")

    var yVals = [ChartDataEntry]()
    yVals.append(BarChartDataEntry(value: previousYear, xIndex: 0))
    yVals.append(BarChartDataEntry(value: currentYear, xIndex: 1))

    var set1 : BarChartDataSet? = nil

    if self.chartView.data?.dataSetCount > 0 {
        let temp = self.chartView.data?.dataSets[0] as! BarChartDataSet
        set1 = temp
        set1!.yVals = yVals
        self.chartView.data?.xValsObjc = xVals
        self.chartView.notifyDataSetChanged()

    } else {
        set1 = BarChartDataSet(yVals: yVals, label: "Dataet")
        set1?.colors = [UIColor.cbiBlueColor(), UIColor.orangeColor()]
        set1?.barSpace = 0.25
        var dataSets = [BarChartDataSet]()
        dataSets .append(set1!)

        let data : BarChartData = BarChartData(xVals: xVals, dataSets: dataSets)
        data.setValueFont(UIFont(name: "HelveticaNeue", size: 8)!)
        data.setValueTextColor(UIColor.blackColor())
        self.chartView.data = data
    }

    let percentage = self.calculatePercentageChangeForDisplay(currentYear, comparedValue: previousYear)
    let value = CBIFormattingUtility.sharedInstance.percentChangeFormatter.stringFromNumber(percentage)
    self.growthLabel.text = value

    self.growthLabel.textColor = UIColor.cbiYellowColor()
    if percentage != 0 {
        self.growthLabel.textColor = percentage < 0 ? UIColor.cbiRedColor() : UIColor.cbiGreenColor()
    }
}

internal func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {

    print("Hello There")
}

/**
 Calculating percentage change for the SKU

 - parameter currentValue:  current
 - parameter comparedValue: previous

 - returns: percentage value
 */
func calculatePercentageChangeForDisplay(currentValue : Double, comparedValue : Double) -> Double {

    if comparedValue <= 0 {
        if currentValue == 0 {
            return 0
        } else if currentValue > 0 {
            return 1
        } else {
            return -1
        }
    } else {
        return (comparedValue == (0)) ? (currentValue - comparedValue) : (currentValue - comparedValue) / comparedValue
    }
}

}
`

@hiralnaik
Copy link
Author

Oops never mind. I just resolved the issue. It is issue with my code.

let data : BarChartData = BarChartData(xVals: xVals, dataSets: dataSets) data.setValueFormatter(CBIFormattingUtility.sharedInstance.valueChangeFormatterForGraph) data.setValueFont(UIFont(name: "HelveticaNeue", size: 8)!) data.setValueTextColor(UIColor.blackColor()) self.chartView.data = data

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

3 participants