LineChartDataSet.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // LineChartDataSet.swift
  3. // Charts
  4. //
  5. // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
  6. // A port of MPAndroidChart for iOS
  7. // Licensed under Apache License 2.0
  8. //
  9. // https://github.com/danielgindi/Charts
  10. //
  11. import Foundation
  12. import CoreGraphics
  13. open class LineChartDataSet: LineRadarChartDataSet, LineChartDataSetProtocol
  14. {
  15. @objc(LineChartMode)
  16. public enum Mode: Int
  17. {
  18. case linear
  19. case stepped
  20. case cubicBezier
  21. case horizontalBezier
  22. }
  23. private func initialize()
  24. {
  25. // default color
  26. circleColors.append(NSUIColor(red: 140.0/255.0, green: 234.0/255.0, blue: 255.0/255.0, alpha: 1.0))
  27. }
  28. public required init()
  29. {
  30. super.init()
  31. initialize()
  32. }
  33. public override init(entries: [ChartDataEntry], label: String)
  34. {
  35. super.init(entries: entries, label: label)
  36. initialize()
  37. }
  38. // MARK: - Data functions and accessors
  39. // MARK: - Styling functions and accessors
  40. /// The drawing mode for this line dataset
  41. ///
  42. /// **default**: Linear
  43. open var mode: Mode = Mode.linear
  44. private var _cubicIntensity = CGFloat(0.2)
  45. /// Intensity for cubic lines (min = 0.05, max = 1)
  46. ///
  47. /// **default**: 0.2
  48. open var cubicIntensity: CGFloat
  49. {
  50. get
  51. {
  52. return _cubicIntensity
  53. }
  54. set
  55. {
  56. _cubicIntensity = newValue.clamped(to: 0.05...1)
  57. }
  58. }
  59. open var isDrawLineWithGradientEnabled = false
  60. open var gradientPositions: [CGFloat]?
  61. /// The radius of the drawn circles.
  62. open var circleRadius = CGFloat(8.0)
  63. /// The hole radius of the drawn circles
  64. open var circleHoleRadius = CGFloat(4.0)
  65. open var circleColors = [NSUIColor]()
  66. /// - Returns: The color at the given index of the DataSet's circle-color array.
  67. /// Performs a IndexOutOfBounds check by modulus.
  68. open func getCircleColor(atIndex index: Int) -> NSUIColor?
  69. {
  70. let size = circleColors.count
  71. let index = index % size
  72. if index >= size
  73. {
  74. return nil
  75. }
  76. return circleColors[index]
  77. }
  78. /// Sets the one and ONLY color that should be used for this DataSet.
  79. /// Internally, this recreates the colors array and adds the specified color.
  80. open func setCircleColor(_ color: NSUIColor)
  81. {
  82. circleColors.removeAll(keepingCapacity: false)
  83. circleColors.append(color)
  84. }
  85. open func setCircleColors(_ colors: NSUIColor...)
  86. {
  87. circleColors.removeAll(keepingCapacity: false)
  88. circleColors.append(contentsOf: colors)
  89. }
  90. /// Resets the circle-colors array and creates a new one
  91. open func resetCircleColors(_ index: Int)
  92. {
  93. circleColors.removeAll(keepingCapacity: false)
  94. }
  95. /// If true, drawing circles is enabled
  96. open var drawCirclesEnabled = true
  97. /// `true` if drawing circles for this DataSet is enabled, `false` ifnot
  98. open var isDrawCirclesEnabled: Bool { return drawCirclesEnabled }
  99. /// The color of the inner circle (the circle-hole).
  100. open var circleHoleColor: NSUIColor? = NSUIColor.white
  101. /// `true` if drawing circles for this DataSet is enabled, `false` ifnot
  102. open var drawCircleHoleEnabled = true
  103. /// `true` if drawing the circle-holes is enabled, `false` ifnot.
  104. open var isDrawCircleHoleEnabled: Bool { return drawCircleHoleEnabled }
  105. /// This is how much (in pixels) into the dash pattern are we starting from.
  106. open var lineDashPhase = CGFloat(0.0)
  107. /// This is the actual dash pattern.
  108. /// I.e. [2, 3] will paint [-- -- ]
  109. /// [1, 3, 4, 2] will paint [- ---- - ---- ]
  110. open var lineDashLengths: [CGFloat]?
  111. /// Line cap type, default is CGLineCap.Butt
  112. open var lineCapType = CGLineCap.butt
  113. /// formatter for customizing the position of the fill-line
  114. private var _fillFormatter: FillFormatter = DefaultFillFormatter()
  115. /// Sets a custom FillFormatterProtocol to the chart that handles the position of the filled-line for each DataSet. Set this to null to use the default logic.
  116. open var fillFormatter: FillFormatter?
  117. {
  118. get
  119. {
  120. return _fillFormatter
  121. }
  122. set
  123. {
  124. _fillFormatter = newValue ?? DefaultFillFormatter()
  125. }
  126. }
  127. // MARK: NSCopying
  128. open override func copy(with zone: NSZone? = nil) -> Any
  129. {
  130. let copy = super.copy(with: zone) as! LineChartDataSet
  131. copy.circleColors = circleColors
  132. copy.circleHoleColor = circleHoleColor
  133. copy.circleRadius = circleRadius
  134. copy.circleHoleRadius = circleHoleRadius
  135. copy.cubicIntensity = cubicIntensity
  136. copy.lineDashPhase = lineDashPhase
  137. copy.lineDashLengths = lineDashLengths
  138. copy.lineCapType = lineCapType
  139. copy.drawCirclesEnabled = drawCirclesEnabled
  140. copy.drawCircleHoleEnabled = drawCircleHoleEnabled
  141. copy.mode = mode
  142. copy._fillFormatter = _fillFormatter
  143. return copy
  144. }
  145. }