Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,14 @@ Some tips for debugging an hidden chart:
* `minX` – minimum x-value.
* `minY` – minimum y-value.
* `topInset` – height of the area at the top of the chart, acting a padding to make place for the top y-axis label.
* `leftInset` – width of the area at the left of the chart, acting a padding to make place for the left x-axis label.
* `xLabelsFormatter` – formats the labels on the x-axis.
* `xLabelsOrientation` – sets the x-axis labels orientation to vertical or horizontal.
* `xLabelsTextAlignment` – text-alignment for the x-labels.
* `xLabelsSkipLast` (default `true`) - Skip the last x-label. Setting this to `false` will make the label overflow the frame width, so use carefully!
* `yLabelsFormatter` – formats the labels on the y-axis.
* `yLabelsOnRightSide` – place the y-labels on the right side.
* `showHighlightLine` (default `true`) – boolen to show or hide the highlight line on touch of the graph.

#### Methods

Expand Down
65 changes: 42 additions & 23 deletions Source/Chart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ open class Chart: UIControl {
Height of the area at the top of the chart, acting a padding to make place for the top y-axis label.
*/
open var topInset: CGFloat = 20


/**
Height of the area at the left of the chart, acting a padding to make place for the left x-axis label.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm is it height or width ? :)

*/
open var leftInset: CGFloat = 0

/**
Width of the chart's lines.
*/
Expand Down Expand Up @@ -186,8 +191,13 @@ open class Chart: UIControl {
open var maxY: Float?

/**
Color for the highlight line.
*/
Should show highlight line when touched.
*/
open var showHighlightLine: Bool = true

/**
Color for the highlight line
*/
open var highlightLineColor = UIColor.gray

/**
Expand Down Expand Up @@ -302,8 +312,8 @@ open class Chart: UIControl {
fileprivate func drawChart() {

drawingHeight = bounds.height - bottomInset - topInset
drawingWidth = bounds.width

drawingWidth = bounds.width - leftInset
let minMax = getMinMax()
min = minMax.min
max = minMax.max
Expand Down Expand Up @@ -413,8 +423,9 @@ open class Chart: UIControl {
} else {
factor = width / (max.x - min.x)
}

let scaled = values.map { factor * ($0 - self.min.x) }

let reversedValues = Array(values.reversed())
let scaled = reversedValues.map {Float(self.leftInset) + width - factor * ($0 - self.min.x) }
return scaled
}

Expand Down Expand Up @@ -521,30 +532,32 @@ open class Chart: UIControl {

// horizontal axis at the bottom
context.move(to: CGPoint(x: CGFloat(0), y: drawingHeight + topInset))
context.addLine(to: CGPoint(x: CGFloat(drawingWidth), y: drawingHeight + topInset))
context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: drawingHeight + topInset))
context.strokePath()

// horizontal axis at the top
context.move(to: CGPoint(x: CGFloat(0), y: CGFloat(0)))
context.addLine(to: CGPoint(x: CGFloat(drawingWidth), y: CGFloat(0)))
context.move(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: CGFloat(0)))
context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: CGFloat(0)))
context.strokePath()

// horizontal axis when y = 0
if min.y < 0 && max.y > 0 {
let y = CGFloat(getZeroValueOnYAxis(zeroLevel: 0))
context.move(to: CGPoint(x: CGFloat(0), y: y))
context.addLine(to: CGPoint(x: CGFloat(drawingWidth), y: y))
context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: y))
context.strokePath()
}

// vertical axis on the left
context.move(to: CGPoint(x: CGFloat(0), y: CGFloat(0)))
context.addLine(to: CGPoint(x: CGFloat(0), y: drawingHeight + topInset))
context.strokePath()


if leftInset < 20 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this this magic number?

// vertical axis on the left
context.move(to: CGPoint(x: CGFloat(0), y: CGFloat(0)))
context.addLine(to: CGPoint(x: CGFloat(0), y: drawingHeight + topInset))
context.strokePath()
}

// vertical axis on the right
context.move(to: CGPoint(x: CGFloat(drawingWidth), y: CGFloat(0)))
context.addLine(to: CGPoint(x: CGFloat(drawingWidth), y: drawingHeight + topInset))
context.move(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: CGFloat(0)))
context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: drawingHeight + topInset))
context.strokePath()

}
Expand Down Expand Up @@ -631,7 +644,11 @@ open class Chart: UIControl {

var labels: [Float]
if yLabels == nil {
labels = [(min.y + max.y) / 2, max.y]
if leftInset < 20 {
labels = [(min.y + max.y) / 2, max.y]
} else {
labels = [min.y, (min.y + max.y) / 2, max.y]
}
if yLabelsOnRightSide || min.y != 0 {
labels.insert(min.y, at: 0)
}
Expand Down Expand Up @@ -726,9 +743,11 @@ open class Chart: UIControl {
delegate?.didFinishTouchingChart(self)
return
}

drawHighlightLineFromLeftPosition(left)


if showHighlightLine == true {
drawHighlightLineFromLeftPosition(left)
}

if delegate == nil {
return
}
Expand Down