Skip to content

Commit

Permalink
Added feature for dashing legend line forms
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Aug 14, 2016
1 parent 6f51a55 commit e01c1be
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
12 changes: 12 additions & 0 deletions Charts/Classes/Components/ChartLegend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ public class ChartLegend: ChartComponentBase
/// The line width for forms that consist of lines
public var formLineWidth = CGFloat(1.5)

/// Line dash configuration for shapes that consist of lines.
///
/// This is how much (in pixels) into the dash pattern are we starting from.
public var formLineDashPhase: CGFloat = 0.0

/// Line dash configuration for shapes that consist of lines.
///
/// This is the actual dash pattern.
/// I.e. [2, 3] will paint [-- -- ]
/// [1, 3, 4, 2] will paint [- ---- - ---- ]
public var formLineDashLengths: [CGFloat]?

public var xEntrySpace = CGFloat(6.0)
public var yEntrySpace = CGFloat(0.0)
public var formToTextSpace = CGFloat(5.0)
Expand Down
36 changes: 29 additions & 7 deletions Charts/Classes/Components/LegendEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,28 @@ public class LegendEntry: NSObject
super.init()
}

/// - parameter label: The legend entry text.
/// A `nil` label will start a group.
/// - parameter form: The form to draw for this entry.
/// - parameter formSize: Set as NaN to use the legend's default
/// - parameter formLineWidth: Set as NaN to use the legend's default
/// - parameter formColor: The color for drawing the form
/// - parameter label: The legend entry text.
/// A `nil` label will start a group.
/// - parameter form: The form to draw for this entry.
/// - parameter formSize: Set to NaN to use the legend's default.
/// - parameter formLineWidth: Set to NaN to use the legend's default.
/// - parameter formLineDashPhase: Line dash configuration.
/// - parameter formLineDashLengths: Line dash configurationas NaN to use the legend's default.
/// - parameter formColor: The color for drawing the form.
public init(label: String?,
form: ChartLegend.Form,
formSize: CGFloat,
formLineWidth: CGFloat,
formLineDashPhase: CGFloat,
formLineDashLengths: [CGFloat]?,
formColor: NSUIColor?)
{
self.label = label
self.form = form
self.formSize = formSize
self.formLineWidth = formLineWidth
self.formLineDashPhase = formLineDashPhase
self.formLineDashLengths = formLineDashLengths
self.formColor = formColor
}

Expand All @@ -61,9 +67,25 @@ public class LegendEntry: NSObject

/// Line width used for shapes that consist of lines.
///
/// Set as NaN to use the legend's default
/// Set to NaN to use the legend's default.
public var formLineWidth: CGFloat = CGFloat.NaN

/// Line dash configuration for shapes that consist of lines.
///
/// This is how much (in pixels) into the dash pattern are we starting from.
///
/// Set to NaN to use the legend's default.
public var formLineDashPhase: CGFloat = 0.0

/// Line dash configuration for shapes that consist of lines.
///
/// This is the actual dash pattern.
/// I.e. [2, 3] will paint [-- -- ]
/// [1, 3, 4, 2] will paint [- ---- - ---- ]
///
/// Set to nil to use the legend's default.
public var formLineDashLengths: [CGFloat]?

/// The color for drawing the form
public var formColor: NSUIColor?
}
12 changes: 12 additions & 0 deletions Charts/Classes/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ public class ChartBaseDataSet: NSObject, IChartDataSet
/// Return `NaN` to use the default legend form line width.
public var formLineWidth: CGFloat = CGFloat.NaN

/// Line dash configuration for legend shapes that consist of lines.
///
/// This is how much (in pixels) into the dash pattern are we starting from.
public var formLineDashPhase: CGFloat = 0.0

/// Line dash configuration for legend shapes that consist of lines.
///
/// This is the actual dash pattern.
/// I.e. [2, 3] will paint [-- -- ]
/// [1, 3, 4, 2] will paint [- ---- - ---- ]
public var formLineDashLengths: [CGFloat]? = nil

/// Set this to true to draw y-values on the chart
public var drawValuesEnabled = true

Expand Down
12 changes: 12 additions & 0 deletions Charts/Classes/Data/Interfaces/IChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ public protocol IChartDataSet
/// Return `NaN` to use the default legend form line width.
var formLineWidth: CGFloat { get }

/// Line dash configuration for legend shapes that consist of lines.
///
/// This is how much (in pixels) into the dash pattern are we starting from.
var formLineDashPhase: CGFloat { get }

/// Line dash configuration for legend shapes that consist of lines.
///
/// This is the actual dash pattern.
/// I.e. [2, 3] will paint [-- -- ]
/// [1, 3, 4, 2] will paint [- ---- - ---- ]
var formLineDashLengths: [CGFloat]? { get }

/// Set this to true to draw y-values on the chart
var drawValuesEnabled: Bool { get set }

Expand Down
26 changes: 26 additions & 0 deletions Charts/Classes/Renderers/LegendRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class LegendRenderer: Renderer
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: clrs[j]
)
)
Expand All @@ -78,6 +80,8 @@ public class LegendRenderer: Renderer
form: .None,
formSize: CGFloat.NaN,
formLineWidth: CGFloat.NaN,
formLineDashPhase: 0.0,
formLineDashLengths: nil,
formColor: nil
)
)
Expand All @@ -95,6 +99,8 @@ public class LegendRenderer: Renderer
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: clrs[j]
)
)
Expand All @@ -110,6 +116,8 @@ public class LegendRenderer: Renderer
form: .None,
formSize: CGFloat.NaN,
formLineWidth: CGFloat.NaN,
formLineDashPhase: 0.0,
formLineDashLengths: nil,
formColor: nil
)
)
Expand All @@ -126,6 +134,8 @@ public class LegendRenderer: Renderer
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: candleDataSet.decreasingColor
)
)
Expand All @@ -136,6 +146,8 @@ public class LegendRenderer: Renderer
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: candleDataSet.increasingColor
)
)
Expand Down Expand Up @@ -163,6 +175,8 @@ public class LegendRenderer: Renderer
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: clrs[j]
)
)
Expand Down Expand Up @@ -522,8 +536,20 @@ public class LegendRenderer: Renderer
case .Line:

let formLineWidth = isnan(entry.formLineWidth) ? legend.formLineWidth : entry.formLineWidth
let formLineDashPhase = isnan(entry.formLineDashPhase) ? legend.formLineDashPhase : entry.formLineDashPhase
let formLineDashLengths = entry.formLineDashLengths == nil ? legend.formLineDashLengths : entry.formLineDashLengths

CGContextSetLineWidth(context, formLineWidth)

if formLineDashLengths != nil && formLineDashLengths!.count > 0
{
CGContextSetLineDash(context, formLineDashPhase, formLineDashLengths!, formLineDashLengths!.count)
}
else
{
CGContextSetLineDash(context, 0.0, nil, 0)
}

CGContextSetStrokeColorWithColor(context, formColor.CGColor)

_formLineSegmentsBuffer[0].x = x
Expand Down
3 changes: 3 additions & 0 deletions ChartsDemo/Classes/Demos/LineChart1ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ - (void)setDataCount:(int)count range:(double)range
set1.circleRadius = 3.0;
set1.drawCircleHoleEnabled = NO;
set1.valueFont = [UIFont systemFontOfSize:9.f];
set1.formLineDashLengths = @[@5.f, @2.5f];
set1.formLineWidth = 1.0;
set1.formSize = 15.0;

NSArray *gradientColors = @[
(id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
Expand Down

0 comments on commit e01c1be

Please sign in to comment.