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 support for a legend above the chart #393

Merged
merged 1 commit into from
Oct 1, 2015
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
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