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

Cannot add labels to a simple Bar Chart #1846

Closed
shayneptorres opened this issue Nov 17, 2016 · 3 comments
Closed

Cannot add labels to a simple Bar Chart #1846

shayneptorres opened this issue Nov 17, 2016 · 3 comments

Comments

@shayneptorres
Copy link

Im not sure if I just don't know how to do it, but I am unable to add labels to invidual bars in a simple bar chart. For example, I want to list how money money I have made within the last 3 months. I want to display this information in a bar chart that has the Money amount as the y-axis and the months I worked as the x-axis. I can get the amount made to show up on the left and right sides of the chart but I am unable to get the name of the months to display anywhere on the graph, the only x-axis values I am getting are a sequence of doubles from 0 to 2.5 (0,0.5,1.0,1.5,2.0,2.5). What can I do to make this happen?

@shayneptorres
Copy link
Author

Here is my code to where I set up the chart:

`

// declared elsewhere
items = ["September","October","November"]
itemsValues = [7432,4321,3998]

func setUpChart(){

    var dataEntries = [BarChartDataEntry]()
    var barChartDataSet = BarChartDataSet()

    for i in 0..<self.items.count {
        let d = BarChartDataEntry(x: Double(i), yValues: [self.itemsValues[i]], label: self.items[i])
        dataEntries.append(de)

    }

    barChartDataSet = BarChartDataSet(values: dataEntries , label: "Monthly Income")

    let bcData = BarChartData(dataSet: barChartDataSet)
    self.chartView.data = bcData


    var colors: [UIColor] = []

    for _ in 0..<self.items.count {
        let red = Double(arc4random_uniform(256))
        let green = Double(arc4random_uniform(256))
        let blue = Double(arc4random_uniform(256))

        let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
        colors.append(color)
    }

    barChartDataSet.colors = colors
    self.chartView.xAxis.labelPosition = .bottom
    self.chartView.drawValueAboveBarEnabled = true

    self.chartView.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: .easeInOutCirc)
}`

@shayneptorres
Copy link
Author

img_0004
Here is an example of the bar chart that is being generated

@liuxuan30
Copy link
Member

liuxuan30 commented Nov 18, 2016

use ValueFormatter. Check ChartDemo.

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