diff --git a/Source/Charts/Charts/BarLineChartViewBase.swift b/Source/Charts/Charts/BarLineChartViewBase.swift index ff61920f7c..aa7591fcb8 100644 --- a/Source/Charts/Charts/BarLineChartViewBase.swift +++ b/Source/Charts/Charts/BarLineChartViewBase.swift @@ -268,7 +268,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD legendRenderer.renderLegend(context: context) - drawDescription(context: context) + drawDescription(in: context) drawMarkers(context: context) } diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index c7967535f3..2135413042 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -265,25 +265,20 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate { let optionalContext = NSUIGraphicsGetCurrentContext() guard let context = optionalContext else { return } - - let frame = self.bounds if data === nil && !noDataText.isEmpty { context.saveGState() defer { context.restoreGState() } - ChartUtils.drawMultilineText( - context: context, - text: noDataText, - point: CGPoint(x: frame.width / 2.0, y: frame.height / 2.0), - attributes: - [.font: noDataFont, - .foregroundColor: noDataTextColor], - constrainedToSize: self.bounds.size, - anchor: CGPoint(x: 0.5, y: 0.5), - angleRadians: 0.0) - + context.drawMultilineText(noDataText, + at: CGPoint(x: bounds.width / 2.0, y: bounds.height / 2.0), + constrainedTo: bounds.size, + anchor: CGPoint(x: 0.5, y: 0.5), + angleRadians: 0.0, + attributes: [.font: noDataFont, + .foregroundColor: noDataTextColor]) + return } @@ -295,7 +290,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate } /// Draws the description text in the bottom right corner of the chart (per default) - internal func drawDescription(context: CGContext) + internal func drawDescription(in context: CGContext) { let description = chartDescription @@ -308,18 +303,16 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate let position = description.position ?? CGPoint(x: bounds.width - viewPortHandler.offsetRight - description.xOffset, y: bounds.height - viewPortHandler.offsetBottom - description.yOffset - description.font.lineHeight) - + let attrs: [NSAttributedStringKey : Any] = [ .font: description.font, .foregroundColor: description.textColor ] - ChartUtils.drawText( - context: context, - text: descriptionText, - point: position, - align: description.textAlign, - attributes: attrs) + context.drawText(descriptionText, + at: position, + align: description.textAlign, + attributes: attrs) } // MARK: - Highlighting diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index e8b653bc1e..b4bfbd6b7f 100644 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -119,7 +119,7 @@ open class PieChartView: PieRadarChartViewBase legendRenderer.renderLegend(context: context) - drawDescription(context: context) + drawDescription(in: context) drawMarkers(context: context) } @@ -397,11 +397,10 @@ open class PieChartView: PieRadarChartViewBase paragraphStyle.alignment = .center attrString = NSMutableAttributedString(string: newValue!) - attrString?.setAttributes([ - NSAttributedStringKey.foregroundColor: NSUIColor.black, - NSAttributedStringKey.font: NSUIFont.systemFont(ofSize: 12.0), - NSAttributedStringKey.paragraphStyle: paragraphStyle - ], range: NSMakeRange(0, attrString!.length)) + attrString?.setAttributes([.foregroundColor: NSUIColor.black, + .font: NSUIFont.systemFont(ofSize: 12.0), + .paragraphStyle: paragraphStyle], + range: NSMakeRange(0, attrString!.length)) } self.centerAttributedText = attrString } diff --git a/Source/Charts/Charts/RadarChartView.swift b/Source/Charts/Charts/RadarChartView.swift index 7b46449585..5fcb6c5374 100644 --- a/Source/Charts/Charts/RadarChartView.swift +++ b/Source/Charts/Charts/RadarChartView.swift @@ -140,7 +140,7 @@ open class RadarChartView: PieRadarChartViewBase legendRenderer.renderLegend(context: context) - drawDescription(context: context) + drawDescription(in: context) drawMarkers(context: context) } diff --git a/Source/Charts/Components/Legend.swift b/Source/Charts/Components/Legend.swift index e684b7b6fe..e1363aaaa3 100644 --- a/Source/Charts/Components/Legend.swift +++ b/Source/Charts/Components/Legend.swift @@ -167,7 +167,7 @@ open class Legend: ComponentBase guard let label = entry.label else { continue } - let size = (label as NSString!).size(withAttributes: [NSAttributedStringKey.font: font]) + let size = (label as NSString!).size(withAttributes: [.font: font]) if size.width > maxW { @@ -256,7 +256,7 @@ open class Legend: ComponentBase if label != nil { - let size = (label as NSString!).size(withAttributes: [NSAttributedStringKey.font: labelFont]) + let size = (label as NSString!).size(withAttributes: [.font: labelFont]) if drawingForm && !wasStacked { @@ -315,7 +315,6 @@ open class Legend: ComponentBase // Start calculating layout - let labelAttrs = [NSAttributedStringKey.font: labelFont] var maxLineWidth: CGFloat = 0.0 var currentLineWidth: CGFloat = 0.0 var requiredWidth: CGFloat = 0.0 @@ -341,9 +340,9 @@ open class Legend: ComponentBase } // grouped forms have null labels - if label != nil + if let label = label { - calculatedLabelSizes[i] = (label as NSString!).size(withAttributes: labelAttrs) + calculatedLabelSizes[i] = (label as NSString).size(withAttributes: [.font: labelFont]) requiredWidth += drawingForm ? formToTextSpace + formSize : 0.0 requiredWidth += calculatedLabelSizes[i].width } diff --git a/Source/Charts/Components/YAxis.swift b/Source/Charts/Components/YAxis.swift index cfe520776e..abd34f7241 100644 --- a/Source/Charts/Components/YAxis.swift +++ b/Source/Charts/Components/YAxis.swift @@ -111,7 +111,7 @@ open class YAxis: AxisBase @objc open func requiredSize() -> CGSize { let label = getLongestLabel() as NSString - var size = label.size(withAttributes: [NSAttributedStringKey.font: labelFont]) + var size = label.size(withAttributes: [.font: labelFont]) size.width += xOffset * 2.0 size.height += yOffset * 2.0 size.width = max(minWidth, min(size.width, maxWidth > 0.0 ? maxWidth : size.width)) diff --git a/Source/Charts/Highlight/RadarHighlighter.swift b/Source/Charts/Highlight/RadarHighlighter.swift index da36aedd4f..d54193c2f8 100644 --- a/Source/Charts/Highlight/RadarHighlighter.swift +++ b/Source/Charts/Highlight/RadarHighlighter.swift @@ -56,7 +56,7 @@ open class RadarHighlighter: PieRadarHighlighter let phaseY = chart.chartAnimator.phaseY let sliceangle = chart.sliceAngle let factor = chart.factor - + for i in chartData.dataSets.indices { guard @@ -68,7 +68,7 @@ open class RadarHighlighter: PieRadarHighlighter let p = chart.centerOffsets.moving(distance: CGFloat(y) * factor * CGFloat(phaseY), atAngle: sliceangle * CGFloat(index) * CGFloat(phaseX) + chart.rotationAngle) - + let highlight = Highlight(x: Double(index), y: entry.y, xPx: p.x, yPx: p.y, dataSetIndex: i, axis: dataSet.axisDependency) vals.append(highlight) } diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index c1a51662d8..007c83813f 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -389,12 +389,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer px += iconsOffset.x py += iconsOffset.y - ChartUtils.drawImage( - context: context, - image: icon, - x: px, - y: py, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: px, y: py), + size: icon.size) } } } @@ -477,12 +474,10 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer if let icon = e.icon, dataSet.isDrawIconsEnabled { - ChartUtils.drawImage( - context: context, - image: icon, - x: x + iconsOffset.x, - y: y + iconsOffset.y, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: x + iconsOffset.x, + y: y + iconsOffset.y), + size: icon.size) } } } @@ -518,12 +513,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer px += iconsOffset.x py += iconsOffset.y - ChartUtils.drawImage( - context: context, - image: icon, - x: px, - y: py, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: px, y: py), + size: icon.size) } } @@ -537,7 +529,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer /// Draws a value at the specified x and y position. @objc open func drawValue(context: CGContext, value: String, xPos: CGFloat, yPos: CGFloat, font: NSUIFont, align: NSTextAlignment, color: NSUIColor) { - ChartUtils.drawText(context: context, text: value, point: CGPoint(x: xPos, y: yPos), align: align, attributes: [NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: color]) + context.drawText(value, at: CGPoint(x: xPos, y: yPos), align: align, attributes: [.font: font, .foregroundColor: color]) } open override func drawExtras(context: CGContext) diff --git a/Source/Charts/Renderers/BubbleChartRenderer.swift b/Source/Charts/Renderers/BubbleChartRenderer.swift index bdf8f5c95d..dd097310c9 100644 --- a/Source/Charts/Renderers/BubbleChartRenderer.swift +++ b/Source/Charts/Renderers/BubbleChartRenderer.swift @@ -180,23 +180,20 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer if dataSet.isDrawValuesEnabled { - ChartUtils.drawText( - context: context, - text: text, - point: CGPoint( - x: pt.x, - y: pt.y - (0.5 * lineHeight)), - align: .center, - attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: valueTextColor]) + context.drawText(text, + at: CGPoint(x: pt.x, + y: pt.y - (0.5 * lineHeight)), + align: .center, + attributes: [.font: valueFont, + .foregroundColor: valueTextColor]) } if let icon = e.icon, dataSet.isDrawIconsEnabled { - ChartUtils.drawImage(context: context, - image: icon, - x: pt.x + iconsOffset.x, - y: pt.y + iconsOffset.y, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: pt.x + iconsOffset.x, + y: pt.y + iconsOffset.y), + size: icon.size) } } } diff --git a/Source/Charts/Renderers/CandleStickChartRenderer.swift b/Source/Charts/Renderers/CandleStickChartRenderer.swift index 8582db4869..6780627679 100644 --- a/Source/Charts/Renderers/CandleStickChartRenderer.swift +++ b/Source/Charts/Renderers/CandleStickChartRenderer.swift @@ -292,27 +292,23 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer if dataSet.isDrawValuesEnabled { - ChartUtils.drawText( - context: context, - text: formatter.stringForValue( - e.high, - entry: e, - dataSetIndex: i, - viewPortHandler: viewPortHandler), - point: CGPoint( - x: pt.x, - y: pt.y - yOffset), - align: .center, - attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: dataSet.valueTextColorAt(j)]) + context.drawText(formatter.stringForValue(e.high, + entry: e, + dataSetIndex: i, + viewPortHandler: viewPortHandler), + at: CGPoint(x: pt.x, + y: pt.y - yOffset), + align: .center, + attributes: [.font: valueFont, + .foregroundColor: dataSet.valueTextColorAt(j)]) } if let icon = e.icon, dataSet.isDrawIconsEnabled { - ChartUtils.drawImage(context: context, - image: icon, - x: pt.x + iconsOffset.x, - y: pt.y + iconsOffset.y, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: pt.x + iconsOffset.x, + y: pt.y + iconsOffset.y), + size: icon.size) } } } diff --git a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift index 373f4df331..768e79c307 100644 --- a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift +++ b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift @@ -372,7 +372,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer viewPortHandler: viewPortHandler) // calculate the correct offset depending on the draw position of the value - let valueTextWidth = valueText.size(withAttributes: [NSAttributedStringKey.font: valueFont]).width + let valueTextWidth = valueText.size(withAttributes: [.font: valueFont]).width posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus)) negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus) @@ -404,12 +404,9 @@ open class HorizontalBarChartRenderer: BarChartRenderer px += iconsOffset.x py += iconsOffset.y - ChartUtils.drawImage( - context: context, - image: icon, - x: px, - y: py, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: px, y: py), + size: icon.size) } } } @@ -485,12 +482,9 @@ open class HorizontalBarChartRenderer: BarChartRenderer px += iconsOffset.x py += iconsOffset.y - ChartUtils.drawImage( - context: context, - image: icon, - x: px, - y: py, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: px, y: py), + size: icon.size) } } else @@ -537,7 +531,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer viewPortHandler: viewPortHandler) // calculate the correct offset depending on the draw position of the value - let valueTextWidth = valueText.size(withAttributes: [NSAttributedStringKey.font: valueFont]).width + let valueTextWidth = valueText.size(withAttributes: [.font: valueFont]).width posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus)) negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus) @@ -570,22 +564,20 @@ open class HorizontalBarChartRenderer: BarChartRenderer if dataSet.isDrawValuesEnabled { drawValue(context: context, - value: valueText, - xPos: x, - yPos: y + yOffset, - font: valueFont, - align: textAlign, - color: dataSet.valueTextColorAt(index)) + value: valueText, + xPos: x, + yPos: y + yOffset, + font: valueFont, + align: textAlign, + color: dataSet.valueTextColorAt(index)) } if let icon = e.icon, dataSet.isDrawIconsEnabled { - ChartUtils.drawImage( - context: context, - image: icon, - x: x + iconsOffset.x, - y: y + iconsOffset.y, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: x + iconsOffset.x, + y: y + iconsOffset.y), + size: icon.size) } } } diff --git a/Source/Charts/Renderers/LegendRenderer.swift b/Source/Charts/Renderers/LegendRenderer.swift index a32344c3aa..4d32b23df7 100755 --- a/Source/Charts/Renderers/LegendRenderer.swift +++ b/Source/Charts/Renderers/LegendRenderer.swift @@ -465,7 +465,7 @@ open class LegendRenderer: NSObject, Renderer if direction == .rightToLeft { - posX -= (e.label as NSString!).size(withAttributes: [NSAttributedStringKey.font: labelFont]).width + posX -= (e.label as NSString!).size(withAttributes: [.font: labelFont]).width } if !wasStacked @@ -568,6 +568,6 @@ open class LegendRenderer: NSObject, Renderer /// Draws the provided label at the given position. @objc open func drawLabel(context: CGContext, x: CGFloat, y: CGFloat, label: String, font: NSUIFont, textColor: NSUIColor) { - ChartUtils.drawText(context: context, text: label, point: CGPoint(x: x, y: y), align: .left, attributes: [NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: textColor]) + context.drawText(label, at: CGPoint(x: x, y: y), align: .left, attributes: [.font: font, .foregroundColor: textColor]) } } diff --git a/Source/Charts/Renderers/LineChartRenderer.swift b/Source/Charts/Renderers/LineChartRenderer.swift index 21b175f73a..4c5cf262a0 100644 --- a/Source/Charts/Renderers/LineChartRenderer.swift +++ b/Source/Charts/Renderers/LineChartRenderer.swift @@ -556,28 +556,25 @@ open class LineChartRenderer: LineRadarRenderer continue } - if dataSet.isDrawValuesEnabled { - ChartUtils.drawText( - context: context, - text: formatter.stringForValue( - e.y, - entry: e, - dataSetIndex: i, - viewPortHandler: viewPortHandler), - point: CGPoint( - x: pt.x, - y: pt.y - CGFloat(valOffset) - valueFont.lineHeight), - align: .center, - attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: dataSet.valueTextColorAt(j)]) + if dataSet.isDrawValuesEnabled + { + context.drawText(formatter.stringForValue(e.y, + entry: e, + dataSetIndex: i, + viewPortHandler: viewPortHandler), + at: CGPoint(x: pt.x, + y: pt.y - CGFloat(valOffset) - valueFont.lineHeight), + align: .center, + attributes: [.font: valueFont, + .foregroundColor: dataSet.valueTextColorAt(j)]) } if let icon = e.icon, dataSet.isDrawIconsEnabled { - ChartUtils.drawImage(context: context, - image: icon, - x: pt.x + iconsOffset.x, - y: pt.y + iconsOffset.y, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: pt.x + iconsOffset.x, + y: pt.y + iconsOffset.y), + size: icon.size) } } } diff --git a/Source/Charts/Renderers/PieChartRenderer.swift b/Source/Charts/Renderers/PieChartRenderer.swift index c71959e8e1..cd018b5efb 100644 --- a/Source/Charts/Renderers/PieChartRenderer.swift +++ b/Source/Charts/Renderers/PieChartRenderer.swift @@ -426,51 +426,42 @@ open class PieChartRenderer: NSObject, DataRenderer if drawXOutside && drawYOutside { - ChartUtils.drawText( - context: context, - text: valueText, - point: labelPoint, - align: align, - attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: valueTextColor] - ) + context.drawText(valueText, + at: labelPoint, + align: align, + attributes: [.font: valueFont, + .foregroundColor: valueTextColor]) if j < data.entryCount && pe?.label != nil { - ChartUtils.drawText( - context: context, - text: pe!.label!, - point: CGPoint(x: labelPoint.x, y: labelPoint.y + lineHeight), - align: align, - attributes: [ - NSAttributedStringKey.font: entryLabelFont ?? valueFont, - NSAttributedStringKey.foregroundColor: entryLabelColor ?? valueTextColor] - ) + context.drawText(pe!.label!, + at: CGPoint(x: labelPoint.x, + y: labelPoint.y + lineHeight), + align: align, + attributes: [.font: entryLabelFont ?? valueFont, + .foregroundColor: entryLabelColor ?? valueTextColor]) } } else if drawXOutside { if j < data.entryCount && pe?.label != nil { - ChartUtils.drawText( - context: context, - text: pe!.label!, - point: CGPoint(x: labelPoint.x, y: labelPoint.y + lineHeight / 2.0), - align: align, - attributes: [ - NSAttributedStringKey.font: entryLabelFont ?? valueFont, - NSAttributedStringKey.foregroundColor: entryLabelColor ?? valueTextColor] - ) + context.drawText(pe!.label!, + at: CGPoint(x: labelPoint.x, + y: labelPoint.y + lineHeight / 2.0), + align: align, + attributes: [.font: entryLabelFont ?? valueFont, + .foregroundColor: entryLabelColor ?? valueTextColor]) } } else if drawYOutside { - ChartUtils.drawText( - context: context, - text: valueText, - point: CGPoint(x: labelPoint.x, y: labelPoint.y + lineHeight / 2.0), - align: align, - attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: valueTextColor] - ) + context.drawText(valueText, + at: CGPoint(x: labelPoint.x, + y: labelPoint.y + lineHeight / 2.0), + align: align, + attributes: [.font: valueFont, + .foregroundColor: valueTextColor]) } } @@ -479,54 +470,40 @@ open class PieChartRenderer: NSObject, DataRenderer // calculate the text position let x = labelRadius * sliceXBase + center.x let y = labelRadius * sliceYBase + center.y - lineHeight - + if drawXInside && drawYInside { - ChartUtils.drawText( - context: context, - text: valueText, - point: CGPoint(x: x, y: y), - align: .center, - attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: valueTextColor] - ) + context.drawText(valueText, + at: CGPoint(x: x, y: y), + align: .center, + attributes: [.font: valueFont, .foregroundColor: valueTextColor]) if j < data.entryCount && pe?.label != nil { - ChartUtils.drawText( - context: context, - text: pe!.label!, - point: CGPoint(x: x, y: y + lineHeight), - align: .center, - attributes: [ - NSAttributedStringKey.font: entryLabelFont ?? valueFont, - NSAttributedStringKey.foregroundColor: entryLabelColor ?? valueTextColor] - ) + context.drawText(pe!.label!, + at: CGPoint(x: x, y: y + lineHeight), + align: .center, + attributes: [.font: entryLabelFont ?? valueFont, + .foregroundColor: entryLabelColor ?? valueTextColor]) } } else if drawXInside { if j < data.entryCount && pe?.label != nil { - ChartUtils.drawText( - context: context, - text: pe!.label!, - point: CGPoint(x: x, y: y + lineHeight / 2.0), - align: .center, - attributes: [ - NSAttributedStringKey.font: entryLabelFont ?? valueFont, - NSAttributedStringKey.foregroundColor: entryLabelColor ?? valueTextColor] - ) + context.drawText(pe!.label!, + at: CGPoint(x: x, y: y + lineHeight / 2.0), + align: .center, + attributes: [.font: entryLabelFont ?? valueFont, + .foregroundColor: entryLabelColor ?? valueTextColor]) } } else if drawYInside { - ChartUtils.drawText( - context: context, - text: valueText, - point: CGPoint(x: x, y: y + lineHeight / 2.0), - align: .center, - attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: valueTextColor] - ) + context.drawText(valueText, + at: CGPoint(x: x, y: y + lineHeight / 2.0), + align: .center, + attributes: [.font: valueFont, .foregroundColor: valueTextColor]) } } @@ -538,11 +515,9 @@ open class PieChartRenderer: NSObject, DataRenderer var y = (labelRadius + iconsOffset.y) * sliceYBase + center.y y += iconsOffset.x - ChartUtils.drawImage(context: context, - image: icon, - x: x, - y: y, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: x, y: y), + size: icon.size) } xIndex += 1 diff --git a/Source/Charts/Renderers/RadarChartRenderer.swift b/Source/Charts/Renderers/RadarChartRenderer.swift index d603a0aad1..a8438b8066 100644 --- a/Source/Charts/Renderers/RadarChartRenderer.swift +++ b/Source/Charts/Renderers/RadarChartRenderer.swift @@ -177,18 +177,14 @@ open class RadarChartRenderer: LineRadarRenderer if dataSet.isDrawValuesEnabled { - ChartUtils.drawText( - context: context, - text: formatter.stringForValue( - e.y, - entry: e, - dataSetIndex: i, - viewPortHandler: viewPortHandler), - point: CGPoint(x: p.x, y: p.y - yoffset - valueFont.lineHeight), - align: .center, - attributes: [NSAttributedStringKey.font: valueFont, - NSAttributedStringKey.foregroundColor: dataSet.valueTextColorAt(j)] - ) + context.drawText(formatter.stringForValue(e.y, + entry: e, + dataSetIndex: i, + viewPortHandler: viewPortHandler), + at: CGPoint(x: p.x, y: p.y - yoffset - valueFont.lineHeight), + align: .center, + attributes: [.font: valueFont, + .foregroundColor: dataSet.valueTextColorAt(j)]) } if let icon = e.icon, dataSet.isDrawIconsEnabled @@ -197,11 +193,9 @@ open class RadarChartRenderer: LineRadarRenderer atAngle: sliceangle * CGFloat(j) * CGFloat(phaseX) + chart.rotationAngle) pIcon.y += iconsOffset.x - ChartUtils.drawImage(context: context, - image: icon, - x: pIcon.x, - y: pIcon.y, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: pIcon.x, y: pIcon.y), + size: icon.size) } } } diff --git a/Source/Charts/Renderers/ScatterChartRenderer.swift b/Source/Charts/Renderers/ScatterChartRenderer.swift index d7b1afa3a5..f2c97749b0 100644 --- a/Source/Charts/Renderers/ScatterChartRenderer.swift +++ b/Source/Charts/Renderers/ScatterChartRenderer.swift @@ -165,24 +165,21 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer if dataSet.isDrawValuesEnabled { - ChartUtils.drawText( - context: context, - text: text, - point: CGPoint( - x: pt.x, - y: pt.y - shapeSize - lineHeight), - align: .center, - attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: dataSet.valueTextColorAt(j)] + context.drawText(text, + at: CGPoint(x: pt.x, + y: pt.y - shapeSize - lineHeight), + align: .center, + attributes: [.font: valueFont, + .foregroundColor: dataSet.valueTextColorAt(j)] ) } if let icon = e.icon, dataSet.isDrawIconsEnabled { - ChartUtils.drawImage(context: context, - image: icon, - x: pt.x + iconsOffset.x, - y: pt.y + iconsOffset.y, - size: icon.size) + context.drawImage(icon, + atCenter: CGPoint(x: pt.x + iconsOffset.x, + y: pt.y + iconsOffset.y), + size: icon.size) } } } diff --git a/Source/Charts/Renderers/XAxisRenderer.swift b/Source/Charts/Renderers/XAxisRenderer.swift index 6cc566aac3..5d58df36dd 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -189,8 +189,8 @@ open class XAxisRenderer: NSObject, AxisRenderer { let longest = axis.getLongestLabel() - let labelSize = longest.size(withAttributes: [NSAttributedStringKey.font: axis.labelFont]) - + let labelSize = longest.size(withAttributes: [.font: axis.labelFont]) + let labelWidth = labelSize.width let labelHeight = labelSize.height @@ -293,11 +293,11 @@ open class XAxisRenderer: NSObject, AxisRenderer #endif paraStyle.alignment = .center - let labelAttrs: [NSAttributedStringKey : Any] = [NSAttributedStringKey.font: axis.labelFont, - NSAttributedStringKey.foregroundColor: axis.labelTextColor, - NSAttributedStringKey.paragraphStyle: paraStyle] + let labelAttrs: [NSAttributedStringKey : Any] = [.font: axis.labelFont, + .foregroundColor: axis.labelTextColor, + .paragraphStyle: paraStyle] let labelRotationAngleRadians = axis.labelRotationAngle.DEG2RAD - + let centeringEnabled = axis.isCenterAxisLabelsEnabled let valueToPixelMatrix = transformer.valueToPixelMatrix @@ -358,7 +358,7 @@ open class XAxisRenderer: NSObject, AxisRenderer x: position.x, y: pos, attributes: labelAttrs, - constrainedToSize: labelMaxSize, + constrainedTo: labelMaxSize, anchor: anchor, angleRadians: labelRotationAngleRadians) } @@ -371,18 +371,16 @@ open class XAxisRenderer: NSObject, AxisRenderer x: CGFloat, y: CGFloat, attributes: [NSAttributedStringKey : Any], - constrainedToSize: CGSize, + constrainedTo size: CGSize, anchor: CGPoint, angleRadians: CGFloat) { - ChartUtils.drawMultilineText( - context: context, - text: formattedLabel, - point: CGPoint(x: x, y: y), - attributes: attributes, - constrainedToSize: constrainedToSize, - anchor: anchor, - angleRadians: angleRadians) + context.drawMultilineText(formattedLabel, + at: CGPoint(x: x, y: y), + constrainedTo: size, + anchor: anchor, + angleRadians: angleRadians, + attributes: attributes) } open func renderGridLines(context: CGContext) @@ -522,48 +520,38 @@ open class XAxisRenderer: NSObject, AxisRenderer let labelLineHeight = limitLine.valueFont.lineHeight let xOffset: CGFloat = limitLine.lineWidth + limitLine.xOffset - - if limitLine.labelPosition == .rightTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x + xOffset, - y: viewPortHandler.contentTop + yOffset), - align: .left, - attributes: [NSAttributedStringKey.font: limitLine.valueFont, NSAttributedStringKey.foregroundColor: limitLine.valueTextColor]) - } - else if limitLine.labelPosition == .rightBottom - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x + xOffset, - y: viewPortHandler.contentBottom - labelLineHeight - yOffset), - align: .left, - attributes: [NSAttributedStringKey.font: limitLine.valueFont, NSAttributedStringKey.foregroundColor: limitLine.valueTextColor]) - } - else if limitLine.labelPosition == .leftTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x - xOffset, - y: viewPortHandler.contentTop + yOffset), - align: .right, - attributes: [NSAttributedStringKey.font: limitLine.valueFont, NSAttributedStringKey.foregroundColor: limitLine.valueTextColor]) - } - else + + let align: NSTextAlignment + let point: CGPoint + + switch limitLine.labelPosition { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x - xOffset, - y: viewPortHandler.contentBottom - labelLineHeight - yOffset), - align: .right, - attributes: [NSAttributedStringKey.font: limitLine.valueFont, NSAttributedStringKey.foregroundColor: limitLine.valueTextColor]) + case .rightTop: + align = .left + point = CGPoint(x: position.x + xOffset, + y: viewPortHandler.contentTop + yOffset) + + case .rightBottom: + align = .left + point = CGPoint(x: position.x + xOffset, + y: viewPortHandler.contentBottom - labelLineHeight - yOffset) + + case .leftTop: + align = .right + point = CGPoint(x: position.x - xOffset, + y: viewPortHandler.contentTop + yOffset) + + case .leftBottom: + align = .right + point = CGPoint(x: position.x - xOffset, + y: viewPortHandler.contentBottom - labelLineHeight - yOffset) } + + context.drawText(label, + at: point, + align: align, + attributes: [.font: limitLine.valueFont, + .foregroundColor: limitLine.valueTextColor]) } } - } diff --git a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift index dc9580d4db..d50b9ccf64 100644 --- a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift @@ -60,8 +60,8 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer { let longest = axis.getLongestLabel() as NSString - let labelSize = longest.size(withAttributes: [NSAttributedStringKey.font: axis.labelFont]) - + let labelSize = longest.size(withAttributes: [.font: axis.labelFont]) + let labelWidth = floor(labelSize.width + axis.xOffset * 3.5) let labelHeight = labelSize.height let labelRotatedSize = CGSize(width: labelSize.width, height: labelHeight).rotatedBy(degrees: axis.labelRotationAngle) @@ -144,7 +144,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer formattedLabel: label, x: pos, y: position.y, - attributes: [NSAttributedStringKey.font: labelFont, NSAttributedStringKey.foregroundColor: labelTextColor], + attributes: [.font: labelFont, .foregroundColor: labelTextColor], anchor: anchor, angleRadians: labelRotationAngleRadians) } @@ -161,13 +161,11 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer anchor: CGPoint, angleRadians: CGFloat) { - ChartUtils.drawText( - context: context, - text: formattedLabel, - point: CGPoint(x: x, y: y), - attributes: attributes, - anchor: anchor, - angleRadians: angleRadians) + context.drawText(formattedLabel, + at: CGPoint(x: x, y: y), + anchor: anchor, + angleRadians: angleRadians, + attributes: attributes) } open override var gridClippingRect: CGRect @@ -297,47 +295,37 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer let xOffset: CGFloat = 4.0 + l.xOffset let yOffset: CGFloat = l.lineWidth + labelLineHeight + l.yOffset - - if l.labelPosition == .rightTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: viewPortHandler.contentRight - xOffset, - y: position.y - yOffset), - align: .right, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) - } - else if l.labelPosition == .rightBottom - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: viewPortHandler.contentRight - xOffset, - y: position.y + yOffset - labelLineHeight), - align: .right, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) - } - else if l.labelPosition == .leftTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: viewPortHandler.contentLeft + xOffset, - y: position.y - yOffset), - align: .left, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) - } - else + + let align: NSTextAlignment + let point: CGPoint + + switch l.labelPosition { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: viewPortHandler.contentLeft + xOffset, - y: position.y + yOffset - labelLineHeight), - align: .left, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) + case .rightTop: + align = .right + point = CGPoint(x: viewPortHandler.contentRight - xOffset, + y: position.y - yOffset) + + case .rightBottom: + align = .right + point = CGPoint(x: viewPortHandler.contentRight - xOffset, + y: position.y + yOffset - labelLineHeight) + + case .leftTop: + align = .left + point = CGPoint(x: viewPortHandler.contentLeft + xOffset, + y: position.y - yOffset) + + case .leftBottom: + align = .left + point = CGPoint(x: viewPortHandler.contentLeft + xOffset, + y: position.y + yOffset - labelLineHeight) } + + context.drawText(label, + at: point, + align: align, + attributes: [.font: l.valueFont, .foregroundColor: l.valueTextColor]) } } } diff --git a/Source/Charts/Renderers/XAxisRendererRadarChart.swift b/Source/Charts/Renderers/XAxisRendererRadarChart.swift index 80cab48fe4..a408df9b68 100644 --- a/Source/Charts/Renderers/XAxisRendererRadarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererRadarChart.swift @@ -56,12 +56,12 @@ open class XAxisRendererRadarChart: XAxisRenderer let angle = (sliceangle * CGFloat(i) + chart.rotationAngle).truncatingRemainder(dividingBy: 360.0) let p = center.moving(distance: CGFloat(chart.yRange) * factor + axis.labelRotatedWidth / 2.0, atAngle: angle) - + drawLabel(context: context, formattedLabel: label, x: p.x, y: p.y - axis.labelRotatedHeight / 2.0, - attributes: [NSAttributedStringKey.font: labelFont, NSAttributedStringKey.foregroundColor: labelTextColor], + attributes: [.font: labelFont, .foregroundColor: labelTextColor], anchor: drawLabelAnchor, angleRadians: labelRotationAngleRadians) } @@ -76,13 +76,11 @@ open class XAxisRendererRadarChart: XAxisRenderer anchor: CGPoint, angleRadians: CGFloat) { - ChartUtils.drawText( - context: context, - text: formattedLabel, - point: CGPoint(x: x, y: y), - attributes: attributes, - anchor: anchor, - angleRadians: angleRadians) + context.drawText(formattedLabel, + at: CGPoint(x: x, y: y), + anchor: anchor, + angleRadians: angleRadians, + attributes: attributes) } open override func renderLimitLines(context: CGContext) diff --git a/Source/Charts/Renderers/YAxisRenderer.swift b/Source/Charts/Renderers/YAxisRenderer.swift index e8a684b49b..d77cb0f31b 100644 --- a/Source/Charts/Renderers/YAxisRenderer.swift +++ b/Source/Charts/Renderers/YAxisRenderer.swift @@ -142,12 +142,10 @@ open class YAxisRenderer: NSObject, AxisRenderer { let text = axis.getFormattedLabel(i) - ChartUtils.drawText( - context: context, - text: text, - point: CGPoint(x: fixedPosition, y: positions[i].y + offset), - align: textAlign, - attributes: [NSAttributedStringKey.font: labelFont, NSAttributedStringKey.foregroundColor: labelTextColor]) + context.drawText(text, + at: CGPoint(x: fixedPosition, y: positions[i].y + offset), + align: textAlign, + attributes: [.font: labelFont, .foregroundColor: labelTextColor]) } } @@ -330,49 +328,39 @@ open class YAxisRenderer: NSObject, AxisRenderer { let labelLineHeight = l.valueFont.lineHeight - let xOffset: CGFloat = 4.0 + l.xOffset - let yOffset: CGFloat = l.lineWidth + labelLineHeight + l.yOffset - - if l.labelPosition == .rightTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: viewPortHandler.contentRight - xOffset, - y: position.y - yOffset), - align: .right, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) - } - else if l.labelPosition == .rightBottom - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: viewPortHandler.contentRight - xOffset, - y: position.y + yOffset - labelLineHeight), - align: .right, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) - } - else if l.labelPosition == .leftTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: viewPortHandler.contentLeft + xOffset, - y: position.y - yOffset), - align: .left, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) - } - else + let xOffset = 4.0 + l.xOffset + let yOffset = l.lineWidth + labelLineHeight + l.yOffset + + let align: NSTextAlignment + let point: CGPoint + + switch l.labelPosition { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: viewPortHandler.contentLeft + xOffset, - y: position.y + yOffset - labelLineHeight), - align: .left, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) + case .rightTop: + align = .right + point = CGPoint(x: viewPortHandler.contentRight - xOffset, + y: position.y - yOffset) + + case .rightBottom: + align = .right + point = CGPoint(x: viewPortHandler.contentRight - xOffset, + y: position.y + yOffset - labelLineHeight) + + case .leftTop: + align = .left + point = CGPoint(x: viewPortHandler.contentLeft + xOffset, + y: position.y - yOffset) + + case .leftBottom: + align = .left + point = CGPoint(x: viewPortHandler.contentLeft + xOffset, + y: position.y + yOffset - labelLineHeight) } + + context.drawText(label, + at: point, + align: align, + attributes: [.font: l.valueFont, .foregroundColor: l.valueTextColor]) } } diff --git a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift index 163a74d146..78e36e8f36 100644 --- a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift @@ -155,12 +155,10 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer { let text = axis.getFormattedLabel(i) - ChartUtils.drawText( - context: context, - text: text, - point: CGPoint(x: positions[i].x, y: fixedPosition - offset), - align: .center, - attributes: [NSAttributedStringKey.font: labelFont, NSAttributedStringKey.foregroundColor: labelTextColor]) + context.drawText(text, + at: CGPoint(x: positions[i].x, y: fixedPosition - offset), + align: .center, + attributes: [.font: labelFont, .foregroundColor: labelTextColor]) } } @@ -304,46 +302,36 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer let xOffset: CGFloat = l.lineWidth + l.xOffset let yOffset: CGFloat = 2.0 + l.yOffset - if l.labelPosition == .rightTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x + xOffset, - y: viewPortHandler.contentTop + yOffset), - align: .left, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) - } - else if l.labelPosition == .rightBottom - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x + xOffset, - y: viewPortHandler.contentBottom - labelLineHeight - yOffset), - align: .left, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) - } - else if l.labelPosition == .leftTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x - xOffset, - y: viewPortHandler.contentTop + yOffset), - align: .right, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) - } - else + let align: NSTextAlignment + let point: CGPoint + + switch l.labelPosition { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x - xOffset, - y: viewPortHandler.contentBottom - labelLineHeight - yOffset), - align: .right, - attributes: [NSAttributedStringKey.font: l.valueFont, NSAttributedStringKey.foregroundColor: l.valueTextColor]) + case .rightTop: + align = .left + point = CGPoint(x: position.x + xOffset, + y: viewPortHandler.contentTop + yOffset) + + case .rightBottom: + align = .left + point = CGPoint(x: position.x + xOffset, + y: viewPortHandler.contentBottom - labelLineHeight - yOffset) + + case .leftTop: + align = .right + point = CGPoint(x: position.x - xOffset, + y: viewPortHandler.contentTop + yOffset) + + case .leftBottom: + align = .right + point = CGPoint(x: position.x - xOffset, + y: viewPortHandler.contentBottom - labelLineHeight - yOffset) } + + context.drawText(label, + at: point, + align: align, + attributes: [.font: l.valueFont, .foregroundColor: l.valueTextColor]) } } diff --git a/Source/Charts/Renderers/YAxisRendererRadarChart.swift b/Source/Charts/Renderers/YAxisRendererRadarChart.swift index c9bd090537..0e5efd23c5 100644 --- a/Source/Charts/Renderers/YAxisRendererRadarChart.swift +++ b/Source/Charts/Renderers/YAxisRendererRadarChart.swift @@ -183,15 +183,11 @@ open class YAxisRendererRadarChart: YAxisRenderer let label = axis.getFormattedLabel(j) - ChartUtils.drawText( - context: context, - text: label, - point: CGPoint(x: p.x + 10.0, y: p.y - labelLineHeight), - align: .left, - attributes: [ - NSAttributedStringKey.font: labelFont, - NSAttributedStringKey.foregroundColor: labelTextColor - ]) + context.drawText(label, + at: CGPoint(x: p.x + 10.0, y: p.y - labelLineHeight), + align: .left, + attributes: [.font: labelFont, + .foregroundColor: labelTextColor]) } } diff --git a/Source/Charts/Utils/ChartUtils.swift b/Source/Charts/Utils/ChartUtils.swift index 3e15790eb9..b8cb16667d 100644 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -99,55 +99,65 @@ extension CGPoint open class ChartUtils { - private static var _defaultValueFormatter: ValueFormatter = ChartUtils.generateDefaultValueFormatter() - - open class func drawImage( - context: CGContext, - image: NSUIImage, - x: CGFloat, - y: CGFloat, - size: CGSize) + private static var _defaultValueFormatter: ValueFormatter = generateDefaultValueFormatter() + + private class func generateDefaultValueFormatter() -> ValueFormatter + { + let formatter = DefaultValueFormatter(decimals: 1) + return formatter + } + + /// - returns: The default value formatter used for all chart components that needs a default + open class func defaultValueFormatter() -> ValueFormatter + { + return _defaultValueFormatter + } +} + +extension CGContext { + + open func drawImage(_ image: NSUIImage, atCenter center: CGPoint, size: CGSize) { var drawOffset = CGPoint() - drawOffset.x = x - (size.width / 2) - drawOffset.y = y - (size.height / 2) - - NSUIGraphicsPushContext(context) - + drawOffset.x = center.x - (size.width / 2) + drawOffset.y = center.y - (size.height / 2) + + NSUIGraphicsPushContext(self) + if image.size.width != size.width && image.size.height != size.height { let key = "resized_\(size.width)_\(size.height)" - + // Try to take scaled image from cache of this image var scaledImage = objc_getAssociatedObject(image, key) as? NSUIImage if scaledImage == nil { // Scale the image NSUIGraphicsBeginImageContextWithOptions(size, false, 0.0) - - image.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: size)) - + + image.draw(in: CGRect(origin: .zero, size: size)) + scaledImage = NSUIGraphicsGetImageFromCurrentImageContext() NSUIGraphicsEndImageContext() - + // Put the scaled image in a cache owned by the original image objc_setAssociatedObject(image, key, scaledImage, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } - + scaledImage?.draw(in: CGRect(origin: drawOffset, size: size)) } else { image.draw(in: CGRect(origin: drawOffset, size: size)) } - + NSUIGraphicsPopContext() } - - open class func drawText(context: CGContext, text: String, point: CGPoint, align: NSTextAlignment, attributes: [NSAttributedStringKey : Any]?) + + open func drawText(_ text: String, at point: CGPoint, align: NSTextAlignment, attributes: [NSAttributedStringKey : Any]?) { var point = point - + if align == .center { point.x -= text.size(withAttributes: attributes).width / 2.0 @@ -156,96 +166,96 @@ open class ChartUtils { point.x -= text.size(withAttributes: attributes).width } - - NSUIGraphicsPushContext(context) - + + NSUIGraphicsPushContext(self) + (text as NSString).draw(at: point, withAttributes: attributes) - + NSUIGraphicsPopContext() } - - open class func drawText(context: CGContext, text: String, point: CGPoint, attributes: [NSAttributedStringKey : Any]?, anchor: CGPoint, angleRadians: CGFloat) + + open func drawText(_ text: String, at point: CGPoint, anchor: CGPoint, angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) { var drawOffset = CGPoint() - - NSUIGraphicsPushContext(context) - + + NSUIGraphicsPushContext(self) + if angleRadians != 0.0 { let size = text.size(withAttributes: attributes) - + // Move the text drawing rect in a way that it always rotates around its center drawOffset.x = -size.width * 0.5 drawOffset.y = -size.height * 0.5 - + var translate = point - + // Move the "outer" rect relative to the anchor, assuming its centered if anchor.x != 0.5 || anchor.y != 0.5 { let rotatedSize = size.rotatedBy(radians: angleRadians) - + translate.x -= rotatedSize.width * (anchor.x - 0.5) translate.y -= rotatedSize.height * (anchor.y - 0.5) } - - context.saveGState() - context.translateBy(x: translate.x, y: translate.y) - context.rotate(by: angleRadians) - + + saveGState() + translateBy(x: translate.x, y: translate.y) + rotate(by: angleRadians) + (text as NSString).draw(at: drawOffset, withAttributes: attributes) - - context.restoreGState() + + restoreGState() } else { if anchor.x != 0.0 || anchor.y != 0.0 { let size = text.size(withAttributes: attributes) - + drawOffset.x = -size.width * anchor.x drawOffset.y = -size.height * anchor.y } - + drawOffset.x += point.x drawOffset.y += point.y - + (text as NSString).draw(at: drawOffset, withAttributes: attributes) } - + NSUIGraphicsPopContext() } - - internal class func drawMultilineText(context: CGContext, text: String, knownTextSize: CGSize, point: CGPoint, attributes: [NSAttributedStringKey : Any]?, constrainedToSize: CGSize, anchor: CGPoint, angleRadians: CGFloat) + + internal func drawMultilineText(_ text: String, at point: CGPoint, constrainedTo size: CGSize, anchor: CGPoint, knownTextSize: CGSize, angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) { - var rect = CGRect(origin: CGPoint(), size: knownTextSize) - - NSUIGraphicsPushContext(context) - + var rect = CGRect(origin: .zero, size: knownTextSize) + + NSUIGraphicsPushContext(self) + if angleRadians != 0.0 { // Move the text drawing rect in a way that it always rotates around its center rect.origin.x = -knownTextSize.width * 0.5 rect.origin.y = -knownTextSize.height * 0.5 - + var translate = point - + // Move the "outer" rect relative to the anchor, assuming its centered if anchor.x != 0.5 || anchor.y != 0.5 { let rotatedSize = knownTextSize.rotatedBy(radians: angleRadians) - + translate.x -= rotatedSize.width * (anchor.x - 0.5) translate.y -= rotatedSize.height * (anchor.y - 0.5) } - - context.saveGState() - context.translateBy(x: translate.x, y: translate.y) - context.rotate(by: angleRadians) - + + saveGState() + translateBy(x: translate.x, y: translate.y) + rotate(by: angleRadians) + (text as NSString).draw(with: rect, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) - - context.restoreGState() + + restoreGState() } else { @@ -254,31 +264,19 @@ open class ChartUtils rect.origin.x = -knownTextSize.width * anchor.x rect.origin.y = -knownTextSize.height * anchor.y } - + rect.origin.x += point.x rect.origin.y += point.y - + (text as NSString).draw(with: rect, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) } - + NSUIGraphicsPopContext() } - - internal class func drawMultilineText(context: CGContext, text: String, point: CGPoint, attributes: [NSAttributedStringKey : Any]?, constrainedToSize: CGSize, anchor: CGPoint, angleRadians: CGFloat) - { - let rect = text.boundingRect(with: constrainedToSize, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) - drawMultilineText(context: context, text: text, knownTextSize: rect.size, point: point, attributes: attributes, constrainedToSize: constrainedToSize, anchor: anchor, angleRadians: angleRadians) - } - private class func generateDefaultValueFormatter() -> ValueFormatter - { - let formatter = DefaultValueFormatter(decimals: 1) - return formatter - } - - /// - returns: The default value formatter used for all chart components that needs a default - open class func defaultValueFormatter() -> ValueFormatter + internal func drawMultilineText(_ text: String, at point: CGPoint, constrainedTo size: CGSize, anchor: CGPoint, angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) { - return _defaultValueFormatter + let rect = text.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) + drawMultilineText(text, at: point, constrainedTo: size, anchor: anchor, knownTextSize: rect.size, angleRadians: angleRadians, attributes: attributes) } }