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

Can't manage to align plots with x axis values #3332

Closed
richardhouta opened this issue Mar 12, 2018 · 2 comments
Closed

Can't manage to align plots with x axis values #3332

richardhouta opened this issue Mar 12, 2018 · 2 comments

Comments

@richardhouta
Copy link

richardhouta commented Mar 12, 2018

Hi to everyone,

I have a chart that should display on the x axis the number of days of a cycle, and the temperatures that the user filled in, the problem is that the user is not required to add a temperature every day if he doesn't want to, and I have to ignore values that are equal to 0. I looked on stack-overflow and here to properly set x-axis labels, but for some reason my plots are not properly aligned with my labels. I also noticed that in my XAxisFormatter the value returned by the stringForValue is not like I expected meaning [1, 2, 3, 4] but more like [0.5, 0.9, 1.6, 2.4] meaning that I cannot use the value convert it as Int and pull the data from my array.

Any idea what could be the problem below part of the code I wrote

ChartDataSetEntries

private func prepareDataEntries(with data: [DailyData]) -> [ChartDataEntry] {
        var dataEntries = [ChartDataEntry]()
        for (index, dailyData) in data.enumerated() {
            if dailyData.temperature > 0 {
                let temperature = String(format:"%.2f", Double(dailyData.temperature)/10)
                if let formattedTemp = Double(temperature) {
                    let chartDataEntry = ChartDataEntry(x: Double(index), y: formattedTemp)
                    dataEntries.append(chartDataEntry)
                    if ovulationDetector.hasOvulationForDay(at: index){
                        ovulation = Highlight(x: chartDataEntry.x, y: chartDataEntry.y, dataSetIndex: 0)
                    }
                }
            }
        }
        return dataEntries
    }

Setup of the XAxis

chartView.xAxis.valueFormatter = XAxisValueFormatter(with: xAxisValues)
            chartView.xAxis.labelPosition = .bottom
            chartView.xAxis.gridColor = UIColor.black.withAlphaComponent(0.15)
            chartView.xAxis.setLabelCount(totalDays, force: true)

XAxisValueFormatter

private class XAxisValueFormatter: NSObject, IAxisValueFormatter {
    private var values: [String]
    
    init(with values: [String]) {
        self.values = values
        super.init()
    }
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
//value cannot be used like values[Int(value)] because I get 0.4 then 0.9 etc... and I will get duplicates since both will be equal to 1
        if let index = axis?.entries.index(where: { (current) -> Bool in
            return current == value
        }) {
            return "\(index+1)"
        }else {
            return "0"
        }
    }
}

screen shot 2018-03-13 at 0 14 08

Any idea what could be the issue ?

@richardhouta
Copy link
Author

@jjatie, @thierryH91200, @danielgindi ?

@liuxuan30
Copy link
Member

override computeAxisValues and feed axis.entries with desired values to make them align.

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