ivnsch / SwiftCharts

Easy to use and highly customizable charts library for iOS
Apache License 2.0
2.53k stars 410 forks source link

how to make the width spacing to be fixed between x-axis element if the date of each element is too far #345

Closed winstonkan closed 6 years ago

winstonkan commented 6 years ago
 func createChartPoint(dateStr: String, percent: Int, readFormatter: DateFormatter, displayFormatter: DateFormatter) -> ChartPoint {
    return ChartPoint(x: createDateAxisValue(dateStr, readFormatter: readFormatter, displayFormatter: displayFormatter), y: ChartAxisValuePercent(percent))
}

func createDateAxisValue(_ dateStr: String, readFormatter: DateFormatter, displayFormatter: DateFormatter) -> ChartAxisValue {
    let labelSettings = ChartLabelSettings(font: UIFont.systemFont(ofSize: 12), rotation: 90, rotationKeep: .top)
    if dateStr == "" {
        return ChartAxisValueString(order: 0, labelSettings: labelSettings)
    }
    let date = readFormatter.date(from: dateStr)!

    return ChartAxisValueDate(date: date, formatter: displayFormatter, labelSettings: labelSettings)
}

    var chartPoints1 = [
        createChartPoint(dateStr: self.allLabResult[0].requestDate!, percent: Int(self.allLabResult[0].result!)!, readFormatter: readFormatter, displayFormatter: displayFormatter)
    ]

    let allChartPoints = (chartPoints1).sorted {(obj1, obj2) in return obj1.x.scalar < obj2.x.scalar}

    let xValues: [ChartAxisValue] = (NSOrderedSet(array: allChartPoints).array as! [ChartPoint]).map{$0.x}
    let yValues = ChartAxisValuesStaticGenerator.generateYAxisValuesWithChartPoints(allChartPoints, minSegmentCount: 5, maxSegmentCount: 20, multiple: 50, axisValueGenerator: {ChartAxisValueDouble($0, labelSettings: labelSettings)}, addPaddingSegmentIfEdge: false)

30697849_2441684792524676_8819559671246356480_n