Skip to content

Commit

Permalink
Named axis support
Browse files Browse the repository at this point in the history
  • Loading branch information
thierryH91200 committed Apr 24, 2017
1 parent aca0491 commit 7dd6d73
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 4 deletions.
35 changes: 33 additions & 2 deletions Source/Charts/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,38 +426,68 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
if leftAxis.needsOffset
{
offsetLeft += leftAxis.requiredSize().width
if leftAxis.nameAxisEnabled
{
let size = leftAxis.nameAxis.size(attributes: [NSFontAttributeName: leftAxis.nameAxisFont])
offsetLeft += size.height
}
}

if rightAxis.needsOffset
{
offsetRight += rightAxis.requiredSize().width
if rightAxis.nameAxisEnabled
{
let size = rightAxis.nameAxis.size(attributes: [NSFontAttributeName: rightAxis.nameAxisFont])
offsetRight += size.height + 5

}
}

if xAxis.isEnabled && xAxis.isDrawLabelsEnabled
{
let xlabelheight = xAxis.labelRotatedHeight + xAxis.yOffset

var size = NSSize()
if xAxis.nameAxisEnabled
{
size = xAxis.nameAxis.size(attributes: [NSFontAttributeName: xAxis.nameAxisFont])
}

// offsets for x-labels
if xAxis.labelPosition == .bottom
{
offsetBottom += xlabelheight
if xAxis.nameAxisEnabled
{
offsetBottom += size.height
}
}
else if xAxis.labelPosition == .top
{
offsetTop += xlabelheight
if xAxis.nameAxisEnabled
{
offsetTop += size.height
}
}
else if xAxis.labelPosition == .bothSided
{
offsetBottom += xlabelheight
offsetTop += xlabelheight
if xAxis.nameAxisEnabled
{
offsetTop += size.height
offsetBottom += size.height
}
}
}

offsetTop += self.extraTopOffset
offsetRight += self.extraRightOffset
offsetBottom += self.extraBottomOffset
offsetLeft += self.extraLeftOffset

_viewPortHandler.restrainViewPort(
offsetLeft: max(self.minOffset, offsetLeft),
offsetTop: max(self.minOffset, offsetTop),
Expand All @@ -469,6 +499,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
prepareValuePxMatrix()
}


/// draws the grid background
internal func drawGridBackground(context: CGContext)
{
Expand Down
5 changes: 5 additions & 0 deletions Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ open class AxisBase: ComponentBase
open var drawGridLinesEnabled = true
open var drawAxisLineEnabled = true

open var nameAxis : String = ""
open var nameAxisFont = NSUIFont.systemFont(ofSize: 14.0)
open var nameAxisTextColor = NSUIColor.blue
open var nameAxisEnabled = false

/// flag that indicates of the labels of this axis should be drawn or not
open var drawLabelsEnabled = true

Expand Down
64 changes: 64 additions & 0 deletions Source/Charts/Renderers/XAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,50 @@ open class XAxisRenderer: AxisRendererBase
}

let yOffset = xAxis.yOffset
let width = (viewPortHandler.chartWidth / 2)
let positions = CGPoint(x: width, y:0.0)

let nameAxisSize = xAxis.nameAxis.size(attributes: [NSFontAttributeName: xAxis.nameAxisFont])

if xAxis.labelPosition == .top
{
var pos = viewPortHandler.contentTop - yOffset
drawLabels(context: context, pos: viewPortHandler.contentTop - yOffset, anchor: CGPoint(x: 0.5, y: 1.0))

pos = pos + (xAxis.labelRotationAngle != 0 ? -xAxis.labelRotatedHeight : -xAxis.labelHeight)
drawNameXAxis ( context: context, fixedPosition: pos, positions: positions, offset: 0.0)
}
else if xAxis.labelPosition == .topInside
{
drawLabels(context: context, pos: viewPortHandler.contentTop + yOffset + xAxis.labelRotatedHeight, anchor: CGPoint(x: 0.5, y: 1.0))
}
else if xAxis.labelPosition == .bottom
{
var pos = viewPortHandler.contentBottom + yOffset
drawLabels(context: context, pos: viewPortHandler.contentBottom + yOffset, anchor: CGPoint(x: 0.5, y: 0.0))

pos = pos + nameAxisSize.height + (xAxis.labelRotationAngle != 0 ? xAxis.labelRotatedHeight : xAxis.labelHeight)
drawNameXAxis ( context: context, fixedPosition: pos, positions: positions, offset: 0.0)
}
else if xAxis.labelPosition == .bottomInside
{
drawLabels(context: context, pos: viewPortHandler.contentBottom - yOffset - xAxis.labelRotatedHeight, anchor: CGPoint(x: 0.5, y: 0.0))
}
else
{ // BOTH SIDED
// top
var pos = viewPortHandler.contentTop - yOffset
drawLabels(context: context, pos: viewPortHandler.contentTop - yOffset, anchor: CGPoint(x: 0.5, y: 1.0))

pos = pos + (xAxis.labelRotationAngle != 0 ? -xAxis.labelRotatedHeight : -xAxis.labelHeight)
drawNameXAxis ( context: context, fixedPosition: pos, positions: positions, offset: 0.0)

// bottom
pos = viewPortHandler.contentBottom + yOffset
drawLabels(context: context, pos: viewPortHandler.contentBottom + yOffset, anchor: CGPoint(x: 0.5, y: 0.0))

pos = pos + nameAxisSize.height + (xAxis.labelRotationAngle != 0 ? xAxis.labelRotatedHeight : xAxis.labelHeight)
drawNameXAxis ( context: context, fixedPosition: pos, positions: positions, offset: 0.0)
}
}

