Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added clamping function for Comparable #3435

Merged
merged 1 commit into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here 12 is cast to Double by default? is this new swift feature?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been the case since Swift 1

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