Skip to content

Commit

Permalink
Add name to the axis based on thierryH91200 PR (ChartsOrg@7dd6d73)
Browse files Browse the repository at this point in the history
  • Loading branch information
kileros committed Oct 22, 2018
1 parent 55028b1 commit 1f6585b
Show file tree
Hide file tree
Showing 8 changed files with 409 additions and 14 deletions.
6 changes: 6 additions & 0 deletions ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class LineChart1ViewController: DemoBaseViewController {
chartView.dragEnabled = true
chartView.setScaleEnabled(true)
chartView.pinchZoomEnabled = true


// x-axis limit line
let llXAxis = ChartLimitLine(limit: 10, label: "Index 10")
Expand All @@ -68,13 +69,18 @@ class LineChart1ViewController: DemoBaseViewController {
ll2.valueFont = .systemFont(ofSize: 10)

let leftAxis = chartView.leftAxis
leftAxis.nameAxisEnabled = true
leftAxis.nameAxis = "teste"
leftAxis.removeAllLimitLines()
leftAxis.addLimitLine(ll1)
leftAxis.addLimitLine(ll2)
leftAxis.axisMaximum = 200
leftAxis.axisMinimum = -50
leftAxis.gridLineDashLengths = [5, 5]
leftAxis.drawLimitLinesBehindDataEnabled = true

chartView.xAxis.nameAxisEnabled = true
chartView.xAxis.nameAxis = "teste 2"

chartView.rightAxis.enabled = false

Expand Down
54 changes: 53 additions & 1 deletion Source/Charts/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -432,30 +432,82 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
if leftAxis.needsOffset
{
offsetLeft += leftAxis.requiredSize().width
if leftAxis.nameAxisEnabled
{
let nameLeftAxisHeight = leftAxis.nameAxis.size(withAttributes: [NSAttributedString.Key.font: leftAxis.nameAxisFont]).height
offsetLeft += nameLeftAxisHeight
leftAxis.nameRectLeft = CGRect(x: offsetLeft - leftAxis.requiredSize().width - nameLeftAxisHeight,
y:viewPortHandler.contentTop,
width: nameLeftAxisHeight,
height:viewPortHandler.contentHeight)
}
}

if rightAxis.needsOffset
{
offsetRight += rightAxis.requiredSize().width
if rightAxis.nameAxisEnabled
{
let nameRightAxisHeight = rightAxis.nameAxis.size(withAttributes: [NSAttributedString.Key.font: rightAxis.nameAxisFont]).height
offsetRight += nameRightAxisHeight
rightAxis.nameRectRight = CGRect(x: viewPortHandler.contentRight + rightAxis.requiredSize().width,
y: viewPortHandler.contentTop,
width: nameRightAxisHeight,
height: viewPortHandler.contentHeight)
}
}

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

var xlabelheight = (xAxis.labelRotationAngle != 0 ? xAxis.labelRotatedHeight : xAxis.labelHeight)
xlabelheight += xAxis.yOffset

var namexAxisHeight = CGFloat(0.0)
var nameAxisRectWidth = CGFloat(0.0)
var nameAxisRectLeft = CGFloat(0.0)
if xAxis.nameAxisEnabled
{
namexAxisHeight = xAxis.nameAxis.size(withAttributes: [NSAttributedString.Key.font: xAxis.nameAxisFont]).height
nameAxisRectWidth = viewPortHandler.contentWidth
nameAxisRectLeft = viewPortHandler.contentLeft
}

// offsets for x-labels
// offsets for x-labels
if xAxis.labelPosition == .bottom
{
offsetBottom += xlabelheight
offsetBottom += xlabelheight + namexAxisHeight
xAxis.nameRectBottom = CGRect(x: nameAxisRectLeft,
y: viewPortHandler.chartHeight - offsetBottom + xlabelheight,
width: nameAxisRectWidth,
height: namexAxisHeight)
}
else if xAxis.labelPosition == .top
{
offsetTop += xlabelheight
offsetTop += xlabelheight + namexAxisHeight
xAxis.nameRectTop = CGRect(x: nameAxisRectLeft,
y: offsetTop - namexAxisHeight - xlabelheight,
width: nameAxisRectWidth,
height: namexAxisHeight)
}
else if xAxis.labelPosition == .bothSided
{
offsetBottom += xlabelheight
offsetTop += xlabelheight
offsetBottom += xlabelheight + namexAxisHeight
xAxis.nameRectBottom = CGRect(x: nameAxisRectLeft,
y: viewPortHandler.chartHeight - offsetBottom + xlabelheight,
width: nameAxisRectWidth,
height: namexAxisHeight)
offsetTop += xlabelheight + namexAxisHeight
xAxis.nameRectTop = CGRect(x: nameAxisRectLeft,
y: offsetTop - namexAxisHeight - xlabelheight,
width: nameAxisRectWidth,
height: namexAxisHeight)
}
}

Expand Down
57 changes: 53 additions & 4 deletions Source/Charts/Charts/HorizontalBarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,84 @@ open class HorizontalBarChartView: BarChartView
offsetTop: &offsetTop,
offsetRight: &offsetRight,
offsetBottom: &offsetBottom)

