Skip to content

Commit

Permalink
all renderers are drawing icon image if it is stored in entitie's dat…
Browse files Browse the repository at this point in the history
…a property and dataset allows
  • Loading branch information
abjurato committed Nov 5, 2016
1 parent b64fa1b commit 5161890
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ open class BarChartDataEntry: ChartDataEntry
calcRanges()
}

/// Constructor for stacked bar entries. One data object for whole stack
public init(x: Double, yValues: [Double], data: AnyObject?)
{
super.init(x: x, y: BarChartDataEntry.calcSum(values: yValues), data: data)
self._yVals = yValues
calcPosNegSum()
}

/// Constructor for normal bars (not stacked).
public override init(x: Double, y: Double, data: AnyObject?)
{
Expand Down
120 changes: 81 additions & 39 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,20 +419,32 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

let val = e.y

drawValue(
context: context,
value: formatter.stringForValue(
val,
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
xPos: x,
yPos: val >= 0.0
? (rect.origin.y + posOffset)
: (rect.origin.y + rect.size.height + negOffset),
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(j))
if dataSet.isDrawValuesEnabled {
drawValue(
context: context,
value: formatter.stringForValue(
val,
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
xPos: x,
yPos: val >= 0.0
? (rect.origin.y + posOffset)
: (rect.origin.y + rect.size.height + negOffset),
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(j))
}

if dataSet.isDrawIconsEnabled {
drawIcon(context: context,
icon: e.data as? NSUIImage,
xPos: x,
yPos: val >= 0.0
? (rect.origin.y + posOffset)
: (rect.origin.y + rect.size.height + negOffset),
offset: dataSet.iconsOffset)
}
}
}
else
Expand Down Expand Up @@ -465,19 +477,30 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
continue
}

drawValue(
context: context,
value: formatter.stringForValue(
e.y,
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
xPos: x,
yPos: rect.origin.y +
(e.y >= 0 ? posOffset : negOffset),
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(index))
if dataSet.isDrawValuesEnabled {
drawValue(
context: context,
value: formatter.stringForValue(
e.y,
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
xPos: x,
yPos: rect.origin.y +
(e.y >= 0 ? posOffset : negOffset),
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(index))
}

if dataSet.isDrawIconsEnabled {
drawIcon(context: context,
icon: e.data as? NSUIImage,
xPos: x,
yPos: rect.origin.y +
(e.y >= 0 ? posOffset : negOffset),
offset: dataSet.iconsOffset)
}
}
else
{
Expand Down Expand Up @@ -524,18 +547,28 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
continue
}

drawValue(
context: context,
value: formatter.stringForValue(
vals[k],
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
xPos: x,
yPos: y,
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(index))
if dataSet.isDrawValuesEnabled {
drawValue(
context: context,
value: formatter.stringForValue(
vals[k],
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
xPos: x,
yPos: y,
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(index))
}

if dataSet.isDrawIconsEnabled {
drawIcon(context: context,
icon: e.data as? NSUIImage,
xPos: x,
yPos: y,
offset: dataSet.iconsOffset)
}
}
}

Expand All @@ -552,6 +585,15 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
ChartUtils.drawText(context: context, text: value, point: CGPoint(x: xPos, y: yPos), align: align, attributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color])
}

/// Draws a value at the specified x and y position.
open func drawIcon(context: CGContext, icon: NSUIImage?, xPos: CGFloat, yPos: CGFloat, offset: CGSize)
{
let point = CGPoint(x: xPos, y: yPos)
if let iconImage = icon {
ChartUtils.drawImage(context: context, image: iconImage, point: point, expectedSize: iconImage.size, offset: offset)
}
}

open override func drawExtras(context: CGContext)
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ open class BarLineScatterCandleBubbleRenderer: DataRenderer
/// - returns: `true` if the DataSet values should be drawn, `false` if not.
internal func shouldDrawValues(forDataSet set: IChartDataSet) -> Bool
{
return set.isVisible && set.isDrawValuesEnabled
return set.isVisible && (set.isDrawValuesEnabled || set.isDrawIconsEnabled)
}

/// Class representing the bounds of the current viewport in terms of indices in the values array of a DataSet.
Expand Down
29 changes: 21 additions & 8 deletions Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,27 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
let valueFont = dataSet.valueFont
let lineHeight = valueFont.lineHeight

ChartUtils.drawText(
context: context,
text: text,
point: CGPoint(
x: pt.x,
y: pt.y - (0.5 * lineHeight)),
align: .center,
attributes: [NSFontAttributeName: valueFont, NSForegroundColorAttributeName: valueTextColor])
if dataSet.isDrawValuesEnabled {
ChartUtils.drawText(
context: context,
text: text,
point: CGPoint(
x: pt.x,
y: pt.y - (0.5 * lineHeight)),
align: .center,
attributes: [NSFontAttributeName: valueFont, NSForegroundColorAttributeName: valueTextColor])
}

