Skip to content

Commit

Permalink
Added clamping function for Comparable (ChartsOrg#3435)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjatie authored and Shineeth Hamza committed Oct 31, 2018
1 parent c827034 commit e9d6892
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 59 deletions.
14 changes: 3 additions & 11 deletions Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,9 @@ open class AxisBase: ComponentBase
}
set
{
_labelCount = newValue

if _labelCount > axisMaxLabels
{
_labelCount = axisMaxLabels
}
if _labelCount < axisMinLabels
{
_labelCount = axisMinLabels
}

let range = axisMinLabels...axisMaxLabels as ClosedRange
_labelCount = newValue.clamped(to: range)

forceLabelsEnabled = false
}
}
Expand Down
13 changes: 1 addition & 12 deletions Source/Charts/Components/ChartLimitLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,7 @@ open class ChartLimitLine: ComponentBase
}
set
{
if newValue < 0.2
{
_lineWidth = 0.2
}
else if newValue > 12.0
{
_lineWidth = 12.0
}
else
{
_lineWidth = newValue
}
_lineWidth = newValue.clamped(to: 0.2...12)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,14 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, ICandleChartD
/// **default**: 0.1 (10%), max 0.45, min 0.0
open var barSpace: CGFloat
{
set
{
if newValue < 0.0
{
_barSpace = 0.0
}
else if newValue > 0.45
{
_barSpace = 0.45
}
else
{
_barSpace = newValue
}
}
get
{
return _barSpace
}
set
{
_barSpace = newValue.clamped(to: 0...0.45)
}
}

/// should the candle bars show?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,7 @@ open class LineChartDataSet: LineRadarChartDataSet, ILineChartDataSet
}
set
{
_cubicIntensity = newValue
if _cubicIntensity > 1.0
{
_cubicIntensity = 1.0
}
if _cubicIntensity < 0.05
{
_cubicIntensity = 0.05
}
_cubicIntensity = newValue.clamped(to: 0.05...1)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,7 @@ open class LineRadarChartDataSet: LineScatterCandleRadarChartDataSet, ILineRadar
}
set
{
if newValue < 0.0
{
_lineWidth = 0.0
}
else if newValue > 10.0
{
_lineWidth = 10.0
}
else
{
_lineWidth = newValue
}
_lineWidth = newValue.clamped(to: 0...10)
}
}

Expand Down
12 changes: 12 additions & 0 deletions Source/Charts/Utils/ChartUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
import Foundation
import CoreGraphics

extension Comparable {
func clamped(to range: ClosedRange<Self>) -> Self {
if self > range.upperBound {
return range.upperBound
} else if self < range.lowerBound {
return range.lowerBound
} else {
return self
}
}
}

extension FloatingPoint
{
var DEG2RAD: Self
Expand Down

0 comments on commit e9d6892

Please sign in to comment.