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

BarChart draw sum of values above bar #3604

Closed
Ewg777 opened this issue Aug 15, 2018 · 2 comments
Closed

BarChart draw sum of values above bar #3604

Ewg777 opened this issue Aug 15, 2018 · 2 comments

Comments

@Ewg777
Copy link
Contributor

Ewg777 commented Aug 15, 2018

Is it possible to build a bar chart like this? Sum of values are placed above the bar

Example

Thanks

@mackyfamous
Copy link

I would like to know if you already fixed this issue, would you mind sharing it? Thanks.

@Ewg777
Copy link
Contributor Author

Ewg777 commented Sep 7, 2018

@mackyfamous here it is!

        var yVals = [BarChartDataEntry]()
        for item in chartDataItems {
            let sum = item.yValues.reduce(0, +)

            let data = String(format: "%.0f", sum) as AnyObject
            let entry = BarChartDataEntry(x: item.x, yValues: item.yValues, data: data)
            yVals.append(entry)
        }

        let set = BarChartDataSet(values: yVals, label: nil)
        set.valueFormatter = IndexValueFormatter()
class IndexValueFormatter: IValueFormatter {

    func stringForValue(_ value: Double,
                        entry: ChartDataEntry,
                        dataSetIndex: Int,
                        viewPortHandler: ViewPortHandler?) -> String {
        let barEntry = entry as! BarChartDataEntry
        let nonEmpty = barEntry.yValues?.filter({ $0 > 0 })// removes zero-item
        guard nonEmpty?.last == value else { return "" }
        return entry.data as! String
    }
}

@Ewg777 Ewg777 closed this as completed Sep 7, 2018
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

2 participants