Skip to content

Commit

Permalink
Support for left-of-chart legends
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Mar 24, 2015
1 parent 2b908e0 commit e132dfc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ public class BarLineChartViewBase: ChartViewBase
{
offsetRight += _legend.textWidthMax + _legend.xOffset * 2.0;
}
if (_legend.position == .LeftOfChart
|| _legend.position == .LeftOfChartCenter)
{
offsetLeft += _legend.textWidthMax + _legend.xOffset * 2.0;
}
else if (_legend.position == .BelowChartLeft
|| _legend.position == .BelowChartRight
|| _legend.position == .BelowChartCenter)
Expand Down
5 changes: 5 additions & 0 deletions Charts/Classes/Charts/HorizontalBarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public class HorizontalBarChartView: BarChartView
{
offsetRight += _legend.textWidthMax + _legend.xOffset * 2.0;
}
else if (_legend.position == .LeftOfChart
|| _legend.position == .LeftOfChartCenter)
{
offsetLeft += _legend.textWidthMax + _legend.xOffset * 2.0;
}
else if (_legend.position == .BelowChartLeft
|| _legend.position == .BelowChartRight
|| _legend.position == .BelowChartCenter)
Expand Down
41 changes: 40 additions & 1 deletion Charts/Classes/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class PieRadarChartViewBase: ChartViewBase

internal override func calculateOffsets()
{
var legendLeft = CGFloat(0.0);
var legendRight = CGFloat(0.0);
var legendBottom = CGFloat(0.0);
var legendTop = CGFloat(0.0);
Expand Down Expand Up @@ -113,13 +114,51 @@ public class PieRadarChartViewBase: ChartViewBase
legendRight = legendWidth;
}
}
else if (_legend.position == .LeftOfChartCenter)
{
// this is the space between the legend and the chart
var spacing = CGFloat(13.0);

legendLeft = self.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;
var legendHeight = _legend.neededHeight + _legend.textHeightMax;

var c = self.center;

var bottomLeft = CGPoint(x: legendWidth - 15.0, y: legendHeight + 15);
var distLegend = distanceToCenter(x: bottomLeft.x, y: bottomLeft.y);

var reference = getPosition(center: c, dist: self.radius,
angle: angleForPoint(x: bottomLeft.x, y: bottomLeft.y));

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

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

if (bottomLeft.y >= c.y && self.bounds.height - legendWidth > self.bounds.width)
{
legendLeft = legendWidth;
}
}
else if (_legend.position == .BelowChartLeft
|| _legend.position == .BelowChartRight
|| _legend.position == .BelowChartCenter)
{
legendBottom = self.requiredBottomOffset;
}


legendLeft += self.requiredBaseOffset;
legendRight += self.requiredBaseOffset;
legendTop += self.requiredBaseOffset;
}
Expand Down
5 changes: 5 additions & 0 deletions Charts/Classes/Components/ChartLegend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class ChartLegend: ChartComponentBase
case RightOfChart
case RightOfChartCenter
case RightOfChartInside
case LeftOfChart
case LeftOfChartCenter
case LeftOfChartInside
case BelowChartLeft
case BelowChartRight
case BelowChartCenter
Expand Down Expand Up @@ -222,6 +225,8 @@ public class ChartLegend: ChartComponentBase

if (position == .RightOfChart
|| position == .RightOfChartCenter
|| position == .LeftOfChart
|| position == .LeftOfChartCenter
|| position == .PiechartCenter)
{
neededWidth = maxEntrySize.width;
Expand Down
35 changes: 28 additions & 7 deletions Charts/Classes/Renderers/ChartLegendRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,25 +354,46 @@ public class ChartLegendRenderer: ChartRendererBase
break;
case .RightOfChart: fallthrough
case .RightOfChartCenter: fallthrough
case .RightOfChartInside:
case .RightOfChartInside: fallthrough
case .LeftOfChart: fallthrough
case .LeftOfChartCenter: fallthrough
case .LeftOfChartInside:

var isRightAligned = legend.position == .RightOfChart ||
legend.position == .RightOfChartCenter ||
legend.position == .RightOfChartInside;

var posX: CGFloat = 0.0, posY: CGFloat = 0.0;

posX = viewPortHandler.chartWidth - xoffset;
if (direction == .LeftToRight)
if (isRightAligned)
{
posX = viewPortHandler.chartWidth - xoffset;
if (direction == .LeftToRight)
{
posX -= legend.textWidthMax;
}
}
else
{
posX -= legend.textWidthMax;
posX = xoffset;
if (direction == .RightToLeft)
{
posX += legend.textWidthMax;
}
}

if (legend.position == .RightOfChart)
if (legend.position == .RightOfChart ||
legend.position == .LeftOfChart)
{
posY = viewPortHandler.contentTop + yoffset
}
else if (legend.position == .RightOfChartCenter)
else if (legend.position == .RightOfChartCenter ||
legend.position == .LeftOfChartCenter)
{
posY = viewPortHandler.chartHeight / 2.0 - legend.neededHeight / 2.0;
}
else /*if (legend.position == .RightOfChartInside)*/
else /*if (legend.position == .RightOfChartInside ||
legend.position == .LeftOfChartInside)*/
{
posY = viewPortHandler.contentTop + yoffset;
}
Expand Down

0 comments on commit e132dfc

Please sign in to comment.