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 ChartsOrg#2138) (ChartsOrg#2214)

* Fixes the distance issue between the legend and the horizontal bar chart (Fixes ChartsOrg#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 Konstantin Zyrianov committed Feb 20, 2018
1 parent 458bc71 commit d765c0e
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions 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

0 comments on commit d765c0e

Please sign in to comment.