Skip to content

Commit

Permalink
Add label colors to legend entries (#3558)
Browse files Browse the repository at this point in the history
* add label colors to legend entries

* Change interface of LegendEntry
  • Loading branch information
pmairoldi authored and jjatie committed Jul 20, 2018
1 parent 1d44ab3 commit 7a5239a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 107 deletions.
29 changes: 7 additions & 22 deletions Source/Charts/Components/LegendEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,21 @@ open 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 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.
@objc public init(label: String?,
form: Legend.Form,
formSize: CGFloat,
formLineWidth: CGFloat,
formLineDashPhase: CGFloat,
formLineDashLengths: [CGFloat]?,
formColor: NSUIColor?)
@objc public init(label: String?)
{
self.label = label
self.form = form
self.formSize = formSize
self.formLineWidth = formLineWidth
self.formLineDashPhase = formLineDashPhase
self.formLineDashLengths = formLineDashLengths
self.formColor = formColor
}

/// The legend entry text.
/// A `nil` label will start a group.
@objc open var label: String?


/// The color for drawing the label
@objc open var labelColor: NSUIColor?

/// The form to draw for this entry.
///
/// `None` will avoid drawing a form, and any related space.
Expand Down
144 changes: 59 additions & 85 deletions Source/Charts/Renderers/LegendRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,24 @@ open class LegendRenderer: NSObject, Renderer

for j in 0..<min(clrs.count, bds.stackSize)
{
entries.append(
LegendEntry(
label: sLabels[j % sLabels.count],
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: clrs[j]
)
)
let entry = LegendEntry(label: sLabels[j % sLabels.count])
entry.form = dataSet.form
entry.formSize = dataSet.formSize
entry.formLineWidth = dataSet.formLineWidth
entry.formLineDashPhase = dataSet.formLineDashPhase
entry.formLineDashLengths = dataSet.formLineDashLengths
entry.formColor = clrs[j]

entries.append(entry)
}

if dataSet.label != nil
{
// add the legend description label

entries.append(
LegendEntry(
label: dataSet.label,
form: .none,
formSize: CGFloat.nan,
formLineWidth: CGFloat.nan,
formLineDashPhase: 0.0,
formLineDashLengths: nil,
formColor: nil
)
)
let entry = LegendEntry(label: dataSet.label)
entry.form = .none

entries.append(entry)
}
}
else if dataSet is PieChartDataSetProtocol
Expand All @@ -92,64 +82,50 @@ open class LegendRenderer: NSObject, Renderer

for j in 0..<min(clrs.count, entryCount)
{
entries.append(
LegendEntry(
label: (pds.entryForIndex(j) as? PieChartDataEntry)?.label,
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: clrs[j]
)
)
let entry = LegendEntry(label: (pds.entryForIndex(j) as? PieChartDataEntry)?.label)
entry.form = dataSet.form
entry.formSize = dataSet.formSize
entry.formLineWidth = dataSet.formLineWidth
entry.formLineDashPhase = dataSet.formLineDashPhase
entry.formLineDashLengths = dataSet.formLineDashLengths
entry.formColor = clrs[j]

entries.append(entry)
}

if dataSet.label != nil
{
// add the legend description label

entries.append(
LegendEntry(
label: dataSet.label,
form: .none,
formSize: CGFloat.nan,
formLineWidth: CGFloat.nan,
formLineDashPhase: 0.0,
formLineDashLengths: nil,
formColor: nil
)
)
let entry = LegendEntry(label: dataSet.label)
entry.form = .none

entries.append(entry)
}
}
else if dataSet is CandleChartDataSetProtocol &&
(dataSet as! CandleChartDataSetProtocol).decreasingColor != nil
{
let candleDataSet = dataSet as! CandleChartDataSetProtocol

entries.append(
LegendEntry(
label: nil,
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: candleDataSet.decreasingColor
)
)

entries.append(
LegendEntry(
label: dataSet.label,
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: candleDataSet.increasingColor
)
)

let decreasingEntry = LegendEntry(label: nil)
decreasingEntry.form = dataSet.form
decreasingEntry.formSize = dataSet.formSize
decreasingEntry.formLineWidth = dataSet.formLineWidth
decreasingEntry.formLineDashPhase = dataSet.formLineDashPhase
decreasingEntry.formLineDashLengths = dataSet.formLineDashLengths
decreasingEntry.formColor = candleDataSet.decreasingColor

entries.append(decreasingEntry)

let increasingEntry = LegendEntry(label: dataSet.label)
increasingEntry.form = dataSet.form
increasingEntry.formSize = dataSet.formSize
increasingEntry.formLineWidth = dataSet.formLineWidth
increasingEntry.formLineDashPhase = dataSet.formLineDashPhase
increasingEntry.formLineDashLengths = dataSet.formLineDashLengths
increasingEntry.formColor = candleDataSet.increasingColor

entries.append(increasingEntry)
}
else
{ // all others
Expand All @@ -167,18 +143,16 @@ open class LegendRenderer: NSObject, Renderer
{ // add label to the last entry
label = dataSet.label
}

entries.append(
LegendEntry(
label: label,
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
formLineDashPhase: dataSet.formLineDashPhase,
formLineDashLengths: dataSet.formLineDashLengths,
formColor: clrs[j]
)
)

let entry = LegendEntry(label: label)
entry.form = dataSet.form
entry.formSize = dataSet.formSize
entry.formLineWidth = dataSet.formLineWidth
entry.formLineDashPhase = dataSet.formLineDashPhase
entry.formLineDashLengths = dataSet.formLineDashLengths
entry.formColor = clrs[j]

entries.append(entry)
}
}
}
Expand Down Expand Up @@ -376,7 +350,7 @@ open class LegendRenderer: NSObject, Renderer
y: posY,
label: e.label!,
font: labelFont,
textColor: labelTextColor)
textColor: e.labelColor ?? labelTextColor)

if direction == .leftToRight
{
Expand Down Expand Up @@ -468,12 +442,12 @@ open class LegendRenderer: NSObject, Renderer

if !wasStacked
{
drawLabel(context: context, x: posX, y: posY, label: e.label!, font: labelFont, textColor: labelTextColor)
drawLabel(context: context, x: posX, y: posY, label: e.label!, font: labelFont, textColor: e.labelColor ?? labelTextColor)
}
else
{
posY += labelLineHeight + yEntrySpace
drawLabel(context: context, x: posX, y: posY, label: e.label!, font: labelFont, textColor: labelTextColor)
drawLabel(context: context, x: posX, y: posY, label: e.label!, font: labelFont, textColor: e.labelColor ?? labelTextColor)
}

// make a step down
Expand Down

0 comments on commit 7a5239a

Please sign in to comment.