Skip to content

Commit

Permalink
Moved ChartUtils drawing methods into CGContext extension (#3086)
Browse files Browse the repository at this point in the history
* Fixed using wrong axis (Issue #2257)

* fix #1830. credit from #2049 (#2874)

* fix #1830. credit from #2049

* add combined chart unit tests for iOS, tvOS (macOS only have build process)

* use iterater rather than index

* Removed redundant ivars in BarLineChartViewBase (#3043)

* Removed redundant ivars in favour of proper access control

* Moved initialization of axes to their declaration to keep the same optionality exposed.

* Update 4.0.0 with master (#3135)

* Replaced relevant `ChartUtils` methods with `Double` extensions (#2994)

* Replaced relevant `ChartUtils` methods with `Double` extensions

Improves readability.
`nextUp` is built in and provides the same functionality.

* Updated `ChartUtilsTests` to match changes

* add option to build demo projects unit tests on iOS (#3121)

* add option to build demo projects unit tests on iOS

* add ChartsDemo-OSX build test.

* Update ViewPortHandler.swift (#3143)

fix a small bug

* Refactored ChartUtils method into CGPoint extension (#3087)

* Refactored ChartUtils method into CGPoint extension

* Replaced ChartUtils.defaultValueFormatter()

* Codestyle fixes

* Minor cleanup to Highlighter types (#3003)

* Minor cleanup to Highlighter types

* Fixes for PR

* Pulled master and updated code style

* added DataApproximator+N extension (#2848)

* added DataApproximator+N extension

* fixed PR notes

* Moved drawing methods into CGContext extension

Much nicer call sites.
Renamed some parameter names.
Removed `NSAttributedStringKey` where type inference was sufficient.
Minor tidy of drawText calls in AxisRenderers

* Pulled latest master

* Pulled master

* Fixed code style
  • Loading branch information
jjatie authored and liuxuan30 committed Jan 8, 2018
1 parent d9dba58 commit 9d8b76d
Show file tree
Hide file tree
Showing 23 changed files with 402 additions and 527 deletions.
2 changes: 1 addition & 1 deletion Source/Charts/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD

legendRenderer.renderLegend(context: context)

drawDescription(context: context)
drawDescription(in: context)

drawMarkers(context: context)
}
Expand Down
35 changes: 14 additions & 21 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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

Expand All @@ -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
Expand Down
11 changes: 5 additions & 6 deletions Source/Charts/Charts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ open class PieChartView: PieRadarChartViewBase

legendRenderer.renderLegend(context: context)

drawDescription(context: context)
drawDescription(in: context)

drawMarkers(context: context)
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Charts/RadarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ open class RadarChartView: PieRadarChartViewBase

legendRenderer.renderLegend(context: context)

drawDescription(context: context)
drawDescription(in: context)

drawMarkers(context: context)
}
Expand Down
9 changes: 4 additions & 5 deletions Source/Charts/Components/Legend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Components/YAxis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions Source/Charts/Highlight/RadarHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
30 changes: 11 additions & 19 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
Expand Down
23 changes: 10 additions & 13 deletions Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
30 changes: 13 additions & 17 deletions Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
44 changes: 18 additions & 26 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
}
}
Expand Down
Loading

0 comments on commit 9d8b76d

Please sign in to comment.