Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
517 changes: 375 additions & 142 deletions Source/Charts/Charts/BarLineChartViewBase.swift

Large diffs are not rendered by default.

153 changes: 133 additions & 20 deletions Source/Charts/Charts/HorizontalBarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ open class HorizontalBarChartView: BarChartView
super.initialize()

_leftAxisTransformer = TransformerHorizontalBarChart(viewPortHandler: _viewPortHandler)
_leftAxisTransformer1 = TransformerHorizontalBarChart(viewPortHandler: _viewPortHandler)

_rightAxisTransformer = TransformerHorizontalBarChart(viewPortHandler: _viewPortHandler)
_rightAxisTransformer1 = TransformerHorizontalBarChart(viewPortHandler: _viewPortHandler)

renderer = HorizontalBarChartRenderer(dataProvider: self, animator: _animator, viewPortHandler: _viewPortHandler)
_leftYAxisRenderer = YAxisRendererHorizontalBarChart(viewPortHandler: _viewPortHandler, yAxis: _leftAxis, transformer: _leftAxisTransformer)

_leftYAxisRenderer = YAxisRendererHorizontalBarChart( viewPortHandler: _viewPortHandler, yAxis: _leftAxis, transformer: _leftAxisTransformer)
_leftYAxisRenderer1 = YAxisRendererHorizontalBarChart( viewPortHandler: _viewPortHandler, yAxis: _leftAxis1, transformer: _leftAxisTransformer1)

_rightYAxisRenderer = YAxisRendererHorizontalBarChart(viewPortHandler: _viewPortHandler, yAxis: _rightAxis, transformer: _rightAxisTransformer)
_rightYAxisRenderer1 = YAxisRendererHorizontalBarChart(viewPortHandler: _viewPortHandler, yAxis: _rightAxis1, transformer: _rightAxisTransformer1)

_xAxisRenderer = XAxisRendererHorizontalBarChart(viewPortHandler: _viewPortHandler, xAxis: _xAxis, transformer: _leftAxisTransformer, chart: self)

self.highlighter = HorizontalBarHighlighter(chart: self)
Expand All @@ -41,47 +49,149 @@ open class HorizontalBarChartView: BarChartView
offsetTop: CGFloat = 0.0,
offsetBottom: CGFloat = 0.0

calculateLegendOffsets(offsetLeft: &offsetLeft,
offsetTop: &offsetTop,
offsetRight: &offsetRight,
offsetBottom: &offsetBottom)
offsetTop += self.extraTopOffset
offsetRight += self.extraRightOffset
offsetBottom += self.extraBottomOffset
offsetLeft += self.extraLeftOffset

var offsetLeftLegend = CGFloat(0.0)
var offsetRightLegend = CGFloat(0.0)
var offsetTopLegend = CGFloat(0.0)
var offsetBottomLegend = CGFloat(0.0)

calculateLegendOffsets(offsetLeft: &offsetLeftLegend,
offsetTop: &offsetTopLegend,
offsetRight: &offsetRightLegend,
offsetBottom: &offsetBottomLegend)

let axisRectWidth = viewPortHandler.contentWidth
let axisRectLeft = viewPortHandler.contentLeft

let lineWidth : CGFloat = 1.0
let stick : CGFloat = 5.0

// offsets for y-labels
if _leftAxis.needsOffset
// Space from top to bottom
// offsetTop = offsetTopLegend + leftAxis1.axisRectTop.height + leftAxis.axisRectTop.height + leftAxis.yOffset

// rect axis
// 1 pixel : line height
// 5 pixels : stick
// n pixels : label height
// n pixels : name height
offsetTop = offsetTopLegend
var offsetTopTmp = offsetTop
if leftAxis1.needsOffsetSecondary
{
offsetTop += _leftAxis.getRequiredHeightSpace()
offsetTop += lineWidth + stick + leftAxis1.getRequiredHeightSpaceSecondary()
if leftAxis1.nameAxisEnabled
{
let nameAxisHeight = leftAxis.nameAxis.size(attributes: [NSFontAttributeName: leftAxis.nameAxisFont]).height
offsetTop += nameAxisHeight
}
let height = offsetTop - offsetTopTmp
leftAxis1.axisRectTop = CGRect(x: axisRectLeft,
y: offsetTopTmp,
width: axisRectWidth,
height: height)
}

