iOS Charts: will intermediately not show the x and y axis values

49 Views Asked by At

I have a LineChartView embedded inside a UIView. I then have that "container" UIView added as a subview on a scroll view. about 75% of the time i have no issues the chart shows up fine. But seemingly randomly the chart will display with all the data points but missing the x-axis and y-axis values and the LineChartDataSet.label will be empty. The graph always works when i run it from xcode but then i will open the xcode build a day later and the x and y axis values are missing though i see the graph itself.

Here is what i'm seeing.

Bad enter image description here

Good! enter image description here

Here is my code

Creation of chart and container view

private lazy var elevationChartContainer: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false


        return view
    }()

    private lazy var elevationChart: LineChartView = {
        let chart = LineChartView()
        chart.translatesAutoresizingMaskIntoConstraints = false
        chart.rightAxis.enabled = false
        chart.isUserInteractionEnabled = false
        chart.xAxis.labelPosition = .bottom
        return chart
    }()

Configuration of chart

private func configureElevationGraphData(activity: Activity) {
        let locations = activity.points
        var distanceTravelled: Measurement<UnitLength> = .init(value: 0, unit: .meters)
        var graphPoints = [ChartDataEntry]()
        var priorPoint = locations.first
        for location in locations {
            let convertedDistance = AppConstants.shared.userSelectedUnits == .Imperial ? distanceTravelled.converted(to: .miles).value : distanceTravelled.converted(to: .kilometers).value
            let convertedAltitude = AppConstants.shared.userSelectedUnits == .Imperial ? location.altitude * 3.281 : location.altitude
            let newPoint = ChartDataEntry(x: convertedDistance, y: convertedAltitude)
            graphPoints.append(newPoint)
            let distanceFromLastPoint = priorPoint?.asCLLocation.distance(from: location.asCLLocation) ?? 0
            distanceTravelled = distanceTravelled + Measurement(value: distanceFromLastPoint, unit: .meters)
            priorPoint = location
        }
        if AppConstants.shared.userSelectedUnits == .Imperial {
            if distanceTravelled.converted(to: .miles).value < 1 {
                elevationChart.xAxis.granularity = 0.5
            } else {
                elevationChart.xAxis.granularity = 1.0
            }
        } else {
            if distanceTravelled.converted(to: .kilometers).value < 1 {
                elevationChart.xAxis.granularity = 0.5
            } else {
                elevationChart.xAxis.granularity = 1.0
            }
        }
        let lineChartDataSet = LineChartDataSet(entries: graphPoints)
        lineChartDataSet.mode = .cubicBezier
        lineChartDataSet.drawCirclesEnabled = false
        lineChartDataSet.lineWidth = 3.0
        lineChartDataSet.setColor(AppConstants.purple900)
        lineChartDataSet.fill = ColorFill(color: AppConstants.purple200)
        lineChartDataSet.drawFilledEnabled = true
        let text = AppConstants.shared.userSelectedUnits == .Imperial ? "feet/mi" : "m/km"
        lineChartDataSet.label = text
        let lineChartData = LineChartData(dataSet: lineChartDataSet)
        elevationChart.data = lineChartData
    }

setting constraints on chart

private func setupElevationChartConstraints() {
        elevationChartContainer.topAnchor.constraint(equalTo: elevationHeaderView.bottomAnchor, constant: gapBetweenScrollViewElements).isActive = true
        elevationChartContainer.widthAnchor.constraint(equalTo: mapAndTimeView.widthAnchor).isActive = true
        elevationChartContainer.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
        elevationChartContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4).isActive = true

        elevationChart.leadingAnchor.constraint(equalTo: elevationChartContainer.leadingAnchor).isActive = true
        elevationChart.trailingAnchor.constraint(equalTo: elevationChartContainer.trailingAnchor).isActive = true
        elevationChart.topAnchor.constraint(equalTo: elevationChartContainer.topAnchor).isActive = true
        elevationChart.bottomAnchor.constraint(equalTo: elevationChartContainer.bottomAnchor).isActive = true
    }

I've tried to restart he app several times this doesn't work. the problem is completely intermitent it comes and goes and i never see it when i run it from xcode so i can't put in break points.

0

There are 0 best solutions below