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 use IValueFormatter in stacked bar chart #2916

Closed
hiralnaik opened this issue Oct 26, 2017 · 6 comments
Closed

How to use IValueFormatter in stacked bar chart #2916

hiralnaik opened this issue Oct 26, 2017 · 6 comments

Comments

@hiralnaik
Copy link

Hi there

I am trying to use IValueFormatter in stacked bar chart but dataSetIndex is always retuning 0. I want to display different text for different stacked bar. I have max 2 stacked bar. Value is coming properly but I want to display text on it.

`public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {

    if let index = chartValues.index(of: value) {
        formatter.numberStyle = .percent
        formatter.maximumFractionDigits = 0
        formatter.minimumIntegerDigits = 1
        print(dataSetIndex) //this return 0 ever time
        print(entry.x)
        print(entry.y)
        return "\(formatter.string(from: NSNumber.init(value: value))!)\n\(self.stringsValues[index])"
    }
    return ""
}`

Any help.

Thanks

@liuxuan30
Copy link
Member

how many data sets you have in the first place.. a stacked bar should only have one data set for sure.. Why don't you check stacked bar chart in ChartsDemo? It's already a good demo

@hiralnaik
Copy link
Author

hiralnaik commented Oct 27, 2017

I checked that demo. It doesn't have that solution what I am looking for. I am display value + text on bar. In the current demo it displays only number with formatter.
For example
I have values [15, 8] and I want to display on stack bar like this

screen shot 2017-10-27 at 9 25 36 am

Thanks

@liuxuan30
Copy link
Member

two things:

  1. it already have formatter, so you can format the string with percentage + value;
  2. wrapping seems not supported yet. You might need to override drawValues to enable two line texts.

@hiralnaik
Copy link
Author

That's right. But think about situation in this function,
public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String

when value will be same how can I identify if the value is for 1st stack or 2nd stack.

something like
value for 1st stack = 0.5
value for 2ns stack = 0.5

@phanindra92
Copy link

    let data = BarChartData(dataSet: set)
    let format = NumberFormatter()
    format.numberStyle = .none
    let formatter = DefaultValueFormatter(formatter: format)
    data.setValueFormatter(formatter)

@chinghoi
Copy link

That's right. But think about situation in this function,
public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String

when value will be same how can I identify if the value is for 1st stack or 2nd stack.

something like
value for 1st stack = 0.5
value for 2ns stack = 0.5

I also encountered this problem, how did you solve it in the end?

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

4 participants