Expand Down Expand Up @@ -262,6 +285,47 @@ open class XAxisRenderer: AxisRendererBase
}
}

/// draws the x-name
open func drawNameXAxis (
context: CGContext,
fixedPosition: CGFloat,
positions: CGPoint,
offset: CGFloat)
{
guard
let xAxis = self.axis as? XAxis
else { return }

if xAxis.nameAxisEnabled == false
{
return
}

#if os(OSX)
let paraStyle = NSParagraphStyle.default().mutableCopy() as! NSMutableParagraphStyle
#else
let paraStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
#endif

paraStyle.alignment = .center
let labelAttrs = [NSFontAttributeName: xAxis.nameAxisFont,
NSForegroundColorAttributeName: xAxis.nameAxisTextColor,
NSParagraphStyleAttributeName: paraStyle] as [String : NSObject]
let labelRotationAngleRadians = 0 * ChartUtils.Math.FDEG2RAD

let text = xAxis.nameAxis
let labelMaxSize = CGSize()

ChartUtils.drawMultilineText(
context: context,
text: text,
point: CGPoint(x: positions.x, y: fixedPosition),
attributes: labelAttrs,
constrainedToSize: labelMaxSize,
anchor: CGPoint(x: 0.5, y: 1.0),
angleRadians: labelRotationAngleRadians)
}

open func drawLabel(
context: CGContext,
formattedLabel: String,
Expand Down
59 changes: 57 additions & 2 deletions Source/Charts/Renderers/YAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ open class YAxisRenderer: AxisRendererBase
let labelPosition = yAxis.labelPosition

var xPos = CGFloat(0.0)
var xPosName = CGFloat(0.0)

var textAlign: NSTextAlignment

Expand All @@ -53,20 +54,25 @@ open class YAxisRenderer: AxisRendererBase
{
textAlign = .right
xPos = viewPortHandler.offsetLeft - xoffset

let label = yAxis.getLongestLabel()
let size = label.size(attributes: [NSFontAttributeName: yAxis.nameAxisFont])
xPosName = size.height
}
else
{
textAlign = .left
xPos = viewPortHandler.offsetLeft + xoffset
}

}
else
{
if labelPosition == .outsideChart
{
textAlign = .left
xPos = viewPortHandler.contentRight + xoffset

xPosName = viewPortHandler.chartWidth - 5
}
else
{
Expand All @@ -81,8 +87,22 @@ open class YAxisRenderer: AxisRendererBase
positions: transformedPositions(),
offset: yoffset - yAxis.labelFont.lineHeight,
textAlign: textAlign)


if yAxis.nameAxisEnabled
{
let high = (viewPortHandler.chartHeight / 2)
var positions = [CGPoint]()
positions.append(CGPoint(x: 0.0, y: high))

drawNameYAxis(
context: context,
fixedPosition: xPosName,
positions: positions[0],
offset: 0.0)
}
}

open override func renderAxisLine(context: CGContext)
{
guard
Expand Down Expand Up @@ -126,6 +146,41 @@ open class YAxisRenderer: AxisRendererBase
context.restoreGState()
}

/// draws the name Yaxis
internal func drawNameYAxis(
context: CGContext,
fixedPosition: CGFloat,
positions: CGPoint,
offset: CGFloat)
{
guard
let yAxis = self.axis as? YAxis
else { return }

#if os(OSX)
let paraStyle = NSParagraphStyle.default().mutableCopy() as! NSMutableParagraphStyle
#else
let paraStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
#endif

paraStyle.alignment = .center

let labelAttrs = [NSFontAttributeName: yAxis.nameAxisFont,
NSForegroundColorAttributeName: yAxis.nameAxisTextColor,
NSParagraphStyleAttributeName: paraStyle] as [String : NSObject]

let labelRotationAngleRadians = -90.0 * ChartUtils.Math.FDEG2RAD
let text = yAxis.nameAxis

ChartUtils.drawText(
context: context,
text: text,
point: CGPoint(x: fixedPosition, y: positions.y + offset),
attributes: labelAttrs,
anchor: CGPoint(x: 1.0, y: 0.5),
angleRadians: labelRotationAngleRadians)
}

/// draws the y-labels on the specified x-position
internal func drawYLabels(
context: CGContext,
Expand Down

0 comments on commit 7dd6d73

Please sign in to comment.