Skip to content

Commit

Permalink
Algorithm updates (ChartsOrg#3638)
Browse files Browse the repository at this point in the history
* algorithm updates

* Minor code org updates

* Relocated util file

* Removed duplicated file

akjflkajfljk
  • Loading branch information
jjatie authored and abhiramvadlapatla committed Dec 21, 2021
1 parent 1f9a7d5 commit a52e7a5
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 135 deletions.
4 changes: 4 additions & 0 deletions Charts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
146EE16342C2BADC92E45BF2 /* LineScatterCandleRadarChartDataSetProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9249AD9AEC8C85772365A128 /* LineScatterCandleRadarChartDataSetProtocol.swift */; };
17E994DA88777AA1D8CCFC58 /* BarChartDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = C31AA65EA27776F8C653C7E8 /* BarChartDataSet.swift */; };
219192CA6B4895319AB49DCA /* BarLineScatterCandleBubbleRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B1C588E9DF6FFD56D7ADF8E /* BarLineScatterCandleBubbleRenderer.swift */; };
221CA2922588FCBC00C2DD1E /* Sequence+KeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 221CA2912588FCBC00C2DD1E /* Sequence+KeyPath.swift */; };
2243BBFD1FF156EC00B49D0B /* EquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2243BBFB1FF156D000B49D0B /* EquatableTests.swift */; };
23649EFC635A76022F07FFA6 /* PieChartDataEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD02157CF8CEE1189BF681DA /* PieChartDataEntry.swift */; };
23FA50B2730D8C7ACA091C4F /* BarChartRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75F279974FE650E57A061B09 /* BarChartRenderer.swift */; };
Expand Down Expand Up @@ -195,6 +196,7 @@
1F3D55A7E6176D52DC957D27 /* XAxisRendererHorizontalBarChart.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = XAxisRendererHorizontalBarChart.swift; path = Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift; sourceTree = "<group>"; };
2194AA554712E6BA2677F114 /* BubbleChartRenderer.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = BubbleChartRenderer.swift; path = Source/Charts/Renderers/BubbleChartRenderer.swift; sourceTree = "<group>"; };
219BC9CEA037F897E92E45D1 /* ScatterChartDataSetProtocol.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ScatterChartDataSetProtocol.swift; path = Source/Charts/Data/Interfaces/ScatterChartDataSetProtocol.swift; sourceTree = "<group>"; };
221CA2912588FCBC00C2DD1E /* Sequence+KeyPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "Sequence+KeyPath.swift"; path = "Source/Charts/Utils/Sequence+KeyPath.swift"; sourceTree = "<group>"; };
2243BBFB1FF156D000B49D0B /* EquatableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EquatableTests.swift; path = Tests/Charts/EquatableTests.swift; sourceTree = "<group>"; };
23D35CF6F9177D77B6B97AE1 /* XShapeRenderer.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = XShapeRenderer.swift; path = Source/Charts/Renderers/Scatter/XShapeRenderer.swift; sourceTree = "<group>"; };
2440DB759AB93B4A928A3F6F /* RadarChartView.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = RadarChartView.swift; path = Source/Charts/Charts/RadarChartView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -647,6 +649,7 @@
C9F3DC252355F791000C3215 /* Platform+Gestures.swift */,
C9F3DC242355F791000C3215 /* Platform+Graphics.swift */,
C9F3DC282355FA2F000C3215 /* Platform+Touch Handling.swift */,
221CA2912588FCBC00C2DD1E /* Sequence+KeyPath.swift */,
FF475B9593B9898853814340 /* Transformer.swift */,
324C9127B53A8D39C8B49277 /* TransformerHorizontalBarChart.swift */,
72EAEBB7CF73E33565FC2896 /* ViewPortHandler.swift */,
Expand Down Expand Up @@ -911,6 +914,7 @@
B539114951455C35BADAE3F3 /* PieChartDataSet.swift in Sources */,
0C52C70C6E6EA09BD7426386 /* RadarChartData.swift in Sources */,
C2EFB4EC8C97FA9987F1B50D /* RadarChartDataEntry.swift in Sources */,
221CA2922588FCBC00C2DD1E /* Sequence+KeyPath.swift in Sources */,
E3B28EA1E21279DF3889BCE8 /* RadarChartDataSet.swift in Sources */,
9A26C8DB1F87B01700367599 /* DataApproximator+N.swift in Sources */,
2B791E64E7C4523B1A63F72A /* ScatterChartData.swift in Sources */,
Expand Down
9 changes: 3 additions & 6 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
// A port of MPAndroidChart for iOS
// Licensed under Apache License 2.0
// Licensed under Apache License 3.0
//
// https://github.com/danielgindi/Charts
//
Expand Down Expand Up @@ -62,12 +62,9 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
// calculate how many digits are needed
setupDefaultFormatter(min: data.yMin, max: data.yMax)

