Chart fixes

This commit is contained in:
Ilya Laktyushin
2020-03-25 20:44:20 +04:00
parent 79024f66f1
commit e854d7b4fa
27 changed files with 143 additions and 61 deletions

View File

@@ -137,7 +137,7 @@ public class BaseChartController: ChartThemeContainer {
}
public var isChartRangePagingEnabled: Bool = false
public var minimumSelectedChartRange: CGFloat = 0.05
public var minimumSelectedChartRange: CGFloat = 0.085
public var chartRangePagingClosure: ((Bool, CGFloat) -> Void)? // isEnabled, PageSize
public func setChartRangePagingEnabled(isEnabled: Bool, minimumSelectionSize: CGFloat) {
isChartRangePagingEnabled = isEnabled
@@ -178,7 +178,7 @@ public class BaseChartController: ChartThemeContainer {
public var setBackButtonVisibilityClosure: ((Bool, Bool) -> Void)?
public var refreshChartToolsClosure: ((Bool) -> Void)?
public func didTapZoomIn(date: Date) {
public func didTapZoomIn(date: Date, pointIndex: Int) {
fatalError("Abstract")
}

View File

@@ -24,6 +24,7 @@ class GeneralChartComponentController: ChartThemeContainer {
var lastChartInteractionPoint: CGPoint = .zero
var isChartInteractionBegun: Bool = false
var isChartInteracting: Bool = false
var ignoreInteraction: Bool = false
let isZoomed: Bool
var isZoomable = true
@@ -169,6 +170,14 @@ class GeneralChartComponentController: ChartThemeContainer {
}
func chartInteractionDidBegin(point: CGPoint) {
guard !ignoreInteraction else {
return
}
if !isChartInteractionBegun && detailsVisible {
self.hideDetailsView(animated: true)
ignoreInteraction = true
return
}
let chartFrame = self.chartFrame()
guard chartFrame.width > 0 else { return }
let horizontalRange = currentHorizontalMainChartRange
@@ -185,19 +194,24 @@ class GeneralChartComponentController: ChartThemeContainer {
showDetailsView(at: chartValue, detailsViewPosition: detailsViewPosition, dataIndex: minIndex, date: closestDate, animted: chartWasInteracting)
}
var detailsVisible = false
func showDetailsView(at chartPosition: CGFloat, detailsViewPosition: CGFloat, dataIndex: Int, date: Date, animted: Bool) {
setDetailsViewModel?(chartDetailsViewModel(closestDate: date, pointIndex: dataIndex), animted)
setDetailsChartVisibleClosure?(true, true)
setDetailsViewPositionClosure?(detailsViewPosition)
detailsVisible = true
}
func chartInteractionDidEnd() {
isChartInteractionBegun = false
isChartInteracting = false
ignoreInteraction = false
}
func hideDetailsView(animated: Bool) {
isChartInteractionBegun = false
setDetailsChartVisibleClosure?(false, animated)
detailsVisible = false
}
var visibleDetailsChartValues: [ChartsCollection.Chart] {
@@ -323,13 +337,13 @@ class GeneralChartComponentController: ChartThemeContainer {
let viewModel = ChartDetailsViewModel(title: dateString,
showArrow: self.isZoomable && !self.isZoomed,
showPrefixes: false,
isLoading: false,
values: values,
totalValue: nil,
tapAction: { [weak self] in
self?.zoomInOnDateClosure?(closestDate) },
hideAction: { [weak self] in
self?.setDetailsChartVisibleClosure?(false, true)
})
return viewModel
}
@@ -338,11 +352,11 @@ class GeneralChartComponentController: ChartThemeContainer {
let fromDate = Date(timeIntervalSince1970: TimeInterval(currentHorizontalMainChartRange.lowerBound) + 1)
let toDate = Date(timeIntervalSince1970: TimeInterval(currentHorizontalMainChartRange.upperBound))
if Calendar.utc.startOfDay(for: fromDate) == Calendar.utc.startOfDay(for: toDate) {
let stirng = BaseConstants.headerFullZoomedFormatter.string(from: fromDate)
self.setChartTitleClosure?(stirng, animated)
let string = BaseConstants.headerFullZoomedFormatter.string(from: fromDate)
self.setChartTitleClosure?(string, animated)
} else {
let stirng = "\(BaseConstants.headerMediumRangeFormatter.string(from: fromDate)) - \(BaseConstants.headerMediumRangeFormatter.string(from: toDate))"
self.setChartTitleClosure?(stirng, animated)
let string = "\(BaseConstants.headerMediumRangeFormatter.string(from: fromDate)) - \(BaseConstants.headerMediumRangeFormatter.string(from: toDate))"
self.setChartTitleClosure?(string, animated)
}
}
}

View File

@@ -18,6 +18,7 @@ public class BaseLinesChartController: BaseChartController {
var zoomChartVisibility: [Bool]
var lastChartInteractionPoint: CGPoint = .zero
var isChartInteractionBegun: Bool = false
var ignoreInteraction: Bool = false
var initialChartRange: ClosedRange<CGFloat> = BaseConstants.defaultRange
var zoomedChartRange: ClosedRange<CGFloat> = BaseConstants.defaultRange
@@ -71,7 +72,8 @@ public class BaseLinesChartController: BaseChartController {
}
public override func chartInteractionDidEnd() {
isChartInteractionBegun = false
ignoreInteraction = false
}
public override func cancelChartInteraction() {
@@ -98,7 +100,7 @@ public class BaseLinesChartController: BaseChartController {
}
func chartDetailsViewModel(closestDate: Date, pointIndex: Int) -> ChartDetailsViewModel {
func chartDetailsViewModel(closestDate: Date, pointIndex: Int, loading: Bool) -> ChartDetailsViewModel {
let values: [ChartDetailsViewModel.Value] = actualChartsCollection.chartValues.enumerated().map { arg in
let (index, component) = arg
return ChartDetailsViewModel.Value(prefix: nil,
@@ -118,20 +120,25 @@ public class BaseLinesChartController: BaseChartController {
let viewModel = ChartDetailsViewModel(title: dateString,
showArrow: total > 0 && self.isZoomable && !self.isZoomed,
showPrefixes: false,
isLoading: loading,
values: values,
totalValue: nil,
tapAction: { [weak self] in self?.didTapZoomIn(date: closestDate) },
hideAction: { [weak self] in
self?.setDetailsChartVisibleClosure?(false, true)
tapAction: { [weak self] in
self?.didTapZoomIn(date: closestDate, pointIndex: pointIndex)
}, hideAction: { [weak self] in
self?.cancelChartInteraction()
})
return viewModel
}
public override func didTapZoomIn(date: Date) {
public override func didTapZoomIn(date: Date, pointIndex: Int) {
guard isZoomed == false else { return }
cancelChartInteraction()
setDetailsViewModel?(chartDetailsViewModel(closestDate: date, pointIndex: pointIndex, loading: true), false)
self.getDetailsData?(date, { updatedCollection in
if let updatedCollection = updatedCollection {
self.cancelChartInteraction()
self.initialChartRange = self.currentHorizontalRange
if let startDate = updatedCollection.axisValues.first,
let endDate = updatedCollection.axisValues.last {

View File

@@ -122,14 +122,22 @@ public class GeneralLinesChartController: BaseLinesChartController {
}
public override func chartInteractionDidBegin(point: CGPoint) {
guard !ignoreInteraction else {
return
}
if !isChartInteractionBegun && !self.verticalLineRenderer.values.isEmpty {
self.cancelChartInteraction()
ignoreInteraction = true
return
}
let horizontalRange = mainLinesRenderer.horizontalRange.current
let chartFrame = self.chartFrame()
guard chartFrame.width > 0 else { return }
let chartInteractionWasBegin = isChartInteractionBegun
let dateToFind = Date(timeIntervalSince1970: TimeInterval(horizontalRange.distance * point.x + horizontalRange.lowerBound))
guard let (closestDate, minIndex) = findClosestDateTo(dateToFind: dateToFind) else { return }
let chartInteractionWasBegin = isChartInteractionBegun
super.chartInteractionDidBegin(point: point)
self.lineBulletsRenderer.bullets = chartLines.compactMap { chart in
@@ -139,7 +147,7 @@ public class GeneralLinesChartController: BaseLinesChartController {
let chartValue: CGFloat = CGFloat(closestDate.timeIntervalSince1970)
let detailsViewPosition = (chartValue - horizontalRange.lowerBound) / horizontalRange.distance * chartFrame.width + chartFrame.minX
self.setDetailsViewModel?(chartDetailsViewModel(closestDate: closestDate, pointIndex: minIndex), chartInteractionWasBegin)
self.setDetailsViewModel?(chartDetailsViewModel(closestDate: closestDate, pointIndex: minIndex, loading: false), chartInteractionWasBegin)
self.setDetailsChartVisibleClosure?(true, true)
self.setDetailsViewPositionClosure?(detailsViewPosition)
self.verticalLineRenderer.values = [chartValue]

View File

@@ -149,6 +149,14 @@ public class TwoAxisLinesChartController: BaseLinesChartController {
}
public override func chartInteractionDidBegin(point: CGPoint) {
guard !ignoreInteraction else {
return
}
if !isChartInteractionBegun && !self.verticalLineRenderer.values.isEmpty {
self.cancelChartInteraction()
ignoreInteraction = true
return
}
let horizontalRange = currentHorizontalRange
let chartFrame = self.chartFrame()
guard chartFrame.width > 0 else { return }
@@ -168,7 +176,7 @@ public class TwoAxisLinesChartController: BaseLinesChartController {
let chartValue: CGFloat = CGFloat(closestDate.timeIntervalSince1970)
let detailsViewPosition = (chartValue - horizontalRange.lowerBound) / horizontalRange.distance * chartFrame.width + chartFrame.minX
self.setDetailsViewModel?(chartDetailsViewModel(closestDate: closestDate, pointIndex: minIndex), chartInteractionWasBegin)
self.setDetailsViewModel?(chartDetailsViewModel(closestDate: closestDate, pointIndex: minIndex, loading: false), chartInteractionWasBegin)
self.setDetailsChartVisibleClosure?(true, true)
self.setDetailsViewPositionClosure?(detailsViewPosition)
self.verticalLineRenderer.values = [chartValue]

View File

@@ -153,6 +153,7 @@ class PercentChartComponentController: GeneralChartComponentController {
let viewModel = ChartDetailsViewModel(title: dateString,
showArrow: total > 0 && self.isZoomable && !self.isZoomed,
showPrefixes: true,
isLoading: false,
values: values,
totalValue: nil,
tapAction: { [weak self] in

View File

@@ -48,7 +48,7 @@ public class PercentPieChartController: BaseChartController {
controller.chartFrame = { [unowned self] in self.chartFrame() }
controller.cartViewBounds = { [unowned self] in self.cartViewBounds() }
controller.zoomInOnDateClosure = { [unowned self] date in
self.didTapZoomIn(date: date)
self.didTapZoomIn(date: date, pointIndex: 0)
}
controller.setChartTitleClosure = { [unowned self] (title, animated) in
self.setChartTitleClosure?(title, animated)
@@ -272,7 +272,7 @@ public class PercentPieChartController: BaseChartController {
switchToChart(chartsCollection: newCollection, isZoomed: true, animated: true)
}
public override func didTapZoomIn(date: Date) {
public override func didTapZoomIn(date: Date, pointIndex: Int) {
self.didTapZoomIn(date: date, animated: true)
}

View File

@@ -150,6 +150,7 @@ class PieChartComponentController: GeneralChartComponentController {
let viewModel = ChartDetailsViewModel(title: "",
showArrow: false,
showPrefixes: false,
isLoading: false,
values: [ChartDetailsViewModel.Value(prefix: nil,
title: title,
value: valueString,

View File

@@ -40,7 +40,7 @@ public class DailyBarsChartController: BaseChartController {
controller.chartFrame = { [unowned self] in self.chartFrame() }
controller.cartViewBounds = { [unowned self] in self.cartViewBounds() }
controller.zoomInOnDateClosure = { [unowned self] date in
self.didTapZoomIn(date: date)
self.didTapZoomIn(date: date, pointIndex: 0)
}
controller.setChartTitleClosure = { [unowned self] (title, animated) in
self.setChartTitleClosure?(title, animated)
@@ -205,7 +205,7 @@ public class DailyBarsChartController: BaseChartController {
}
}
public override func didTapZoomIn(date: Date) {
public override func didTapZoomIn(date: Date, pointIndex: Int) {
guard isZoomed == false else { return }
if isZoomed {
return linesController.hideDetailsView(animated: true)

View File

@@ -43,7 +43,7 @@ public class StackedBarsChartController: BaseChartController {
controller.chartFrame = { [unowned self] in self.chartFrame() }
controller.cartViewBounds = { [unowned self] in self.cartViewBounds() }
controller.zoomInOnDateClosure = { [unowned self] date in
self.didTapZoomIn(date: date)
self.didTapZoomIn(date: date, pointIndex: 0)
}
controller.setChartTitleClosure = { [unowned self] (title, animated) in
self.setChartTitleClosure?(title, animated)
@@ -220,7 +220,7 @@ public class StackedBarsChartController: BaseChartController {
}
}
public override func didTapZoomIn(date: Date) {
public override func didTapZoomIn(date: Date, pointIndex: Int) {
guard isZoomed == false else { return }
if isZoomed {
return zoomedBarsController.hideDetailsView(animated: true)

View File

@@ -43,7 +43,7 @@ public class StepBarsChartController: BaseChartController {
controller.chartFrame = { [unowned self] in self.chartFrame() }
controller.cartViewBounds = { [unowned self] in self.cartViewBounds() }
controller.zoomInOnDateClosure = { [unowned self] date in
self.didTapZoomIn(date: date)
self.didTapZoomIn(date: date, pointIndex: 0)
}
controller.setChartTitleClosure = { [unowned self] (title, animated) in
self.setChartTitleClosure?("", animated)
@@ -228,7 +228,7 @@ public class StepBarsChartController: BaseChartController {
}
}
public override func didTapZoomIn(date: Date) {
public override func didTapZoomIn(date: Date, pointIndex: Int) {
guard isZoomed == false else { return }
if isZoomed {
return zoomedBarsController.hideDetailsView(animated: true)

View File

@@ -152,6 +152,15 @@ public class TwoAxisStepBarsChartController: BaseLinesChartController {
}
public override func chartInteractionDidBegin(point: CGPoint) {
guard !ignoreInteraction else {
return
}
if !isChartInteractionBegun && !self.verticalLineRenderer.values.isEmpty {
self.cancelChartInteraction()
ignoreInteraction = true
return
}
let horizontalRange = currentHorizontalRange
let chartFrame = self.chartFrame()
guard chartFrame.width > 0 else { return }
@@ -183,8 +192,8 @@ public class TwoAxisStepBarsChartController: BaseLinesChartController {
}
let chartValue: CGFloat = CGFloat(closestDate.timeIntervalSince1970)
let detailsViewPosition = (chartValue - horizontalRange.lowerBound) / horizontalRange.distance * chartFrame.width + chartFrame.minX
self.setDetailsViewModel?(chartDetailsViewModel(closestDate: closestDate, pointIndex: minIndex), chartInteractionWasBegin)
let detailsViewPosition = (chartValue - horizontalRange.lowerBound) / horizontalRange.distance * chartFrame.width + chartFrame.minX + barOffset
self.setDetailsViewModel?(chartDetailsViewModel(closestDate: closestDate, pointIndex: minIndex, loading: false), chartInteractionWasBegin)
self.setDetailsChartVisibleClosure?(true, true)
self.setDetailsViewPositionClosure?(detailsViewPosition)
self.verticalLineRenderer.values = [chartValue]

View File

@@ -39,6 +39,9 @@ class PieChartRenderer: BaseChartRenderer {
}
private(set) var selectedSegment: Int?
func selectSegmentAt(at indexToSelect: Int?, animated: Bool) {
guard selectedSegment != indexToSelect else {
return
}
selectedSegment = indexToSelect
for (index, animator) in setlectedSegmentsAnimators.enumerated() {
let fraction: CGFloat = (index == indexToSelect) ? 1.0 : 0.0