Skip to content

Commit

Permalink
Allow custom legend data
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed May 19, 2015
1 parent fc563cb commit 98cfe18
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
5 changes: 4 additions & 1 deletion Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate

_xAxisRenderer?.computeAxis(xValAverageLength: _data.xValAverageLength, xValues: _data.xVals);

_legendRenderer?.computeLegend(_data);
if (_legend !== nil && !_legend.isLegendCustom)
{
_legendRenderer?.computeLegend(_data);
}

calculateOffsets();

Expand Down
5 changes: 4 additions & 1 deletion Charts/Classes/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public class PieRadarChartViewBase: ChartViewBase

calcMinMax();

_legendRenderer.computeLegend(_data);
if (_legend !== nil && !_legend.isLegendCustom)
{
_legendRenderer.computeLegend(_data);
}

calculateOffsets();

Expand Down
5 changes: 4 additions & 1 deletion Charts/Classes/Charts/RadarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ public class RadarChartView: PieRadarChartViewBase
_yAxisRenderer?.computeAxis(yMin: _yAxis.axisMinimum, yMax: _yAxis.axisMaximum);
_xAxisRenderer?.computeAxis(xValAverageLength: _data.xValAverageLength, xValues: _data.xVals);

_legendRenderer?.computeLegend(_data);
if (_legend !== nil && !_legend.isLegendCustom)
{
_legendRenderer?.computeLegend(_data);
}

calculateOffsets();

Expand Down
63 changes: 55 additions & 8 deletions Charts/Classes/Components/ChartLegend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ public class ChartLegend: ChartComponentBase

/// the legend colors array, each color is for the form drawn at the same index
public var colors = [UIColor?]()

// the legend text array. a nil label will start a group.
public var labels = [String?]()

/// colors that will be appended to the end of the colors array after calculating the legend.
public var extraColors = [UIColor?]()

/// labels that will be appended to the end of the labels array after calculating the legend. a nil label will start a group.
public var extraLabels = [String?]()

/// Are the legend labels/colors a custom value or auto calculated? If false, then it's auto, if true, then custom.
/// :default: false (automatic legend)
private var _isLegendCustom = false

public var position = ChartLegendPosition.BelowChartLeft
public var direction = ChartLegendDirection.LeftToRight
Expand Down Expand Up @@ -204,25 +208,54 @@ public class ChartLegend: ChartComponentBase
textHeightMax = maxEntrySize.height;
}
}


/// MARK: - Custom legend

/// Sets a custom legend's labels and colors arrays.
/// The colors count should match the labels count.
/// * Each color is for the form drawn at the same index.
/// * A nil label will start a group.
/// * A nil color will avoid drawing a form, and a clearColor will leave a space for the form.
/// This will disable the feature that automatically calculates the legend labels and colors from the datasets.
/// Call resetLegendToAuto(...) to re-enable automatic calculation (and then notifyDataSetChanged() is needed).
public func setLegend(#colors: [UIColor?], labels: [String?])
{
self.labels = labels;
self.colors = colors;
_isLegendCustom = true;
}

/// Calling this will disable the custom legend labels (set by setLegend(...)). Instead, the labels will again be calculated automatically (after notifyDataSetChanged() is called).
public func resetLegendToAuto()
{
_isLegendCustom = false;
}

/// Returns true if a custom legend labels and colors has been set
/// :default: false (automatic legend)
public var isLegendCustom: Bool
{
return _isLegendCustom;
}

/// MARK: - ObjC compatibility

/// the legend colors array, each color is for the form drawn at the same index
/// (ObjC bridging functions, as Swift 1.2 does not bridge optionals in array to NSNulls)
public var colorsObjc: [NSObject]
{
get { return ChartUtils.bridgedObjCGetUIColorArray(swift: colors); }
set { self.colors = ChartUtils.bridgedObjCGetUIColorArray(objc: newValue); }
}

// the legend text array. a nil label will start a group.
/// (ObjC bridging functions, as Swift 1.2 does not bridge optionals in array to NSNulls)
public var labelsObjc: [NSObject]
{
get { return ChartUtils.bridgedObjCGetStringArray(swift: labels); }
set { self.labels = ChartUtils.bridgedObjCGetStringArray(objc: newValue); }
}

/// colors that will be appended to the end of the colors array after calculating the legend.
/// this will not be used when using customLabels/customColors
/// (ObjC bridging functions, as Swift 1.2 does not bridge optionals in array to NSNulls)
Expand All @@ -231,7 +264,7 @@ public class ChartLegend: ChartComponentBase
get { return ChartUtils.bridgedObjCGetUIColorArray(swift: extraColors); }
set { self.extraColors = ChartUtils.bridgedObjCGetUIColorArray(objc: newValue); }
}

/// labels that will be appended to the end of the labels array after calculating the legend. a nil label will start a group.
/// this will not be used when using customLabels/customColors
/// (ObjC bridging functions, as Swift 1.2 does not bridge optionals in array to NSNulls)
Expand All @@ -240,4 +273,18 @@ public class ChartLegend: ChartComponentBase
get { return ChartUtils.bridgedObjCGetStringArray(swift: extraLabels); }
set { self.extraLabels = ChartUtils.bridgedObjCGetStringArray(objc: newValue); }
}

/// Sets a custom legend's labels and colors arrays.
/// The colors count should match the labels count.
/// * Each color is for the form drawn at the same index.
/// * A nil label will start a group.
/// * A nil color will avoid drawing a form, and a clearColor will leave a space for the form.
/// This will disable the feature that automatically calculates the legend labels and colors from the datasets.
/// Call resetLegendToAuto(...) to re-enable automatic calculation, and then if needed - call notifyDataSetChanged() on the chart to make it refresh the data.
public func setLegend(#colors: [NSObject], labels: [NSObject])
{
self.labelsObjc = labels;
self.colorsObjc = colors;
_isLegendCustom = true;
}
}

0 comments on commit 98cfe18

Please sign in to comment.