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

Add label colors to legend entries #3558

Merged
merged 2 commits into from
Jul 20, 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
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