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 1 commit
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
18 changes: 12 additions & 6 deletions Source/Charts/Components/LegendEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ open class LegendEntry: NSObject

/// - parameter label: The legend entry text.
/// A `nil` label will start a group.
/// - parameter labelColor: The color for drawing the label.
/// - 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?)
labelColor: NSUIColor?,
form: Legend.Form,
formSize: CGFloat,
formLineWidth: CGFloat,
formLineDashPhase: CGFloat,
formLineDashLengths: [CGFloat]?,
formColor: NSUIColor?)
{
self.label = label
self.labelColor = labelColor
self.form = form
self.formSize = formSize
self.formLineWidth = formLineWidth
Expand All @@ -53,6 +56,9 @@ open class LegendEntry: NSObject
/// A `nil` label will start a group.
@objc open var label: String?

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

/// The form to draw for this entry.
///
/// `None` will avoid drawing a form, and any related space.
Expand Down
13 changes: 10 additions & 3 deletions Source/Charts/Renderers/LegendRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ open class LegendRenderer: NSObject, Renderer
entries.append(
LegendEntry(
label: sLabels[j % sLabels.count],
labelColor: nil,
Copy link
Member

Choose a reason for hiding this comment

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

is it possible to use default value? seeing labelColor: nil repeat

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could but the rest of that function doesn’t. Seems weird to have a default at the start of the function. That’s my preference though

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could but the rest of that function doesn’t. Seems weird to have a default at the start of the function. That’s my preference though

Copy link
Member

Choose a reason for hiding this comment

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

ok. if @jjatie has no question or no response for a while, we could merge then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Cool. Trying to finish up 4.0

Copy link
Collaborator

@jjatie jjatie Jul 17, 2018

Choose a reason for hiding this comment

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

I think the issue is that we are putting too many things in the initializer. It's already pretty big and if we add more functionality and expose through the initializer, it will only get bigger. The only thing we need to create a valid LegendEntry is the label, everything else is optional. The label should be a constant and the rest of the properties remain vars. i.e.

@objc(ChartLegendEntry)
open class LegendEntry: NSObject
{
    public override init()
    {
        super.init()
    }
    
    /// - parameter label:                  The legend entry text.
    ///                                     A `nil` label will start a group.
    @objc public init(label: String?)
    {
        self.label = label
    }
    
    /// The legend entry text.
    /// A `nil` label will start a group.
    @objc open let label: String?
    
    /// The color for drawing the label
    open var labelColor: NSUIColor?
        
    /// The form to draw for this entry.
    ///
    /// `None` will avoid drawing a form, and any related space.
    /// `Empty` will avoid drawing a form, but keep its space.
    /// `Default` will use the Legend's default.
    @objc open var form: Legend.Form = .default
    
    /// Form size will be considered except for when .None is used
    ///
    /// Set as NaN to use the legend's default
    @objc open var formSize: CGFloat = CGFloat.nan
    
    /// Line width used for shapes that consist of lines.
    ///
    /// Set to NaN to use the legend's default.
    @objc open 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.
    @objc open 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.
    @objc open var formLineDashLengths: [CGFloat]?
    
    /// The color for drawing the form
    @objc open var formColor: NSUIColor?
}

Copy link
Collaborator

@jjatie jjatie Jul 17, 2018

Choose a reason for hiding this comment

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

It might even make sense to only allow the entry group to be created through the empty initializer, i.e.:

@objc(ChartLegendEntry)
open class LegendEntry: NSObject
{    
    /// A label to start a group.
    public override init()
    {
        super.init()
    }
    
    /// - parameter label: The legend entry text.
    @objc public init(label: String)
    {
        self.label = label
    }
    
    // ...
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Don’t know if the empty initializer is useful but I agree with your points. I’ll change some stuff

form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
Expand All @@ -76,6 +77,7 @@ open class LegendRenderer: NSObject, Renderer
entries.append(
LegendEntry(
label: dataSet.label,
labelColor: nil,
form: .none,
formSize: CGFloat.nan,
formLineWidth: CGFloat.nan,
Expand All @@ -95,6 +97,7 @@ open class LegendRenderer: NSObject, Renderer
entries.append(
LegendEntry(
label: (pds.entryForIndex(j) as? PieChartDataEntry)?.label,
labelColor: nil,
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
Expand All @@ -112,6 +115,7 @@ open class LegendRenderer: NSObject, Renderer
entries.append(
LegendEntry(
label: dataSet.label,
labelColor: nil,
form: .none,
formSize: CGFloat.nan,
formLineWidth: CGFloat.nan,
Expand All @@ -130,6 +134,7 @@ open class LegendRenderer: NSObject, Renderer
entries.append(
LegendEntry(
label: nil,
labelColor: nil,
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
Expand All @@ -142,6 +147,7 @@ open class LegendRenderer: NSObject, Renderer
entries.append(
LegendEntry(
label: dataSet.label,
labelColor: nil,
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
Expand Down Expand Up @@ -171,6 +177,7 @@ open class LegendRenderer: NSObject, Renderer
entries.append(
LegendEntry(
label: label,
labelColor: nil,
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
Expand Down Expand Up @@ -376,7 +383,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 +475,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