var nameAxisRectWidth = CGFloat(0.0)
var nameAxisRectLeft = CGFloat(0.0)
if leftAxis.nameAxisEnabled || rightAxis.nameAxisEnabled
{
nameAxisRectWidth = viewPortHandler.contentWidth
nameAxisRectLeft = viewPortHandler.contentLeft
}

// offsets for y-labels
if leftAxis.needsOffset
{
offsetTop += leftAxis.getRequiredHeightSpace()
if leftAxis.nameAxisEnabled
{
let nameLeftAxisSize = leftAxis.nameAxis.size(withAttributes: [NSAttributedString.Key.font: leftAxis.nameAxisFont])
offsetTop += nameLeftAxisSize.height
leftAxis.nameRectTop = CGRect(x: nameAxisRectLeft,
y: offsetTop - nameLeftAxisSize.height - leftAxis.getRequiredHeightSpace(),
width: nameAxisRectWidth,
height: nameLeftAxisSize.height)
}
}

if rightAxis.needsOffset
{
offsetBottom += rightAxis.getRequiredHeightSpace()
if rightAxis.nameAxisEnabled
{
let nameRightAxisSize = rightAxis.nameAxis.size(withAttributes: [NSAttributedString.Key.font: rightAxis.nameAxisFont])
offsetBottom += nameRightAxisSize.height
rightAxis.nameRectBottom = CGRect(x: nameAxisRectLeft,
y: viewPortHandler.chartHeight - offsetBottom + rightAxis.getRequiredHeightSpace() ,
width: nameAxisRectWidth,
height: nameRightAxisSize.height)
}
}

let xlabelwidth = _xAxis.labelRotatedWidth

if _xAxis.isEnabled
{
var namexAxisHeight = CGFloat(0.0)
if xAxis.nameAxisEnabled
{
namexAxisHeight = xAxis.nameAxis.size(withAttributes: [NSAttributedString.Key.font: xAxis.nameAxisFont]).height
}

// offsets for x-labels
if _xAxis.labelPosition == .bottom
{
offsetLeft += xlabelwidth
offsetLeft += xlabelwidth + namexAxisHeight
xAxis.nameRectLeft = CGRect(x: offsetLeft - xlabelwidth - namexAxisHeight,
y:viewPortHandler.contentTop,
width: namexAxisHeight,
height:viewPortHandler.contentHeight)
}
else if _xAxis.labelPosition == .top
{
offsetRight += xlabelwidth
offsetRight += xlabelwidth + namexAxisHeight
xAxis.nameRectRight = CGRect(x: viewPortHandler.contentRight + xlabelwidth,
y: viewPortHandler.contentTop,
width: namexAxisHeight,
height: viewPortHandler.contentHeight)
}
else if _xAxis.labelPosition == .bothSided
{
offsetLeft += xlabelwidth
offsetRight += xlabelwidth
offsetLeft += xlabelwidth + namexAxisHeight
xAxis.nameRectLeft = CGRect(x: offsetLeft - xlabelwidth - namexAxisHeight,
y:viewPortHandler.contentTop,
width: namexAxisHeight,
height:viewPortHandler.contentHeight)

offsetRight += xlabelwidth + namexAxisHeight
xAxis.nameRectRight = CGRect(x: viewPortHandler.contentRight + xlabelwidth,
y: viewPortHandler.contentTop,
width: namexAxisHeight,
height: viewPortHandler.contentHeight)
}
}

