Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Left inset & a option to hide highlight line when touched #83

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ let chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
| `showXLabelsAndGrid` | Enable the lines for the labels on the x-axis (`Bool`, default `true`) |
| `showYLabelsAndGrid` | Enable the lines for the labels on the y-axis (`Bool`, default `true`) |
| `topInset` | Height of the area at the top of the chart, acting a padding to make place for the top y-axis label (`CGFloat`, default `20`) |
| `leftInset` | Width of the area at the left of the chart, acting a padding to make place for the left x-axis label. |
| `xLabels` | The values to display as labels on the x-axis. You can format these values with the `xLabelFormatter` attribute. As default, it will display the values of the series which has the most data. `[Double]?` |
| `xLabelsFormatter` | Function to format the labels on the x-axis (`(Int, Double) -> String`) |
| `xLabelsOrientation:` | Set the x-axis labels orientation to `vertical` or `horizontal` (`ChartLabelOrientation`, default `.horizontal`) |
Expand Down Expand Up @@ -375,7 +376,6 @@ Use the `ChartDelegate` protocol to tell other objects about the chart’s touch

Shorthands for various colors.

**Example**

```swift
let series = ChartSeries([0, 6, 2, 8, 4, 7, 3, 10, 8])
Expand Down
58 changes: 36 additions & 22 deletions Source/Chart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,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


/**
Width of the area at the left of the chart, acting a padding to make place for the left x-axis label.
*/
open var leftInset: CGFloat = 0

/**
Width of the chart's lines.
*/
Expand Down Expand Up @@ -189,8 +194,8 @@ open class Chart: UIControl {
open var maxY: Double?

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

/**
Expand Down Expand Up @@ -310,8 +315,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 @@ -417,8 +422,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,31 +527,35 @@ 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()
}

//Display vertical axis to the right if left inset
if leftInset == 0 {
// 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()
} else {
// vertical axis on the right
context.move(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: CGFloat(0)))
context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: drawingHeight + topInset))
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()

// 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.strokePath()
}

fileprivate func drawLabelsAndGridOnXAxis() {
Expand Down Expand Up @@ -626,7 +636,11 @@ open class Chart: UIControl {

var labels: [Double]
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 @@ -719,7 +733,7 @@ open class Chart: UIControl {
}

drawHighlightLineFromLeftPosition(left)

if delegate == nil {
return
}
Expand Down