if _rightAxis.needsOffset
offsetTopTmp = offsetTop
if leftAxis.needsOffset
{
offsetBottom += _rightAxis.getRequiredHeightSpace()
offsetTop += lineWidth + stick + leftAxis.getRequiredHeightSpace()
if leftAxis.nameAxisEnabled
{
let nameAxisHeight = leftAxis.nameAxis.size(attributes: [NSFontAttributeName: leftAxis.nameAxisFont]).height
offsetTop += nameAxisHeight
}
let height = offsetTop - offsetTopTmp
leftAxis.axisRectTop = CGRect(x: axisRectLeft,
y: offsetTopTmp,
width: axisRectWidth,
height: height)
}
offsetTop += leftAxis.yOffset

let xlabelwidth = _xAxis.labelRotatedWidth
// Space from bottom to top
offsetBottom = offsetBottomLegend
var offsetBottomTmp = offsetBottom
if rightAxis1.needsOffsetSecondary
{
offsetBottom += rightAxis1.getRequiredHeightSpaceSecondary()
if rightAxis1.nameAxisEnabled
{
let nameAxisHeight = rightAxis1.nameAxis.size(attributes: [NSFontAttributeName: rightAxis1.nameAxisFont]).height
offsetBottom += nameAxisHeight
}
let height = offsetBottom - offsetBottomTmp
rightAxis1.axisRectBottom = CGRect(x: axisRectLeft,
y: viewPortHandler.chartHeight - offsetBottom,
width: axisRectWidth,
height: height)
}

offsetBottomTmp = offsetBottom
if rightAxis.needsOffset
{
offsetBottom += rightAxis.getRequiredHeightSpace()
if rightAxis.nameAxisEnabled
{
let nameAxisHeight = rightAxis.nameAxis.size(attributes: [NSFontAttributeName: rightAxis.nameAxisFont]).height
offsetBottom += nameAxisHeight
}
let height = offsetBottom - offsetBottomTmp
rightAxis.axisRectBottom = CGRect(x: axisRectLeft,
y: viewPortHandler.chartHeight - offsetBottom,
width: axisRectWidth,
height: height)
}
offsetBottom += leftAxis.yOffset

