import UIKit //import RxCocoa //import RxSwift import SciChart import SnapKit final class NativeChartView: UIStackView { let priceSurface: SCIChartSurface let priceSeries = SCIFastCandlestickRenderableSeries() var priceSurfaceHelper: SCICallbackHelperProtocol? let volumeSurface: SCIChartSurface let positiveVolumeSeries = SCIFastColumnRenderableSeries() let negativeVolumeSeries = SCIFastColumnRenderableSeries() // fileprivate let newRange = BehaviorRelay(value: nil) override init(frame: CGRect) { priceSurface = SCIChartSurface(frame: frame.with { $0.size.height *= 0.8 }) volumeSurface = SCIChartSurface(frame: frame.with { $0.size.height *= 0.2 }) super.init(frame: frame) initialize() } required init(coder: NSCoder) { priceSurface = SCIChartSurface(frame: .zero) volumeSurface = SCIChartSurface(frame: .zero) super.init(coder: coder) initialize() } private func initialize() { axis = .vertical addArrangedSubview(priceSurface) addArrangedSubview(volumeSurface) priceSurface.snp.makeConstraints { [weak self] make in guard let volumeSurface = self?.volumeSurface else { return } make.height.equalTo(volumeSurface).multipliedBy(5) } for surface in [priceSurface, volumeSurface] { SCIThemeManager.applyTheme(toThemeable: surface, withThemeKey: "SciChart_Bright_Spark") let (xAxis, yAxis) = (SCIDateTimeAxis(), SCINumericAxis()) xAxis.growBy = SCIDoubleRange(min: SCIGeneric(0), max: SCIGeneric(0.1)) xAxis.autoRange = .once xAxis.maxAutoTicks = 10 xAxis.textFormatting = " MM-dd HH:mm " xAxis.minimalZoomConstrain = SCIGeneric(3600 * 10) xAxis.maximalZoomConstrain = SCIGeneric(3600 * 100) surface.xAxes.add(xAxis) yAxis.growBy = SCIDoubleRange(min: SCIGeneric(0), max: SCIGeneric(0.1)) yAxis.autoRange = .always yAxis.maxAutoTicks = 5 yAxis.textFormatting = "0.00000000" yAxis.style.recommendedSize = 50 surface.yAxes.add(yAxis) } priceSurfaceHelper = priceSurface.xAxes.defaultAxis()?.registerVisibleRangeChangedCallback { [weak self] newRange, _, _, _ in self?.newRange.accept(newRange) // self?.volumeSurface.xAxes.defaultAxis()?.visibleRange = newRange } setPriceSurface() setVolumeSurface() customColor() } private func setPriceSurface() { priceSurface.chartModifiers = .init(childModifiers: [SCIPinchZoomModifier(), SCIZoomPanModifier()]) priceSurface.xAxes.defaultAxis()?.style.recommendedSize = 10 priceSeries.dataSeries = SCIOhlcDataSeries(xType: .dateTime, yType: .double) priceSurface.renderableSeries.add(priceSeries) } private func setVolumeSurface() { volumeSurface.xAxes.defaultAxis().style.recommendedSize = 1 positiveVolumeSeries.dataSeries = SCIXyDataSeries(xType: .dateTime, yType: .double) volumeSurface.renderableSeries.add(positiveVolumeSeries) negativeVolumeSeries.dataSeries = SCIXyDataSeries(xType: .dateTime, yType: .double) volumeSurface.renderableSeries.add(negativeVolumeSeries) } private func customColor() { for surface in [priceSurface, volumeSurface] { surface.backgroundColor = .white for axis in [surface.xAxes.defaultAxis(), surface.yAxes.defaultAxis()] { axis?.style.labelStyle.color = .black axis?.style.gridBandBrush.color = .white axis?.style.majorGridLineBrush.color = #colorLiteral(red: 0.8823529412, green: 0.9254901961, blue: 0.9490196078, alpha: 1) //225 236 242 axis?.style.drawMajorTicks = false axis?.style.drawMinorGridLines = false axis?.style.drawMinorTicks = false } } priceSeries.strokeUpStyle.color = .red priceSeries.fillUpBrushStyle.color = .red priceSeries.strokeDownStyle.color = .blue priceSeries.fillDownBrushStyle.color = .blue positiveVolumeSeries.strokeStyle.color = .red positiveVolumeSeries.fillBrushStyle.color = .red negativeVolumeSeries.strokeStyle.color = .blue negativeVolumeSeries.fillBrushStyle.color = .blue } }