diff --git a/README.md b/README.md index 7ae170e44..9729c8dc1 100644 --- a/README.md +++ b/README.md @@ -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`) | @@ -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]) diff --git a/Source/Chart.swift b/Source/Chart.swift index 96a7b4856..cf37e2bce 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -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. */ @@ -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 /** @@ -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 @@ -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 } @@ -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() { @@ -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) } @@ -719,7 +733,7 @@ open class Chart: UIControl { } drawHighlightLineFromLeftPosition(left) - + if delegate == nil { return }