Expand Down
11 changes: 10 additions & 1 deletion Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ open class AxisBase: ComponentBase

@objc open var drawGridLinesEnabled = true
@objc open var drawAxisLineEnabled = true


open var nameAxis : String = ""
open var nameAxisFont = NSUIFont.boldSystemFont(ofSize: 12.0)
open var nameAxisTextColor = NSUIColor.black
open var nameAxisEnabled = false
/// flag that indicates of the labels of this axis should be drawn or not
@objc open var drawLabelsEnabled = true

Expand Down Expand Up @@ -212,6 +216,11 @@ open class AxisBase: ComponentBase

/// the total range of values this axis covers
@objc open var axisRange = Double(0)

open var nameRectBottom = CGRect()
open var nameRectTop = CGRect()
open var nameRectLeft = CGRect()
open var nameRectRight = CGRect()

/// The minumum number of labels on the axis
@objc open var axisMinLabels = Int(2) {
Expand Down
88 changes: 84 additions & 4 deletions Source/Charts/Renderers/XAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,64 @@ open class XAxisRenderer: AxisRendererBase

if xAxis.labelPosition == .top
{
drawLabels(context: context, pos: viewPortHandler.contentTop - yOffset, anchor: CGPoint(x: 0.5, y: 1.0))
let pos = viewPortHandler.contentTop - yOffset
drawLabels(context: context, pos: pos, anchor: CGPoint(x: 0.5, y: 1.0))
drawNameXAxis(context: context, nameRect: xAxis.nameRectTop)
}
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
{
drawLabels(context: context, pos: viewPortHandler.contentBottom + yOffset, anchor: CGPoint(x: 0.5, y: 0.0))
let pos = viewPortHandler.contentBottom + yOffset
drawLabels(context: context, pos: pos, anchor: CGPoint(x: 0.5, y: 0.0))
drawNameXAxis(context: context, nameRect: xAxis.nameRectBottom)
}
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
drawLabels(context: context, pos: viewPortHandler.contentTop - yOffset, anchor: CGPoint(x: 0.5, y: 1.0))
drawLabels(context: context, pos: viewPortHandler.contentBottom + yOffset, anchor: CGPoint(x: 0.5, y: 0.0))
// top
var pos = viewPortHandler.contentTop - yOffset
drawLabels(context: context, pos: pos, anchor: CGPoint(x: 0.5, y: 1.0))
drawNameXAxis ( context: context, nameRect: xAxis.nameRectTop)

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

/// draws the x-name
open func drawNameXAxis ( context: CGContext, nameRect: CGRect)
{
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 = [NSAttributedString.Key.font: xAxis.nameAxisFont,
NSAttributedString.Key.foregroundColor: xAxis.nameAxisTextColor,
NSAttributedString.Key.paragraphStyle: paraStyle]

let text = xAxis.nameAxis
text.draw(in: nameRect, withAttributes: labelAttrs)
}

private var _axisLineSegmentsBuffer = [CGPoint](repeating: CGPoint(), count: 2)

Expand Down Expand Up @@ -250,6 +288,48 @@ 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 = [NSAttributedString.Key.font: xAxis.nameAxisFont,
NSAttributedString.Key.foregroundColor: xAxis.nameAxisTextColor,
NSAttributedString.Key.paragraphStyle: paraStyle]
let labelRotationAngleRadians = 0 * CGFloat(.pi / 180.0)

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)
}


@objc open func drawLabel(
context: CGContext,
Expand Down
Loading

0 comments on commit 1f6585b

Please sign in to comment.