Skip to content

Commit

Permalink
Upgrade library from Charts to DGCharts latest version (#975)
Browse files Browse the repository at this point in the history
* Upgrade to charts 5.0.0

* Rename lib

* Update imports

* Limit max scale
  • Loading branch information
hungnb94 authored Oct 28, 2023
1 parent 18bc536 commit 2cc913d
Show file tree
Hide file tree
Showing 39 changed files with 183 additions and 164 deletions.
28 changes: 14 additions & 14 deletions ios/ReactNativeCharts/BalloonMarker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import Foundation;

import Charts;
import DGCharts;

import SwiftyJSON;

Expand All @@ -25,10 +25,10 @@ open class BalloonMarker: MarkerView {
open var textColor: UIColor?
open var minimumSize = CGSize()


fileprivate var insets = UIEdgeInsets(top: 8.0,left: 8.0,bottom: 20.0,right: 8.0)
fileprivate var topInsets = UIEdgeInsets(top: 20.0,left: 8.0,bottom: 8.0,right: 8.0)

fileprivate var labelns: NSString?
fileprivate var _labelSize: CGSize = CGSize()
fileprivate var _size: CGSize = CGSize()
Expand Down Expand Up @@ -59,9 +59,9 @@ open class BalloonMarker: MarkerView {


var rect = CGRect(origin: point, size: _size)

if point.y - _size.height < 0 {

if point.x - _size.width / 2.0 < 0 {
drawTopLeftRect(context: context, rect: rect)
} else if (chart != nil && point.x + width - _size.width / 2.0 > (chart?.bounds.width)!) {
Expand All @@ -71,14 +71,14 @@ open class BalloonMarker: MarkerView {
rect.origin.x -= _size.width / 2.0
drawTopCenterRect(context: context, rect: rect)
}

rect.origin.y += self.topInsets.top
rect.size.height -= self.topInsets.top + self.topInsets.bottom

} else {

rect.origin.y -= _size.height

if point.x - _size.width / 2.0 < 0 {
drawLeftRect(context: context, rect: rect)
} else if (chart != nil && point.x + width - _size.width / 2.0 > (chart?.bounds.width)!) {
Expand All @@ -88,12 +88,12 @@ open class BalloonMarker: MarkerView {
rect.origin.x -= _size.width / 2.0
drawCenterRect(context: context, rect: rect)
}

rect.origin.y += self.insets.top
rect.size.height -= self.insets.top + self.insets.bottom

}

return rect
}

Expand Down Expand Up @@ -138,9 +138,9 @@ open class BalloonMarker: MarkerView {
context.fillPath()

}

func drawTopCenterRect(context: CGContext, rect: CGRect) {

context.setFillColor((color?.cgColor)!)
context.beginPath()
context.move(to: CGPoint(x: rect.origin.x + rect.size.width / 2.0, y: rect.origin.y))
Expand All @@ -152,7 +152,7 @@ open class BalloonMarker: MarkerView {
context.addLine(to: CGPoint(x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0, y: rect.origin.y + arrowSize.height))
context.addLine(to: CGPoint(x: rect.origin.x + rect.size.width / 2.0, y: rect.origin.y))
context.fillPath()

}

func drawTopLeftRect(context: CGContext, rect: CGRect) {
Expand Down Expand Up @@ -215,7 +215,7 @@ open class BalloonMarker: MarkerView {
if let object = entry.data as? JSON {
if object["marker"].exists() {
label = object["marker"].stringValue;

if highlight.stackIndex != -1 && object["marker"].array != nil {
label = object["marker"].arrayValue[highlight.stackIndex].stringValue
}
Expand Down
32 changes: 16 additions & 16 deletions ios/ReactNativeCharts/ChartGroupHolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import Foundation

import Charts
import DGCharts

class ChartHolder {
open weak var chart:BarLineChartViewBase?
open var syncX: Bool;
open var syncY:Bool;

init( chart: BarLineChartViewBase, syncX: Bool, syncY: Bool) {
self.chart = chart
self.syncX = syncX
Expand All @@ -23,45 +23,45 @@ class ChartHolder {

open class ChartGroupHolder {
static var chartGroup: Dictionary<String, Dictionary<String, ChartHolder>> = [:]

public static func addChart(group: String, identifier: String , chart: BarLineChartViewBase, syncX: Bool, syncY: Bool) {
objc_sync_enter(chartGroup)
defer { objc_sync_exit(chartGroup) }

let keyExists = chartGroup[group] != nil

if !keyExists {
chartGroup[group] = [:]
}

chartGroup[group]![identifier] = ChartHolder(chart: chart, syncX: syncX, syncY: syncY)
}


// sync gesture to other chart in the same group
public static func sync( group: String, identifier: String, scaleX: CGFloat, scaleY:CGFloat, centerX: CGFloat, centerY:CGFloat, performImmediately: Bool) {

objc_sync_enter(chartGroup)
defer { objc_sync_exit(chartGroup) }

if let identifierMap = chartGroup[group] {
for (identifierKey, chartHolder) in identifierMap {
if identifierKey != identifier {
if let chart = chartHolder.chart {

let axis = chart.getAxis(YAxis.AxisDependency.left).enabled ? YAxis.AxisDependency.left : YAxis.AxisDependency.right

let contentRect = chart.contentRect

let originalCenterValue = chart.valueForTouchPoint(point: CGPoint(x: contentRect.midX, y: contentRect.midY), axis: axis)

let finalScaleX = chartHolder.syncX ? scaleX : chart.scaleX
let finalScaleY = chartHolder.syncY ? scaleY : chart.scaleY

let finalCenterX = chartHolder.syncX ? centerX : originalCenterValue.x;
let finalCenterY = chartHolder.syncY ? centerY : originalCenterValue.y;
chart.zoom(scaleX: finalScaleX, scaleY: finalScaleY, xValue: Double(finalCenterX), yValue: Double(finalCenterY), axis: axis);

chart.zoom(scaleX: finalScaleX, scaleY: finalScaleY, xValue: Double(finalCenterX), yValue: Double(finalCenterY), axis: axis);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ios/ReactNativeCharts/CircleMarker.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import Charts
import DGCharts

open class CircleMarker: MarkerImage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import Charts
import DGCharts

open class ConfigurableMinimumLinePositionFillFormatter: NSObject, FillFormatter {

Expand Down
2 changes: 1 addition & 1 deletion ios/ReactNativeCharts/CustomChartDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
import Charts
import DGCharts

open class CustomChartDateFormatter: NSObject, ValueFormatter, AxisValueFormatter {

Expand Down
2 changes: 1 addition & 1 deletion ios/ReactNativeCharts/DataExtract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//

import Foundation
import Charts
import DGCharts
import SwiftyJSON

open class DataExtract {
Expand Down
2 changes: 1 addition & 1 deletion ios/ReactNativeCharts/IndexValueFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

import Charts
import DGCharts

open class IndexValueFormatter: NSObject, ValueFormatter {

Expand Down
14 changes: 7 additions & 7 deletions ios/ReactNativeCharts/RNBarLineChartBaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//

import Foundation
import Charts
import DGCharts

protocol RNBarLineChartBaseManager {
var _bridge : RCTBridge? {get}
Expand All @@ -17,42 +17,42 @@ extension RNBarLineChartBaseManager {
(view.chart as! BarLineChartViewBase).moveViewToX(xValue.doubleValue);
}
}

func _moveViewTo(_ reactTag: NSNumber, xValue: NSNumber, yValue: NSNumber, axisDependency: NSString) {
_bridge?.uiManager.addUIBlock { (uiManager: RCTUIManager?, viewRegistry:[NSNumber : UIView]?) in
let view: RNBarLineChartViewBase = viewRegistry![reactTag] as! RNBarLineChartViewBase;
(view.chart as! BarLineChartViewBase).moveViewTo(xValue: xValue.doubleValue, yValue: yValue.doubleValue, axis: BridgeUtils.parseAxisDependency(axisDependency as String));
}
}

func _moveViewToAnimated(_ reactTag: NSNumber, xValue: NSNumber, yValue: NSNumber, axisDependency: NSString, duration: NSNumber) {
_bridge?.uiManager.addUIBlock { (uiManager: RCTUIManager?, viewRegistry:[NSNumber : UIView]?) in
let view: RNBarLineChartViewBase = viewRegistry![reactTag] as! RNBarLineChartViewBase;
(view.chart as! BarLineChartViewBase).moveViewToAnimated(xValue: xValue.doubleValue, yValue: yValue.doubleValue, axis: BridgeUtils.parseAxisDependency(axisDependency as String), duration: duration.doubleValue / 1000.0);
}
}

func _centerViewTo(_ reactTag: NSNumber, xValue: NSNumber, yValue: NSNumber, axisDependency: NSString) {
_bridge?.uiManager.addUIBlock { (uiManager: RCTUIManager?, viewRegistry:[NSNumber : UIView]?) in
let view: RNBarLineChartViewBase = viewRegistry![reactTag] as! RNBarLineChartViewBase;
(view.chart as! BarLineChartViewBase).centerViewTo(xValue: xValue.doubleValue, yValue: yValue.doubleValue, axis: BridgeUtils.parseAxisDependency(axisDependency as String));
}
}

func _centerViewToAnimated(_ reactTag: NSNumber, xValue: NSNumber, yValue: NSNumber, axisDependency: NSString, duration: NSNumber) {
_bridge?.uiManager.addUIBlock { (uiManager: RCTUIManager?, viewRegistry:[NSNumber : UIView]?) in
let view: RNBarLineChartViewBase = viewRegistry![reactTag] as! RNBarLineChartViewBase;
(view.chart as! BarLineChartViewBase).centerViewToAnimated(xValue: xValue.doubleValue, yValue: yValue.doubleValue, axis: BridgeUtils.parseAxisDependency(axisDependency as String), duration: duration.doubleValue / 1000.0);
}
}

func _fitScreen(_ reactTag: NSNumber) {
_bridge?.uiManager.addUIBlock { (uiManager: RCTUIManager?, viewRegistry:[NSNumber : UIView]?) in
let view: RNBarLineChartViewBase = viewRegistry![reactTag] as! RNBarLineChartViewBase;
(view.chart as! BarLineChartViewBase).fitScreen();
}
}

func _highlights(_ reactTag: NSNumber, config: NSArray) {
_bridge?.uiManager.addUIBlock { (uiManager: RCTUIManager?, viewRegistry:[NSNumber : UIView]?) in
let view: RNBarLineChartViewBase = viewRegistry![reactTag] as! RNBarLineChartViewBase;
Expand Down
1 change: 1 addition & 0 deletions ios/ReactNativeCharts/RNBarLineChartManagerBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ RCT_EXPORT_VIEW_PROPERTY(borderColor, NSInteger) \
RCT_EXPORT_VIEW_PROPERTY(borderWidth, CGFloat) \
RCT_EXPORT_VIEW_PROPERTY(maxVisibleValueCount, NSInteger) \
RCT_EXPORT_VIEW_PROPERTY(visibleRange, NSDictionary) \
RCT_EXPORT_VIEW_PROPERTY(maxScale, NSDictionary) \
RCT_EXPORT_VIEW_PROPERTY(autoScaleMinMaxEnabled, BOOL) \
RCT_EXPORT_VIEW_PROPERTY(keepPositionOnRotation, BOOL) \
RCT_EXPORT_VIEW_PROPERTY(scaleEnabled, BOOL) \
Expand Down
Loading

0 comments on commit 2cc913d

Please sign in to comment.