SwiftCharts如何在yLabel上隐藏midValue

时间:2018-02-08 06:51:28

标签: swift swiftcharts

looks like

图表库" SwiftCharts"可以显示"默认" maxValue,midValue和minValue,但是如何隐藏midValue" 98" ?请帮帮我,谢谢!

我在SwiftCharts上使用了演示项目: 码 func initializeChart(cell:MarketPriceCell,index:Int){

//初始化数据系列和标签

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文件:

{
 "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 }...

1 个答案:

答案 0 :(得分:0)

y轴的标签在以下功能中设置:

    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!
    }

因此您必须设置自己的yLabel数组(仅包含min和max)并将它们分配给您的图表。 另一个选择是更改上面的代码并删除midvalue的项目:

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