Skip to content

Commit

Permalink
Fixes the distance issue between the legend and the horizontal bar ch…
Browse files Browse the repository at this point in the history
…art (Fixes #2138) (#2214)

* Fixes the distance issue between the legend and the horizontal bar chart (Fixes #2138)

Override the function calculateLegendOffsets in HorizontalBarChartView
in order to select the correct axis for distance calculation. In case
of horizontal chart the x-axis is described the the rightAxis (bottom)
or the leftAxis(top).

* Added guard for _legend status
  • Loading branch information
SvenMuc authored and jjatie committed Jan 21, 2018
1 parent 4fbeb9d commit b0022f8
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion Source/Charts/Charts/HorizontalBarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,67 @@ open class HorizontalBarChartView: BarChartView
self.highlighter = HorizontalBarHighlighter(chart: self)
}

internal override func calculateLegendOffsets(offsetLeft: inout CGFloat, offsetTop: inout CGFloat, offsetRight: inout CGFloat, offsetBottom: inout CGFloat)
{
guard
let legend = _legend,
legend.isEnabled,
legend.drawInside
else { return }

// setup offsets for legend
switch legend.orientation
{
case .vertical:
switch legend.horizontalAlignment
{
case .left:
offsetLeft += min(legend.neededWidth, _viewPortHandler.chartWidth * legend.maxSizePercent) + legend.xOffset

case .right:
offsetRight += min(legend.neededWidth, _viewPortHandler.chartWidth * legend.maxSizePercent) + legend.xOffset

case .center:

switch legend.verticalAlignment
{
case .top:
offsetTop += min(legend.neededHeight, _viewPortHandler.chartHeight * legend.maxSizePercent) + legend.yOffset

case .bottom:
offsetBottom += min(legend.neededHeight, _viewPortHandler.chartHeight * legend.maxSizePercent) + legend.yOffset

default:
break
}
}

case .horizontal:
switch legend.verticalAlignment
{
case .top:
offsetTop += min(legend.neededHeight, _viewPortHandler.chartHeight * legend.maxSizePercent) + legend.yOffset

// left axis equals the top x-axis in a horizontal chart
if leftAxis.isEnabled && leftAxis.isDrawLabelsEnabled
{
offsetTop += leftAxis.getRequiredHeightSpace()
}

case .bottom:
offsetBottom += min(legend.neededHeight, _viewPortHandler.chartHeight * legend.maxSizePercent) + legend.yOffset

// right axis equals the bottom x-axis in a horizontal chart
if rightAxis.isEnabled && rightAxis.isDrawLabelsEnabled
{
offsetBottom += rightAxis.getRequiredHeightSpace()
}
default:
break
}
}
}

internal override func calculateOffsets()
{
var offsetLeft: CGFloat = 0.0,
Expand Down Expand Up @@ -81,7 +142,7 @@ open class HorizontalBarChartView: BarChartView
offsetRight += self.extraRightOffset
offsetBottom += self.extraBottomOffset
offsetLeft += self.extraLeftOffset

_viewPortHandler.restrainViewPort(
offsetLeft: max(self.minOffset, offsetLeft),
offsetTop: max(self.minOffset, offsetTop),
Expand Down

0 comments on commit b0022f8

Please sign in to comment.