Search code examples
swiftswiftcharts

SwiftCharts how to hide the midValue on yLabel


looks like

The charting library "SwiftCharts" can show the "Default" maxValue , midValue and the minValue, but how to hide the midValue "98 " ? please help me, thanks!

I used the demo project on the SwiftCharts: Code func initializeChart(cell: MarketPriceCell, index: Int) {

// Initialize data series and labels

var serieData: [Double] = []
var labels: [Double] = []
var labelsAsString: Array<String> = []
let beginInd = 0
let oneFour = stockValuesAll.count / 4
let twoFour = (stockValuesAll.count * 2) / 4
let thrFour = (stockValuesAll.count * 3) / 4
labels = [Double(beginInd), (Double(oneFour)), (Double(twoFour)),(Double(thrFour))]
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let ddate1 = dateFormatter.string(from: stockValuesAll[beginInd]["date"] as! Date)
let ddate2 = dateFormatter.string(from: stockValuesAll[oneFour]["date"] as! Date)
let ddate3 = dateFormatter.string(from: stockValuesAll[twoFour]["date"] as! Date)
let ddate4 = dateFormatter.string(from: stockValuesAll[thrFour]["date"] as! Date)

labelsAsString = [ddate1,ddate2,ddate3,ddate4]
/// Date formatter to retrieve the month names
for (_, value) in stockValuesAll.enumerated() {
    serieData.append(value["close"] as! Double)

}
let series = ChartSeries(serieData)
series.area = true
// Configure chart layout
cell.stkChart.lineWidth = 0.5
cell.stkChart.labelFont = UIFont.systemFont(ofSize: 12)
cell.stkChart.xLabels = labels
cell.stkChart.xLabelsFormatter = { (labelIndex: Int, labelValue: Double) -> String in
    return labelsAsString[labelIndex]
}
cell.stkChart.xLabelsTextAlignment = .left
cell.stkChart.minY = serieData.min()! - 5
cell.stkChart.add(series)

Json File:

{
 "quotes": 
[
 { "date": "2017-07-01", "close": 93.52 },
 { "date": "2017-07-02", "close": 93.48 },
 { "date": "2017-07-03", "close": 94.03 },
 { "date": "2017-07-07", "close": 95.97 }...

Solution

  • The labels for the y-axis are set in the following function:

        fileprivate func drawLabelsAndGridOnYAxis() {
        let context = UIGraphicsGetCurrentContext()!
        context.setStrokeColor(gridColor.cgColor)
        context.setLineWidth(0.5)
    
        var labels: [Double]
        if yLabels == nil {
            labels = [(min.y + max.y) / 2, max.y]
            if yLabelsOnRightSide || min.y != 0 {
                labels.insert(min.y, at: 0)
            }
        } else {
            labels = yLabels!
        }
    

    so you would have to set your own array of yLabels (which contain only min and max) and assign them to your chart. Another option would be to change the above code and remove the item for the midvalue:

    if yLabels == nil {
        labels = [max.y]