Skip to content

Commit

Permalink
Merge branch 'BarChart_CustomValueOffset' of github.com:bivant/Charts…
Browse files Browse the repository at this point in the history
… into BarChart_CustomValueOffset

# Conflicts:
#	Source/Charts/Renderers/BarChartRenderer.swift
  • Loading branch information
bivant committed Aug 14, 2024
2 parents d40009f + f68b6f0 commit ec08161
Show file tree
Hide file tree
Showing 57 changed files with 699 additions and 169 deletions.
15 changes: 0 additions & 15 deletions Charts.xcworkspace/xcshareddata/swiftpm/Package.resolved

This file was deleted.

49 changes: 49 additions & 0 deletions ChartsDemo-iOS/Objective-C/DemoBaseViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,55 @@ - (void)handleOption:(NSString *)key forChartView:(ChartViewBase *)chartView

[chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleValuesPosition"])
{
BarChartView *barChart = (BarChartView *)chartView;
barChart.drawValueAboveBarEnabled = !barChart.drawValueAboveBarEnabled;

[chartView notifyDataSetChanged];
}

if ([key isEqualToString:@"toggleValuesSideFlexible"])
{
BarChartView *barChart = (BarChartView *)chartView;
barChart.isDrawValueSideFlexible = !barChart.isDrawValueSideFlexible;

[chartView notifyDataSetChanged];
}

if ([key isEqualToString:@"toggleValueColorsAdjustment"])
{
for (id<BarChartDataSetProtocol, NSObject> set in chartView.data.dataSets)
{
if ([set conformsToProtocol:@protocol(BarChartDataSetProtocol)])
{
set.valueColorsAdjustment = !set.valueColorsAdjustment;
}
}

[chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleValueColorsSecondary"])
{
for (id<BarChartDataSetProtocol, NSObject> set in chartView.data.dataSets)
{
if ([set conformsToProtocol:@protocol(BarChartDataSetProtocol)])
{
if (set.valueColorsSecondary.count == 0)
{
set.valueTextColorSecondary = [UIColor blackColor];
}
else
{
[set resetValueSecondaryColors];
}
}
}

[chartView setNeedsDisplay];
}
}

#pragma mark - Actions
Expand Down
4 changes: 4 additions & 0 deletions ChartsDemo-iOS/Objective-C/Demos/BarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ - (void)viewDidLoad

self.options = @[
@{@"key": @"toggleValues", @"label": @"Toggle Values"},
@{@"key": @"toggleValuesPosition", @"label": @"Toggle Values Position"},
@{@"key": @"toggleValuesSideFlexible", @"label": @"Toggle Y-Values Flexible Side"},
@{@"key": @"toggleValueColorsAdjustment", @"label": @"Toggle Y-Values Color Adjustment"},
@{@"key": @"toggleValueColorsSecondary", @"label": @"Toggle Y-Values Secondary Color"},
@{@"key": @"toggleIcons", @"label": @"Toggle Icons"},
@{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
@{@"key": @"animateX", @"label": @"Animate X"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ - (void)viewDidLoad

self.options = @[
@{@"key": @"toggleValues", @"label": @"Toggle Values"},
@{@"key": @"toggleValuesPosition", @"label": @"Toggle Values Position"},
@{@"key": @"toggleValuesSideFlexible", @"label": @"Toggle Y-Values Flexible Side"},
@{@"key": @"toggleValueColorsAdjustment", @"label": @"Toggle Y-Values Color Adjustment"},
@{@"key": @"toggleValueColorsSecondary", @"label": @"Toggle Y-Values Secondary Color"},
@{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
@{@"key": @"animateX", @"label": @"Animate X"},
@{@"key": @"animateY", @"label": @"Animate Y"},
Expand Down
37 changes: 37 additions & 0 deletions ChartsDemo-iOS/Swift/DemoBaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ enum Option {
case toggleYLabels
case toggleRotate
case toggleHighlightCircle
// Bar Chart
case toggleValuesPosition
case toggleValuesSideFlexible
case toggleValueColorsAdjustment
case toggleValueColorsSecondary

var label: String {
switch self {
Expand Down Expand Up @@ -91,6 +96,11 @@ enum Option {
case .toggleYLabels: return "Toggle Y-Labels"
case .toggleRotate: return "Toggle Rotate"
case .toggleHighlightCircle: return "Toggle highlight circle"
//Bar Chart
case .toggleValuesPosition: return "Toggle Y-Values Position"
case .toggleValuesSideFlexible: return "Toggle Y-Values Flexible Side"
case .toggleValueColorsAdjustment: return "Toggle Y-Values Color Adjustment"
case .toggleValueColorsSecondary: return "Toggle Y-Values Secondary Color"
}
}
}
Expand Down Expand Up @@ -175,6 +185,33 @@ class DemoBaseViewController: UIViewController, ChartViewDelegate {
}
}
chartView.setNeedsDisplay()

case .toggleValuesPosition:
let barChart = chartView as! BarChartView
barChart.drawValueAboveBarEnabled = !barChart.drawValueAboveBarEnabled
chartView.notifyDataSetChanged()

case .toggleValuesSideFlexible:
let barChart = chartView as! BarChartView
barChart.isDrawValueSideFlexible = !barChart.isDrawValueSideFlexible
chartView.notifyDataSetChanged()

case .toggleValueColorsAdjustment:
for set in chartView.data!.dataSets {
set.valueColorsAdjustment = !set.valueColorsAdjustment
}
chartView.setNeedsDisplay()

case .toggleValueColorsSecondary:
for set in chartView.data!.dataSets {
if set.valueColorsSecondary.isEmpty {
set.valueTextColorSecondary = .black
} else {
set.resetValueSecondaryColors()
}
}
chartView.setNeedsDisplay()

default:
break
}
Expand Down
5 changes: 5 additions & 0 deletions ChartsDemo-iOS/Swift/Demos/BarChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class BarChartViewController: DemoBaseViewController {
self.title = "Bar Chart"

self.options = [.toggleValues,
.toggleValuesPosition,
.toggleValuesSideFlexible,
.toggleValueColorsAdjustment,
.toggleValueColorsSecondary,
.toggleHighlight,
.animateX,
.animateY,
Expand Down Expand Up @@ -134,6 +138,7 @@ class BarChartViewController: DemoBaseViewController {
set1.colors = ChartColorTemplates.material()
set1.drawValuesEnabled = false

set1.valueColorsAdjustment = chartView.valueColorsAdjustment
let data = BarChartData(dataSet: set1)
data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 10)!)
data.barWidth = 0.9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class PositiveNegativeBarChartViewController: DemoBaseViewController {
// Do any additional setup after loading the view.
self.title = "Positive/Negative Bar Chart"
self.options = [.toggleValues,
.toggleValuesPosition,
.toggleValuesSideFlexible,
.toggleValueColorsAdjustment,
.toggleValueColorsSecondary,
.toggleHighlight,
.animateX,
.animateY,
Expand Down Expand Up @@ -98,6 +102,7 @@ class PositiveNegativeBarChartViewController: DemoBaseViewController {
let set = BarChartDataSet(entries: yVals, label: "Values")
set.colors = colors
set.valueColors = colors
set.valueColorsAdjustment = chartView.valueColorsAdjustment

let data = BarChartData(dataSet: set)
data.setValueFont(.systemFont(ofSize: 13))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class StackedBarChartViewController: DemoBaseViewController {
// Do any additional setup after loading the view.
self.title = "Stacked Bar Chart"
self.options = [.toggleValues,
.toggleValuesPosition,
.toggleValuesSideFlexible,
.toggleValueColorsAdjustment,
.toggleValueColorsSecondary,
.toggleIcons,
.toggleHighlight,
.animateX,
Expand Down
28 changes: 21 additions & 7 deletions Source/Charts/Charts/BarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ open class BarChartView: BarLineChartViewBase, BarChartDataProvider
/// if set to true, all values are drawn above their bars, instead of below their top
private var _drawValueAboveBarEnabled = true

/// if set to true and _drawValueAboveBarEnabled is false, values those do not fit into the value bar are drawn above their bars, instead of below their top
private var _drawValueInsideBarSoft = false
/// if set to true values those do not fit into the value bar/placed outside of visible area are drawn above/inside their bars
/// side effect - vertical bars offset (top/bottom) are set to 0 to prevent chart placement adjustment for low/big values
private var _drawValueSideFlexible = false

/// Distance fo the values from the bars top
private var _valuesOffset: CGFloat = 4.5

private var _valueColorsAdjustment = false

/// if set to true, a grey area is drawn behind each bar that indicates the maximum value
private var _drawBarShadowEnabled = false

Expand Down Expand Up @@ -157,14 +161,14 @@ open class BarChartView: BarLineChartViewBase, BarChartDataProvider
}
}

@objc open var isDrawValueInsideBarSoft: Bool
@objc open var isDrawValueSideFlexible: Bool
{
get { return _drawValueInsideBarSoft }
get { return _drawValueSideFlexible }
set
{
_drawValueInsideBarSoft = newValue
if _drawValueInsideBarSoft {
//these offsets are not needed as the value labels do not "cross" zero edge
_drawValueSideFlexible = newValue
if _drawValueSideFlexible {
//these offsets are not needed as the value labels do not "cross" zero or chart min/max edges
//default values (0.1) provide gap if min/max value are close to the edges, no need the gap to fit in content
leftAxis.spaceTop = 0
leftAxis.spaceBottom = 0
Expand All @@ -185,6 +189,16 @@ open class BarChartView: BarLineChartViewBase, BarChartDataProvider
}
}

@objc open var valueColorsAdjustment: Bool
{
get { return _valueColorsAdjustment }
set
{
_valueColorsAdjustment = newValue
notifyDataSetChanged()
}
}

/// if set to true, a grey area is drawn behind each bar that indicates the maximum value
@objc open var drawBarShadowEnabled: Bool
{
Expand Down
6 changes: 3 additions & 3 deletions Source/Charts/Charts/CombinedChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider
set { (renderer as! CombinedChartRenderer).drawValueAboveBarEnabled = newValue }
}

@objc open var isDrawValueInsideBarSoft: Bool
@objc open var isDrawValueSideFlexible: Bool
{
get { return (renderer as! CombinedChartRenderer).isDrawValueInsideBarSoft }
set { (renderer as! CombinedChartRenderer).isDrawValueInsideBarSoft = newValue }
get { return (renderer as! CombinedChartRenderer).isDrawValueSideFlexible }
set { (renderer as! CombinedChartRenderer).isDrawValueSideFlexible = newValue }
}

@objc open var valuesOffset: CGFloat
Expand Down
41 changes: 41 additions & 0 deletions Source/Charts/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ open class ChartBaseDataSet: NSObject, ChartDataSetProtocol, NSCopying
/// List representing all colors that are used for drawing the actual values for this DataSet
open var valueColors = [NSUIColor]()

/// valueColors in case of soft positions in effect (BarCharView:isDrawValueSideFlexible)
open var valueColorsSecondary = [NSUIColor]()

/// Check if colors for the values for the DataSet are distinct from the background/bar color and apply inverted color if opposite
open var valueColorsAdjustment: Bool = false

/// The label string that describes the DataSet.
open var label: String? = "DataSet"

Expand Down Expand Up @@ -302,6 +308,39 @@ open class ChartBaseDataSet: NSObject, ChartDataSetProtocol, NSCopying
return valueColors[index % valueColors.count]
}

open var valueTextColorSecondary: NSUIColor
{
get
{
return valueTextColorSecondaryAt(0)
}
set
{
valueColorsSecondary.removeAll(keepingCapacity: false)
valueColorsSecondary.append(newValue)
}
}

open func resetValueSecondaryColors()
{
valueColorsSecondary.removeAll(keepingCapacity: false)
}

/// - Returns: The secondary color at the specified index that is used for drawing the values inside the chart. Uses modulus internally.
open func valueTextColorSecondaryAt(_ index: Int) -> NSUIColor
{
var index = index
if index < 0
{
index = 0
}
if valueColorsSecondary.isEmpty
{
return valueTextColorAt(index)
}
return valueColorsSecondary[index % valueColorsSecondary.count]
}

/// the font for the value-text labels
open var valueFont: NSUIFont = NSUIFont.systemFont(ofSize: 7.0)

Expand Down Expand Up @@ -393,6 +432,8 @@ open class ChartBaseDataSet: NSObject, ChartDataSetProtocol, NSCopying

copy.colors = colors
copy.valueColors = valueColors
copy.valueColorsAdjustment = valueColorsAdjustment
copy.valueColorsSecondary = valueColorsSecondary
copy.label = label
copy.axisDependency = axisDependency
copy.highlightEnabled = highlightEnabled
Expand Down
17 changes: 17 additions & 0 deletions Source/Charts/Data/Interfaces/ChartDataSetProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ public protocol ChartDataSetProtocol
/// List representing all colors that are used for drawing the actual values for this DataSet
var valueColors: [NSUIColor] { get }

/// valueColors in case of soft positions in effect (BarCharView:isDrawValueSideFlexible)
var valueColorsSecondary: [NSUIColor] { get }

/// Check if colors for the values for the DataSet are distinct from the background/bar color and apply inverted color if opposite
var valueColorsAdjustment: Bool { get set }

/// All the colors that are used for this DataSet.
/// Colors are reused as soon as the number of Entries the DataSet represents is higher than the size of the colors array.
var colors: [NSUIColor] { get }
Expand Down Expand Up @@ -205,6 +211,17 @@ public protocol ChartDataSetProtocol

/// - Returns: The color at the specified index that is used for drawing the values inside the chart. Uses modulus internally.
func valueTextColorAt(_ index: Int) -> NSUIColor

/// Sets/get a single secondary color for value text.
/// Setting the color clears the valueColorsSecondary array and adds a single color.
/// Getting will return the first color in the valueColorsSecondary array.
/// If the array is empty - return value of valueTextColor
var valueTextColorSecondary: NSUIColor { get set }

/// - Returns: The secondary color at the specified index that is used for drawing the values inside the chart. Uses modulus internally. If empty - return values from the valueTextColorAt
func valueTextColorSecondaryAt(_ index: Int) -> NSUIColor

func resetValueSecondaryColors()

/// the font for the value-text labels
var valueFont: NSUIFont { get set }
Expand Down
8 changes: 5 additions & 3 deletions Source/Charts/Interfaces/BarChartDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ public protocol BarChartDataProvider: BarLineScatterCandleBubbleChartDataProvide

var isDrawBarShadowEnabled: Bool { get }
var isDrawValueAboveBarEnabled: Bool { get }
/// if set to true and isDrawValueAboveBarEnabled is false, values those do not fit into the value bar are drawn above their bars, instead of below their top
var isDrawValueInsideBarSoft: Bool { get }
/// if set to true values those
/// 1.do not fit into the value bar are drawn above them (below for negative), instead of partially inside and/or below (above if negative)
/// 2.do not fit into the visible area above/below their bars are drawn inside
var isDrawValueSideFlexible: Bool { get }
/// distance from top (bottom in negative) for values drawn outside/inside the bar
var valuesOffset: CGFloat { get }
var isHighlightFullBarEnabled: Bool { get }
}
}
Loading

0 comments on commit ec08161

Please sign in to comment.