Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Charts - always show limit line

Tags:

ios-charts

I'm using the "Charts" library (by Daniel Gindi) on iOS.

I draw a LineChartView (no issue there), and want to add a limit line to represent the target value:

let targetLine = ChartLimitLine(limit: targetValue, label: "")
lineChartView.leftAxis.addLimitLine(targetLine)

The issue I have is: if the y-values of my chart are too far from the target, the limit line just doesn't show on the chart.

Examples:

  • With a target of 80, and the last value as 59: the limit line does not show. limit line not appearing

  • With a target of 80, and the last value as 79: the limit line does show. limit line appearing

How can I make sure that the limit line will always appear, no matter what the y-values are?

Appendix : here is the rest of my drawing code, it's very standard:

let chartView = LineChartView()
chartView.backgroundColor = UIColor.whiteColor()

chartView.dragEnabled = false
chartView.doubleTapToZoomEnabled = false
chartView.pinchZoomEnabled = false
chartView.highlightPerTapEnabled = false

chartView.descriptionText = ""
chartView.legend.enabled = false
chartView.rightAxis.enabled = false

// Set y axis
let yAxis = chartView.leftAxis
yAxis.removeAllLimitLines()
yAxis.drawZeroLineEnabled = true
yAxis.drawLimitLinesBehindDataEnabled = true
yAxis.valueFormatter = yValuesFormatter

// Set x axis
let xAxis = chartView.xAxis
xAxis.labelPosition = .Bottom
xAxis.drawLabelsEnabled = true
xAxis.drawLimitLinesBehindDataEnabled = true
xAxis.avoidFirstLastClippingEnabled = true


// Create a new dataset
let dataSet = LineChartDataSet(yVals: entries, label: "")
dataSet.drawValuesEnabled = false
dataSet.lineWidth = 2
dataSet.colors = [UIColor.customBlue]
dataSet.circleRadius = 5
dataSet.circleColors = [UIColor.customBlue]
dataSet.drawCircleHoleEnabled = false
dataSet.fillColor = UIColor.cityzenAccent
dataSet.fillAlpha = 0.5
dataSet.drawFilledEnabled = (chartType == .linefill) ? true : false

let data = LineChartData(xVals: xValues, dataSets: [dataSet])
chartView.data = data

The code for the limit lines takes place AFTER all that.

Thanks

like image 587
Frédéric Adda Avatar asked Nov 20 '22 11:11

Frédéric Adda


1 Answers

I faced a similar problem, but in my case, I solved it by changing the maximum of the axis.

By changing the axis.maximum, you can always display the limitLine.

chartView.leftAxis.maximum = max(*limitLineValue*, *maxValueInDataSet*)
like image 56
shindyu dev Avatar answered Mar 24 '23 20:03

shindyu dev