5

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

Frederic Adda
  • 5,905
  • 4
  • 56
  • 71

3 Answers3

3

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*)
  • In =my case the XAxis.axisMaximum = 6 and my series of x is 600s values occuring regularly at 0.1s. So the xValue is computed as i/100 which does not print. If I make it 602 items it prints fine. Finding. if last xValue is axisMaximum no graph is printed. – user462990 Jan 13 '22 at 14:24
0

I'm not sure I understood your propose, but I think you should set axisMaximum to 80 or over it. You say your graph is static , so maybe you can limit left Y Axis's range in 40 ~ 90 by set axisMinimum and axisMaximum.

If you just want to show the all chart without scale, just call

chart.setScaleEnabled(false)
Y.Bi
  • 657
  • 6
  • 11
  • The thing is: I can't give a fixed value for the maximum. It has to be calculated for each chart. – Frederic Adda Feb 20 '17 at 11:23
  • @FrédéricAdda I should say, if the axisMaximum is auto-calculated, and if it's small than 80, then you will never see a limit line for y value at 80.But if the axisMaximum is always large able to show the limit line, the problem is just about scaling, see my answer, I updated it. – Y.Bi Feb 20 '17 at 12:04
-1
  /// if the chart is fully zoomed out, return true
    open var isFullyZoomedOut: Bool
    {
        return isFullyZoomedOutX && isFullyZoomedOutY
    }

    /// - returns: `true` if the chart is fully zoomed out on it's y-axis (vertical).
    open var isFullyZoomedOutY: Bool
    {
        return !(_scaleY > _minScaleY || _minScaleY > 1.0)
    }
AFTAB MUHAMMED KHAN
  • 2,189
  • 3
  • 18
  • 24