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

One label for each data point - Line Chart - SOLUTION FOUND #2553

Closed
ethanfox opened this issue Jun 24, 2017 · 11 comments
Closed

One label for each data point - Line Chart - SOLUTION FOUND #2553

ethanfox opened this issue Jun 24, 2017 · 11 comments

Comments

@ethanfox
Copy link

How to I make it to where there is only one label for each data point in my linechart?

Below is what I have.
img_4142

This is what I am trying to get:
screen shot 2017-06-24 at 12 58 30 pm

@thierryH91200
Copy link
Contributor

If this is what you want to do here the solution in archive.zip
do not consider some options these are PRs in progress

see #2094

capture d ecran 2017-06-24 a 19 46 49

Archive.zip

@ethanfox
Copy link
Author

ethanfox commented Jun 24, 2017

@thierryH91200
setting granularity = 1 did not fix it for me. What else could it be??

chartView.xAxis.granularityEnabled = true
chartView.xAxis.granularity = 1

@thierryH91200
Copy link
Contributor

all the trick is in the formatter
look at archive.zip

@ethanfox
Copy link
Author

ethanfox commented Jun 24, 2017

@thierryH91200
I'm getting my dates from my Firebase database. It's the date of whenever someone reviewed a business. So they will not always be the same time apart - if that makes sense. And I am saving them into an array. So would I need the date formatter still?

@thierryH91200
Copy link
Contributor

Yes the whole solution is in the formatter
Go slowly
Read the #2094

@ethanfox
Copy link
Author

@thierryH91200
Would I need this also?
chartView.xAxis.valueFormatter = DateValueFormatter(miniTime : from, interval: hourSeconds)

@thierryH91200
Copy link
Contributor

this is the soluce....
try and see
read the #2094

@ethanfox
Copy link
Author

I guess I'll never know. If you're only going to allude to random crap then don't reply

@thierryH91200
Copy link
Contributor

I am tired I wanted to help you and well so much worse
goodbye
It works for other people

@ethanfox
Copy link
Author

Here's what I got:
`class CustomLabelsAxisValueFormatter : NSObject, IAxisValueFormatter {

var labels: [String] = []


var dateFormatter : DateFormatter
var miniTime: Double


public init(miniTime: Double) {
    //super.init()
    self.miniTime = miniTime
    self.dateFormatter = DateFormatter()
    self.dateFormatter.dateFormat = "dd/MM HH:mm"
    self.dateFormatter.dateFormat = "dd/MM"
    dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT+0:00") as TimeZone!
}



func stringForValue(_ value: Double, axis: AxisBase?) -> String {
   
    let date2 = Date (timeIntervalSince1970: (value * 3600 * 24) + miniTime)
    
    let count = self.labels.count
    
    guard let axis = axis, count > 0 else {
        
        return dateFormatter.string (from: date2)
    }
    
    let factor = axis.axisMaximum / Double(count)
    
    let index = Int((value / factor).rounded())
    
    if index >= 0 && index < count {
        
        return self.labels[index]
    }
    
    return dateFormatter.string (from: date2)
    
    
    

    
}

}
`

@ethanfox
Copy link
Author

ethanfox commented Jun 25, 2017

I found a simple solution. :)

` func setChart(dataPoints: [String], values: [Double]) {
   chartView.noDataText = "You need to provide data for the chart."

var dataEntries: [ChartDataEntry] = []

for i in 0..<dataPoints.count {
    let dataEntry = ChartDataEntry(x: Double(i)+0.5, y: values[i])
    dataEntries.append(dataEntry)
}

let chartDataSet = LineChartDataSet(values: dataEntries, label: "")
let chartData = LineChartData(dataSets: [chartDataSet])


chartDataSet.colors = [red]
chartDataSet.drawCirclesEnabled = true
chartDataSet.circleRadius = 16
chartDataSet.circleColors = [red]
chartDataSet.circleHoleRadius = 12
chartDataSet.circleHoleColor = UIColor.white
chartDataSet.lineWidth = 4
chartView.data = chartData

}`

Notice the +0.5.

@ethanfox ethanfox changed the title One label for each data point - Line Chart One label for each data point - Line Chart - SOLUTION FOUND Jun 25, 2017
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