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

Labels on x-axes not linked to chart data points #3203

Closed
achirkof opened this issue Jan 22, 2018 · 1 comment
Closed

Labels on x-axes not linked to chart data points #3203

achirkof opened this issue Jan 22, 2018 · 1 comment

Comments

@achirkof
Copy link

I have an arrays for values and dates which I want to show on chart. But labels on x-axes are not linked to chart data points. They just spread thru x-axes evenly. I would like to labels on x-axes be linked to data points and positioned straight below chart point.

Here is my chart:

enter image description here

And my code below:

private class DateValueFormatter: NSObject, IAxisValueFormatter {
    private let dateFormatter = DateFormatter()
    
    override init() {
        super.init()
        dateFormatter.dateFormat = "dd/MM"
    }
    
    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return dateFormatter.string(from: Date(timeIntervalSince1970: value))
    }
}

class ProgressSection: BaseMainCell, ChartViewDelegate {
    
    let chartView: LineChartView = {
        let chart = LineChartView()
        chart.backgroundColor = Colors.colorWhite
        return chart
    }()
    
    var chartData = LineChartData()
    var chartEnties = [ChartDataEntry]()
    
    let weight: [Double] = [66, 66.3, 65.5, 69, 70.4, 71]
    let measureDates: [Double] = [1478266901, 1481204501, 1484228501, 1487943701, 1490109325, 1491572501]
    let measureDatesString: [String] = ["04/11", "08/12", "12/01", "24/02", "21/03", "07/04"]
    
    override func setupViews() {
        super.setupViews()
        
        addSubview(chartView)
        chartView.snp.makeConstraints { (make) in
            make.topMargin.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalToSuperview().multipliedBy(0.45)
        }
        
        addSubview(shadowView)
        shadowView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(-2)
            make.width.equalToSuperview()
            make.height.equalTo(2)
        }
        
        drawChart()
    }
    
    func drawChart() {
        let valueFormatter = NumberFormatter()
        valueFormatter.maximumFractionDigits = 1
        valueFormatter.minimumFractionDigits = 0
        
        for i in 0..<weight.count {
            let entry = ChartDataEntry(x: Double(measureDates[i]), y: Double(weight[i]))
            chartEnties.append(entry)
        }
        
        let weightDataset = LineChartDataSet(values: chartEnties, label: "вес, кг")
        
        let gradientColors = [Colors.colorGreen.withAlphaComponent(0).cgColor, Colors.colorGreen.withAlphaComponent(0.2).cgColor]
        let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: nil)!
        weightDataset.setColor(Colors.colorGreen.withAlphaComponent(0.6))
        weightDataset.setCircleColor(Colors.colorGreen)
        weightDataset.fillAlpha = 1
        weightDataset.fill = Fill(linearGradient: gradient, angle: 90)
        weightDataset.drawFilledEnabled = true
        
        weightDataset.lineWidth = 3
        weightDataset.circleRadius = 4
        weightDataset.drawCircleHoleEnabled = false
        weightDataset.valueFont = UIFont(name: "OpenSans-Semibold", size: 12)!
        weightDataset.valueTextColor = Colors.colorGreen
        weightDataset.mode = .horizontalBezier
        
        chartData.addDataSet(weightDataset)
        chartData.setValueFormatter(DefaultValueFormatter(formatter: valueFormatter))
        
        chartView.delegate = self
        
        chartView.leftAxis.enabled = false
        chartView.rightAxis.enabled = false
        
        chartView.xAxis.setLabelCount(measureDates.count, force: true)
        chartView.xAxis.drawAxisLineEnabled = false
        chartView.xAxis.drawGridLinesEnabled = false
        chartView.xAxis.valueFormatter = DateValueFormatter()
        chartView.xAxis.forceLabelsEnabled = true
        
        chartView.drawBordersEnabled = false
        chartView.drawGridBackgroundEnabled = false
        chartView.xAxis.labelPosition = .bottom
        chartView.chartDescription?.font = UIFont(name: "OpenSans", size: 12)!
        chartView.chartDescription?.text = ""
        chartView.data = chartData
    }
}

Arrays measureDates and measureDatesString have equal values just in different format. I use it for investigation.

If I change my code:

for i in 0..<weight.count {
            let entry = ChartDataEntry(x: Double(measureDates[i]), y: Double(weight[i]))
            chartEnties.append(entry)
        }

and instead using date from measureDates array Double(measureDates[i]) put index to value x like let entry = ChartDataEntry(x: Double(i), y: Double(weight[i])) x-axes label will be positioned straight below data points, but of cause values be "01/01" for every point and it is not what I need.

@liuxuan30
Copy link
Member

in your case, you have to override computeAxisValues() to force return the entries you want to into _axis.entries.

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