let xlabelwidth = _xAxis.labelRotatedWidth
if _xAxis.isEnabled
{
var namexAxisHeight = CGFloat(0.0)
if xAxis.nameAxisEnabled
{
namexAxisHeight = xAxis.nameAxis.size(attributes: [NSFontAttributeName: xAxis.nameAxisFont]).height
}

// offsets for x-labels
offsetLeft = offsetLeftLegend
offsetRight = offsetRightLegend
if _xAxis.labelPosition == .bottom
{
offsetLeft += xlabelwidth
offsetLeft += xlabelwidth + namexAxisHeight
xAxis.axisRectLeft = CGRect(x: offsetLeft - xlabelwidth - namexAxisHeight,
y:viewPortHandler.contentTop,
width: namexAxisHeight,
height:viewPortHandler.contentHeight)
}
else if _xAxis.labelPosition == .top
{
offsetRight += xlabelwidth
offsetRight += xlabelwidth + namexAxisHeight
xAxis.axisRectRight = 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.axisRectLeft = CGRect(x: offsetLeft - xlabelwidth - namexAxisHeight,
y:viewPortHandler.contentTop,
width: namexAxisHeight,
height:viewPortHandler.contentHeight)

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

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 @@ -95,7 +205,10 @@ open class HorizontalBarChartView: BarChartView
internal override func prepareValuePxMatrix()
{
_rightAxisTransformer.prepareMatrixValuePx(chartXMin: _rightAxis._axisMinimum, deltaX: CGFloat(_rightAxis.axisRange), deltaY: CGFloat(_xAxis.axisRange), chartYMin: _xAxis._axisMinimum)
_rightAxisTransformer1.prepareMatrixValuePx(chartXMin: _rightAxis1._axisMinimum, deltaX: CGFloat(_rightAxis1.axisRange), deltaY: CGFloat(_xAxis.axisRange), chartYMin: _xAxis._axisMinimum)

_leftAxisTransformer.prepareMatrixValuePx(chartXMin: _leftAxis._axisMinimum, deltaX: CGFloat(_leftAxis.axisRange), deltaY: CGFloat(_xAxis.axisRange), chartYMin: _xAxis._axisMinimum)
_leftAxisTransformer1.prepareMatrixValuePx(chartXMin: _leftAxis1._axisMinimum, deltaX: CGFloat(_leftAxis1.axisRange), deltaY: CGFloat(_xAxis.axisRange), chartYMin: _xAxis._axisMinimum)
}

open override func getMarkerPosition(highlight: Highlight) -> CGPoint
Expand Down Expand Up @@ -135,7 +248,7 @@ open class HorizontalBarChartView: BarChartView

return vals
}

open override func getHighlightByTouchPoint(_ pt: CGPoint) -> Highlight?
{
if _data === nil
Expand Down
42 changes: 27 additions & 15 deletions Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,43 @@ open class AxisBase: ComponentBase
open var drawGridLinesEnabled = true
open var drawAxisLineEnabled = true

open var nameAxis = ""
open var nameAxisFont = NSUIFont.systemFont(ofSize: 12.0)
open var nameAxisEnabled = false

open var axisRectBottom = CGRect()
open var axisRectTop = CGRect()
open var axisRectLeft = CGRect()
open var axisRectRight = CGRect()

fileprivate var _nameAxisTextColor = Int(6)

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

fileprivate var _centerAxisLabelsEnabled = false

/// Centers the axis labels instead of drawing them at their original position.
/// This is useful especially for grouped BarChart.
open var centerAxisLabelsEnabled: Bool
{
{
get { return _centerAxisLabelsEnabled && entryCount > 0 }
set { _centerAxisLabelsEnabled = newValue }
}

open var isCenterAxisLabelsEnabled: Bool
{
{
get { return centerAxisLabelsEnabled }
}

/// array of limitlines that can be set for the axis
fileprivate var _limitLines = [ChartLimitLine]()

/// Are the LimitLines drawn behind the data or in front of the data?
///
///
/// **default**: false
open var drawLimitLinesBehindDataEnabled = false

/// the flag can be used to turn off the antialias for grid lines
open var gridAntialiasEnabled = true

Expand Down Expand Up @@ -100,7 +111,7 @@ open class AxisBase: ComponentBase
///
/// **default**: 1.0
open var granularity: Double
{
{
get
{
return _granularity
Expand All @@ -116,7 +127,7 @@ open class AxisBase: ComponentBase

/// The minimum interval between axis values.
open var isGranularityEnabled: Bool
{
{
get
{
return granularityEnabled
Expand Down Expand Up @@ -158,7 +169,7 @@ open class AxisBase: ComponentBase
/// If no formatter is set, the chart will automatically determine a reasonable formatting (concerning decimals) for all the values that are drawn inside the chart.
/// Use `nil` to use the formatter calculated by the chart.
open var valueFormatter: IAxisValueFormatter?
{
{
get
{
if _axisValueFormatter == nil ||
Expand All @@ -184,7 +195,7 @@ open class AxisBase: ComponentBase
open var isDrawLabelsEnabled: Bool { return drawLabelsEnabled }

/// Are the LimitLines drawn behind the data or in front of the data?
///
///
/// **default**: false
open var isDrawLimitLinesBehindDataEnabled: Bool { return drawLimitLinesBehindDataEnabled }

Expand Down Expand Up @@ -213,13 +224,14 @@ open class AxisBase: ComponentBase
/// the total range of values this axis covers
open var axisRange = Double(0)


/// the number of label entries the axis should have
/// max = 25,
/// min = 2,
/// default = 6,
/// be aware that this number is not fixed and can only be approximated
open var labelCount: Int
{
{
get
{
return _labelCount
Expand Down Expand Up @@ -302,15 +314,15 @@ open class AxisBase: ComponentBase
/// This property is deprecated - Use `axisMinimum` instead.
@available(*, deprecated: 1.0, message: "Use axisMinimum instead.")
open var axisMinValue: Double
{
{
get { return axisMinimum }
set { axisMinimum = newValue }
}

/// This property is deprecated - Use `axisMaximum` instead.
@available(*, deprecated: 1.0, message: "Use axisMaximum instead.")
open var axisMaxValue: Double
{
{
get { return axisMaximum }
set { axisMaximum = newValue }
}
Expand All @@ -319,7 +331,7 @@ open class AxisBase: ComponentBase
/// If set, this value will not be calculated automatically depending on the provided data.
/// Use `resetCustomAxisMin()` to undo this.
open var axisMinimum: Double
{
{
get
{
return _axisMinimum
Expand All @@ -336,7 +348,7 @@ open class AxisBase: ComponentBase
/// If set, this value will not be calculated automatically depending on the provided data.
/// Use `resetCustomAxisMax()` to undo this.
open var axisMaximum: Double
{
{
get
{
return _axisMaximum
Expand Down
Loading