Skip to content

Commit

Permalink
Allow limiting the legend size based on a percentage of the chart size
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed May 27, 2015
1 parent 8e32ff6 commit aa1ad23
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 46 deletions.
18 changes: 9 additions & 9 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,19 +352,19 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
if (_legend.position == .RightOfChart
|| _legend.position == .RightOfChartCenter)
{
offsetRight += _legend.textWidthMax + _legend.xOffset * 2.0;
offsetRight += min(_legend.neededWidth, _viewPortHandler.chartWidth * _legend.maxSizePercent) + _legend.xOffset * 2.0;
}
if (_legend.position == .LeftOfChart
|| _legend.position == .LeftOfChartCenter)
{
offsetLeft += _legend.textWidthMax + _legend.xOffset * 2.0;
offsetLeft += min(_legend.neededWidth, _viewPortHandler.chartWidth * _legend.maxSizePercent) + _legend.xOffset * 2.0;
}
else if (_legend.position == .BelowChartLeft
|| _legend.position == .BelowChartRight
|| _legend.position == .BelowChartCenter)
{

offsetBottom += _legend.textHeightMax * 3.0;
var yOffset = _legend.textHeightMax * 2.0; // It's possible that we do not need this offset anymore as it is available through the extraOffsets
offsetBottom += min(_legend.neededHeight + yOffset, _viewPortHandler.chartHeight * _legend.maxSizePercent);
}
}

Expand Down Expand Up @@ -404,13 +404,13 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
offsetBottom += self.extraBottomOffset;
offsetLeft += self.extraLeftOffset;

var min = CGFloat(10.0);
var minOffset = CGFloat(10.0);

_viewPortHandler.restrainViewPort(
offsetLeft: max(min, offsetLeft),
offsetTop: max(min, offsetTop),
offsetRight: max(min, offsetRight),
offsetBottom: max(min, offsetBottom));
offsetLeft: max(minOffset, offsetLeft),
offsetTop: max(minOffset, offsetTop),
offsetRight: max(minOffset, offsetRight),
offsetBottom: max(minOffset, offsetBottom));
}

prepareOffsetMatrix();
Expand Down
18 changes: 9 additions & 9 deletions Charts/Classes/Charts/HorizontalBarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public class HorizontalBarChartView: BarChartView
if (_legend.position == .RightOfChart
|| _legend.position == .RightOfChartCenter)
{
offsetRight += _legend.textWidthMax + _legend.xOffset * 2.0;
offsetRight += min(_legend.neededWidth, _viewPortHandler.chartWidth * _legend.maxSizePercent) + _legend.xOffset * 2.0;
}
else if (_legend.position == .LeftOfChart
|| _legend.position == .LeftOfChartCenter)
{
offsetLeft += _legend.textWidthMax + _legend.xOffset * 2.0;
offsetLeft += min(_legend.neededWidth, _viewPortHandler.chartWidth * _legend.maxSizePercent) + _legend.xOffset * 2.0;
}
else if (_legend.position == .BelowChartLeft
|| _legend.position == .BelowChartRight
|| _legend.position == .BelowChartCenter)
{

offsetBottom += _legend.textHeightMax * 3.0;
var yOffset = _legend.textHeightMax * 2.0; // It's possible that we do not need this offset anymore as it is available through the extraOffsets
offsetBottom += min(_legend.neededHeight + yOffset, _viewPortHandler.chartHeight * _legend.maxSizePercent);
}
}

Expand Down Expand Up @@ -95,13 +95,13 @@ public class HorizontalBarChartView: BarChartView
offsetBottom += self.extraBottomOffset;
offsetLeft += self.extraLeftOffset;

var min: CGFloat = 10.0;
var minOffset: CGFloat = 10.0;

_viewPortHandler.restrainViewPort(
offsetLeft: max(min, offsetLeft),
offsetTop: max(min, offsetTop),
offsetRight: max(min, offsetRight),
offsetBottom: max(min, offsetBottom));
offsetLeft: max(minOffset, offsetLeft),
offsetTop: max(minOffset, offsetTop),
offsetRight: max(minOffset, offsetRight),
offsetBottom: max(minOffset, offsetBottom));

prepareOffsetMatrix();
prepareValuePxMatrix();
Expand Down
44 changes: 21 additions & 23 deletions Charts/Classes/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,22 @@ public class PieRadarChartViewBase: ChartViewBase

