Skip to content

Commit

Permalink
Add support for a legend above the chart
Browse files Browse the repository at this point in the history
  • Loading branch information
icecrystal23 committed Sep 22, 2015
1 parent 7b85077 commit c332010
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
let yOffset = _legend.textHeightMax; // 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)
}
else if (_legend.position == .AboveChartLeft
|| _legend.position == .AboveChartRight
|| _legend.position == .AboveChartCenter)
{
let yOffset = _legend.textHeightMax; // It's possible that we do not need this offset anymore as it is available through the extraOffsets
offsetTop += min(_legend.neededHeight + yOffset, _viewPortHandler.chartHeight * _legend.maxSizePercent)
}
}

// offsets for y-labels
Expand Down
7 changes: 7 additions & 0 deletions Charts/Classes/Charts/HorizontalBarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public class HorizontalBarChartView: BarChartView
let 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)
}
else if (_legend.position == .AboveChartLeft
|| _legend.position == .AboveChartRight
|| _legend.position == .AboveChartCenter)
{
let yOffset = _legend.textHeightMax * 2.0; // It's possible that we do not need this offset anymore as it is available through the extraOffsets
offsetTop += min(_legend.neededHeight + yOffset, _viewPortHandler.chartHeight * _legend.maxSizePercent)
}
}

// offsets for y-labels
Expand Down
2 changes: 1 addition & 1 deletion Charts/Classes/Charts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public class PieChartView: PieRadarChartViewBase
}
}

internal override var requiredBottomOffset: CGFloat
internal override var requiredLegendOffset: CGFloat
{
return _legend.font.pointSize * 2.0
}
Expand Down
17 changes: 12 additions & 5 deletions Charts/Classes/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,17 @@ public class PieRadarChartViewBase: ChartViewBase
|| _legend.position == .BelowChartRight
|| _legend.position == .BelowChartCenter)
{
let yOffset = self.requiredBottomOffset; // It's possible that we do not need this offset anymore as it is available through the extraOffsets
let yOffset = self.requiredLegendOffset; // It's possible that we do not need this offset anymore as it is available through the extraOffsets
legendBottom = min(_legend.neededHeight + yOffset, _viewPortHandler.chartHeight * _legend.maxSizePercent)
}

else if (_legend.position == .AboveChartLeft
|| _legend.position == .AboveChartRight
|| _legend.position == .AboveChartCenter)
{
let yOffset = self.requiredLegendOffset; // It's possible that we do not need this offset anymore as it is available through the extraOffsets
legendTop = min(_legend.neededHeight + yOffset, _viewPortHandler.chartHeight * _legend.maxSizePercent)
}

legendLeft += self.requiredBaseOffset
legendRight += self.requiredBaseOffset
legendTop += self.requiredBaseOffset
Expand Down Expand Up @@ -326,10 +333,10 @@ public class PieRadarChartViewBase: ChartViewBase
fatalError("radius cannot be called on PieRadarChartViewBase")
}

/// - returns: the required bottom offset for the chart.
internal var requiredBottomOffset: CGFloat
/// - returns: the required offset for the chart legend.
internal var requiredLegendOffset: CGFloat
{
fatalError("requiredBottomOffset cannot be called on PieRadarChartViewBase")
fatalError("requiredLegendOffset cannot be called on PieRadarChartViewBase")
}

/// - returns: the base offset needed for the chart without calculating the
Expand Down
2 changes: 1 addition & 1 deletion Charts/Classes/Charts/RadarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public class RadarChartView: PieRadarChartViewBase
}
}

internal override var requiredBottomOffset: CGFloat
internal override var requiredLegendOffset: CGFloat
{
return _legend.font.pointSize * 4.0
}
Expand Down
8 changes: 7 additions & 1 deletion Charts/Classes/Components/ChartLegend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class ChartLegend: ChartComponentBase
case BelowChartRight
case BelowChartCenter
case PiechartCenter
case AboveChartLeft
case AboveChartRight
case AboveChartCenter
}

@objc
Expand Down Expand Up @@ -235,7 +238,10 @@ public class ChartLegend: ChartComponentBase
}
else if (position == .BelowChartLeft
|| position == .BelowChartRight
|| position == .BelowChartCenter)
|| position == .BelowChartCenter
|| position == .AboveChartLeft
|| position == .AboveChartRight
|| position == .AboveChartCenter)
{
var labels = self.labels
var colors = self.colors
Expand Down
15 changes: 12 additions & 3 deletions Charts/Classes/Renderers/ChartLegendRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,16 @@ public class ChartLegendRenderer: ChartRendererBase
{
case .BelowChartLeft: fallthrough
case .BelowChartRight: fallthrough
case .BelowChartCenter:
case .BelowChartCenter: fallthrough
case .AboveChartLeft: fallthrough
case .AboveChartRight: fallthrough
case .AboveChartCenter:

let contentWidth: CGFloat = viewPortHandler.contentWidth

var originPosX: CGFloat

if (legendPosition == .BelowChartLeft)
if (legendPosition == .BelowChartLeft || legendPosition == .AboveChartLeft)
{
originPosX = viewPortHandler.contentLeft + xoffset

Expand All @@ -154,7 +157,7 @@ public class ChartLegendRenderer: ChartRendererBase
originPosX += _legend.neededWidth
}
}
else if (legendPosition == .BelowChartRight)
else if (legendPosition == .BelowChartRight || legendPosition == .AboveChartRight)
{
originPosX = viewPortHandler.contentRight - xoffset

Expand All @@ -174,6 +177,12 @@ public class ChartLegendRenderer: ChartRendererBase

var posX: CGFloat = originPosX
var posY: CGFloat = viewPortHandler.chartHeight - yoffset - _legend.neededHeight
if (legendPosition == .AboveChartLeft
|| legendPosition == .AboveChartRight
|| legendPosition == .AboveChartCenter)
{
posY = 0;
}

var lineIndex: Int = 0

Expand Down

0 comments on commit c332010

Please sign in to comment.