for set in data
for set in data where set.valueFormatter is DefaultValueFormatter
{
if set.valueFormatter is DefaultValueFormatter
{
set.valueFormatter = defaultValueFormatter
}
set.valueFormatter = defaultValueFormatter
}

// let the chart know there is new data
Expand Down
19 changes: 6 additions & 13 deletions Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,12 @@ open class AxisBase: ComponentBase

@objc open func getLongestLabel() -> String
{
var longest = ""

for i in entries.indices
{
let text = getFormattedLabel(i)

if longest.count < text.count
{
longest = text
}
}

return longest
let longest = entries.indices
.lazy
.map(getFormattedLabel(_:))
.max(by: \.count)

return longest ?? ""
}

/// - Returns: The formatted label at the specified index. This will either use the auto-formatter or the custom formatter (if one is set).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ open class BarChartData: BarLineScatterCandleBubbleChartData
{
fromX += diff
}

}

notifyDataChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ open class BarChartDataEntry: ChartDataEntry
return _positiveSum
}

var stackSize: Int { yValues?.count ?? 1}

@objc open func calcPosNegSum()
{
(_negativeSum, _positiveSum) = _yVals?.reduce(into: (0,0)) { (result, y) in
Expand Down Expand Up @@ -175,7 +177,7 @@ open class BarChartDataEntry: ChartDataEntry
get { return self._yVals }
set
{
self.y = BarChartDataEntry.calcSum(values: newValue)
self.y = BarChartDataEntry.calcSum(values: newValue ?? [])
self._yVals = newValue
calcPosNegSum()
calcRanges()
Expand Down Expand Up @@ -205,18 +207,8 @@ open class BarChartDataEntry: ChartDataEntry
/// - Parameters:
/// - vals:
/// - Returns:
private static func calcSum(values: [Double]?) -> Double
private static func calcSum(values: [Double]) -> Double
{
guard let values = values
else { return 0.0 }

var sum = 0.0

for f in values
{
sum += f
}

return sum
values.reduce(into: 0, +=)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,17 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData
/// stacks. All values belonging to a stack are calculated separately.
private func calcEntryCountIncludingStacks(entries: [BarChartDataEntry])
{
_entryCountStacks = 0

entries.forEach { _entryCountStacks += $0.yValues?.count ?? 1 }
_entryCountStacks = entries.lazy
.map(\.stackSize)
.reduce(into: 0, +=)
}

/// calculates the maximum stacksize that occurs in the Entries array of this DataSet
private func calcStackSize(entries: [BarChartDataEntry])
{
for e in entries where (e.yValues?.count ?? 0) > _stackSize
{
_stackSize = e.yValues!.count
}
_stackSize = entries.lazy
.map(\.stackSize)
.max() ?? 1
}

open override func calcMinMax(entry e: ChartDataEntry)
Expand Down
1 change: 1 addition & 0 deletions Source/Charts/Highlight/BarHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ open class BarHighlighter: ChartHighlighter
@objc open func getClosestStackIndex(ranges: [Range]?, value: Double) -> Int
{
guard let ranges = ranges else { return 0 }

if let stackIndex = ranges.firstIndex(where: { $0.contains(value) }) {
return stackIndex
} else {
Expand Down
18 changes: 5 additions & 13 deletions Source/Charts/Highlight/RadarHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,12 @@ open class RadarHighlighter: PieRadarHighlighter
let highlights = getHighlights(forIndex: index)

let distanceToCenter = Double(chart.distanceToCenter(x: x, y: y) / chart.factor)

var closest: Highlight?
var distance = Double.greatestFiniteMagnitude

for high in highlights
{
let cdistance = abs(high.y - distanceToCenter)
if cdistance < distance
{
closest = high
distance = cdistance
}

func closestToCenter(lhs: Highlight, rhs: Highlight) -> Bool {
abs(lhs.y - distanceToCenter) < abs(rhs.y - distanceToCenter)
}


let closest = highlights.min(by: closestToCenter(lhs:rhs:))
return closest
}

Expand Down
33 changes: 17 additions & 16 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,13 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
// Create and append the corresponding accessibility element to accessibilityOrderedElements
if let chart = dataProvider as? BarChartView
{
let element = createAccessibleElement(withIndex: j,
container: chart,
dataSet: dataSet,
dataSetIndex: index,
stackSize: stackSize)
{ (element) in
let element = createAccessibleElement(
withIndex: j,
container: chart,
dataSet: dataSet,
dataSetIndex: index,
stackSize: stackSize
) { (element) in
element.accessibilityFrame = barRect
}

Expand Down Expand Up @@ -536,8 +537,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
// if we have stacks

var bufferIndex = 0

for index in 0 ..< Int(ceil(Double(dataSet.entryCount) * animator.phaseX))
let lastIndex = ceil(Double(dataSet.entryCount) * animator.phaseX)

for index in 0 ..< Int(lastIndex)
{
guard let e = dataSet.entryForIndex(index) as? BarChartDataEntry else { continue }

Expand All @@ -548,19 +550,18 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
let x = rect.origin.x + rect.size.width / 2.0

// we still draw stacked bars, but there is one non-stacked in between
if let vals = vals
if let values = vals
{
// draw stack values
var transformed = [CGPoint]()

var posY = 0.0
var negY = -e.negativeSum

for k in vals.indices
for value in values
{
let value = vals[k]
var y: Double

let y: Double

if value == 0.0 && (posY == 0.0 || negY == 0.0)
{
// Take care of the situation of a 0.0 value, which overlaps a non-zero bar
Expand All @@ -582,9 +583,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

trans.pointValuesToPixel(&transformed)

for (val, transformed) in zip(vals, transformed)
for (value, transformed) in zip(values, transformed)
{
let drawBelow = (val == 0.0 && negY == 0.0 && posY > 0.0) || val < 0.0
let drawBelow = (value == 0.0 && negY == 0.0 && posY > 0.0) || value < 0.0
let y = transformed.y + (drawBelow ? negOffset : posOffset)

guard viewPortHandler.isInBoundsRight(x) else { break }
Expand All @@ -597,7 +598,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
drawValue(
context: context,
value: formatter.stringForValue(
val,
value,
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
Expand Down
3 changes: 2 additions & 1 deletion Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
accessibleChartElements.append(element)
}

for case let (i, set) as (Int, BubbleChartDataSetProtocol) in bubbleData.enumerated() where set.isVisible
let sets = bubbleData.dataSets as! [BubbleChartDataSet]
for case let (i, set) in zip(sets.indices, sets) where set.isVisible
{
drawDataSet(context: context, dataSet: set, dataSetIndex: i)
}
Expand Down
18 changes: 5 additions & 13 deletions Source/Charts/Renderers/CombinedChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,30 @@ open class CombinedChartRenderer: NSObject, DataRenderer
{
_renderers.append(BarChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break


case .line:
if chart.lineData !== nil
{
_renderers.append(LineChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break


case .candle:
if chart.candleData !== nil
{
_renderers.append(CandleStickChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break


case .scatter:
if chart.scatterData !== nil
{
_renderers.append(ScatterChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break


case .bubble:
if chart.bubbleData !== nil
{
_renderers.append(BubbleChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break
}
}

Expand Down Expand Up @@ -190,10 +185,7 @@ open class CombinedChartRenderer: NSObject, DataRenderer
/// e.g. if you provide [DrawOrder.Bar, DrawOrder.Line], the bars will be drawn behind the lines.
open var drawOrder: [CombinedChartView.DrawOrder]
{
get
{
return _drawOrder
}
get { _drawOrder }
set
{
if !newValue.isEmpty
Expand Down
Loading

0 comments on commit a52e7a5

Please sign in to comment.