if (_legend != nil && _legend.enabled)
{
var fullLegendWidth = min(_legend.neededWidth, _viewPortHandler.chartWidth * _legend.maxSizePercent);
fullLegendWidth += _legend.formSize + _legend.formToTextSpace;

if (_legend.position == .RightOfChartCenter)
{
// this is the space between the legend and the chart
var spacing = CGFloat(13.0);
let spacing = CGFloat(13.0);

legendRight = self.fullLegendWidth + spacing;
legendRight = fullLegendWidth + spacing;
}
else if (_legend.position == .RightOfChart)
{

// this is the space between the legend and the chart
var spacing = CGFloat(8.0);
var legendWidth = self.fullLegendWidth + spacing;
let spacing = CGFloat(8.0);

var legendWidth = fullLegendWidth + spacing;
var legendHeight = _legend.neededHeight + _legend.textHeightMax;

var c = self.midPoint;
Expand All @@ -117,12 +120,12 @@ public class PieRadarChartViewBase: ChartViewBase
angle: angleForPoint(x: bottomRight.x, y: bottomRight.y));

var distReference = distanceToCenter(x: reference.x, y: reference.y);
var min = CGFloat(5.0);
var minOffset = CGFloat(5.0);

if (distLegend < distReference)
{
var diff = distReference - distLegend;
legendRight = min + diff;
legendRight = minOffset + diff;
}

if (bottomRight.y >= c.y && self.bounds.height - legendWidth > self.bounds.width)
Expand All @@ -133,16 +136,17 @@ public class PieRadarChartViewBase: ChartViewBase
else if (_legend.position == .LeftOfChartCenter)
{
// this is the space between the legend and the chart
var spacing = CGFloat(13.0);
let spacing = CGFloat(13.0);

legendLeft = self.fullLegendWidth + spacing;
legendLeft = fullLegendWidth + spacing;
}
else if (_legend.position == .LeftOfChart)
{

// this is the space between the legend and the chart
var spacing = CGFloat(8.0);
var legendWidth = self.fullLegendWidth + spacing;
let spacing = CGFloat(8.0);

var legendWidth = fullLegendWidth + spacing;
var legendHeight = _legend.neededHeight + _legend.textHeightMax;

var c = self.midPoint;
Expand Down Expand Up @@ -184,22 +188,22 @@ public class PieRadarChartViewBase: ChartViewBase
legendBottom += self.extraBottomOffset;
legendLeft += self.extraLeftOffset;

var min = CGFloat(10.0);
var minOffset = CGFloat(10.0);

if (self.isKindOfClass(RadarChartView))
{
let x = (self as! RadarChartView).xAxis;

if (x.isEnabled)
{
min = max(10.0, x.labelWidth);
minOffset = max(10.0, x.labelWidth);
}
}

var offsetLeft = max(min, legendLeft);
var offsetTop = max(min, legendTop);
var offsetRight = max(min, legendRight);
var offsetBottom = max(min, max(self.requiredBaseOffset, legendBottom));
var offsetLeft = max(minOffset, legendLeft);
var offsetTop = max(minOffset, legendTop);
var offsetRight = max(minOffset, legendRight);
var offsetBottom = max(minOffset, max(self.requiredBaseOffset, legendBottom));

_viewPortHandler.restrainViewPort(offsetLeft: offsetLeft, offsetTop: offsetTop, offsetRight: offsetRight, offsetBottom: offsetBottom);
}
Expand Down Expand Up @@ -333,12 +337,6 @@ public class PieRadarChartViewBase: ChartViewBase
{
fatalError("requiredBaseOffset cannot be called on PieRadarChartViewBase");
}

/// Returns the required right offset for the chart.
private var fullLegendWidth: CGFloat
{
return _legend.textWidthMax + _legend.formSize + _legend.formToTextSpace;
}

public override var chartXMax: Float
{
Expand Down
18 changes: 13 additions & 5 deletions Charts/Classes/Components/ChartLegend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,18 @@ public class ChartLegend: ChartComponentBase
return CGSize(width: width, height: height);
}
public var neededWidth = CGFloat(0.0);
public var neededHeight = CGFloat(0.0);
public var textWidthMax = CGFloat(0.0);
public var textHeightMax = CGFloat(0.0);
public var neededWidth = CGFloat(0.0)
public var neededHeight = CGFloat(0.0)
public var textWidthMax = CGFloat(0.0)
public var textHeightMax = CGFloat(0.0)
/// The maximum relative size out of the whole chart view.
/// If the legend is to the right/left of the chart, then this affects the width of the legend.
/// If the legend is to the top/bottom of the chart, then this affects the height of the legend.
/// If the legend is the center of the piechart, then this defines the size of the rectangular bounds out of the size of the "hole".
/// default: 1.0 (100%)
public var maxSizePercent: CGFloat = 1.0
public func calculateDimensions(labelFont: UIFont)
{
var maxEntrySize = getMaximumEntrySize(labelFont);
Expand All @@ -205,6 +212,7 @@ public class ChartLegend: ChartComponentBase
}
else
{
/* BelowChartLeft. BelowChartRight, BelowChartCenter, RightOfChartInside, LeftOfChartInside */
neededWidth = fullSize.width;
neededHeight = maxEntrySize.height;
textWidthMax = maxEntrySize.width;
Expand Down
1 change: 1 addition & 0 deletions Charts/Classes/Data/BarChartDataEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Foundation

public class BarChartDataEntry: ChartDataEntry
{
/// the values the stacked barchart holds
private var _values: [Float]!

/// Constructor for stacked bar entries.
Expand Down

0 comments on commit aa1ad23

Please sign in to comment.