if let icon = e.data as? NSUIImage, dataSet.isDrawIconsEnabled {
ChartUtils.drawImage(context: context,
image: icon,
point: CGPoint(
x: pt.x,
y: pt.y),
expectedSize: icon.size,
offset: dataSet.iconsOffset)
}

}
}
}
Expand Down
36 changes: 24 additions & 12 deletions Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,30 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer
continue
}

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: [NSFontAttributeName: valueFont, NSForegroundColorAttributeName: dataSet.valueTextColorAt(j)])
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: [NSFontAttributeName: valueFont, NSForegroundColorAttributeName: dataSet.valueTextColorAt(j)])
}

if let icon = e.data as? NSUIImage, dataSet.isDrawIconsEnabled {
ChartUtils.drawImage(context: context,
image: icon,
point: CGPoint(
x: pt.x,
y: pt.y),
expectedSize: icon.size,
offset: dataSet.iconsOffset)
}
}
}
}
Expand Down
84 changes: 58 additions & 26 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer
{
guard let dataSet = dataSets[dataSetIndex] as? IBarChartDataSet else { continue }

if !shouldDrawValues(forDataSet: dataSet)
if !shouldDrawValues(forDataSet: dataSet) || !(dataSet.isDrawIconsEnabled && dataSet.isVisible)
{
continue
}
Expand Down Expand Up @@ -383,15 +383,26 @@ open class HorizontalBarChartRenderer: BarChartRenderer
negOffset = -negOffset - valueTextWidth
}

drawValue(
context: context,
value: valueText,
xPos: (rect.origin.x + rect.size.width)
+ (val >= 0.0 ? posOffset : negOffset),
yPos: y + yOffset,
font: valueFont,
align: textAlign,
color: dataSet.valueTextColorAt(j))
if dataSet.isDrawValuesEnabled {
drawValue(
context: context,
value: valueText,
xPos: (rect.origin.x + rect.size.width)
+ (val >= 0.0 ? posOffset : negOffset),
yPos: y + yOffset,
font: valueFont,
align: textAlign,
color: dataSet.valueTextColorAt(j))
}

if dataSet.isDrawIconsEnabled {
drawIcon(context: context,
icon: e.data as? NSUIImage,
xPos: (rect.origin.x + rect.size.width)
+ (val >= 0.0 ? posOffset : negOffset),
yPos: y,
offset: dataSet.iconsOffset)
}
}
}
else
Expand Down Expand Up @@ -444,15 +455,26 @@ open class HorizontalBarChartRenderer: BarChartRenderer
negOffset = -negOffset - valueTextWidth
}

drawValue(
context: context,
value: valueText,
xPos: (rect.origin.x + rect.size.width)
+ (val >= 0.0 ? posOffset : negOffset),
yPos: rect.origin.y + yOffset,
font: valueFont,
align: textAlign,
color: dataSet.valueTextColorAt(index))
if dataSet.isDrawValuesEnabled {
drawValue(
context: context,
value: valueText,
xPos: (rect.origin.x + rect.size.width)
+ (val >= 0.0 ? posOffset : negOffset),
yPos: rect.origin.y + yOffset,
font: valueFont,
align: textAlign,
color: dataSet.valueTextColorAt(index))
}

if dataSet.isDrawIconsEnabled {
drawIcon(context: context,
icon: e.data as? NSUIImage,
xPos: (rect.origin.x + rect.size.width)
+ (val >= 0.0 ? posOffset : negOffset),
yPos: rect.origin.y,
offset: dataSet.iconsOffset)
}
}
else
{
Expand Down Expand Up @@ -521,13 +543,23 @@ open class HorizontalBarChartRenderer: BarChartRenderer
continue
}

drawValue(context: context,
value: valueText,
xPos: x,
yPos: y + yOffset,
font: valueFont,
align: textAlign,
color: dataSet.valueTextColorAt(index))
if dataSet.isDrawValuesEnabled {
drawValue(context: context,
value: valueText,
xPos: x,
yPos: y + yOffset,
font: valueFont,
align: textAlign,
color: dataSet.valueTextColorAt(index))
}

if dataSet.isDrawIconsEnabled {
drawIcon(context: context,
icon: e.data as? NSUIImage,
xPos: x,
yPos: y,
offset: dataSet.iconsOffset)
}
}
}

Expand Down
Loading

0 comments on commit 5161890

Please sign in to comment.