Y Axis value is repeat for large number in Bar Chart despite we are using granularityEnable and setting value as well. We have use below OIAnalysisLargeValueFormatter but it seems it is working but granularity is not taking correct value.
`final class OIAnalysisLargeValueFormatter: AxisValueFormatter {
var suffix = ["", "k", "L", "Cr"]
public init() { }
fileprivate func format(value: Double) -> String {
var sig = value
var length = 3
var fraction = 0
let numberFormatter = NumberFormatter()
numberFormatter.usesGroupingSeparator = true
numberFormatter.negativePrefix = "-"
if abs(sig) >= 10000000 {
sig = (Double(sig) / 100000000)
sig *= 10
length -= 0
numberFormatter.maximumFractionDigits = 1
} else if abs(sig) >= 100000 {
sig = (Double(sig) / 1000000).rounded()
sig *= 10
length -= 1
numberFormatter.maximumFractionDigits = 0
} else if abs(sig) >= 10000 {
sig = (Double(sig) / 10000).rounded() //' K'
length -= 2
} else {
length -= 3 // ""
}
let number = NSNumber(value: sig)
numberFormatter.maximumFractionDigits = fraction
numberFormatter.positiveSuffix = suffix[length]
numberFormatter.negativeSuffix = suffix[length]
guard let string = numberFormatter.string(from: number) else {
return NumberFormatter.localizedString(from: number, number: .decimal)
}
return string
}
func stringForValue(
_ value: Double, axis: AxisBase?) -> String {
return format(value: value)
}
func stringForValue(
_ value: Double,
entry: ChartDataEntry,
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String {
return format(value: value)
}
}`
2. In positive negative chart it is not showing Zero position as Axis in between.
'Data Building :- >
OiAnalysisChartDataBuilder
func processOIChartDataSet(strikes: [Strike], spotPrice: Double) -> ViewController.Content {
var dataEntries = [[BarChartDataEntry]]()
var callEntries = [BarChartDataEntry]()
var putEntries = [BarChartDataEntry]()
var xAxisValues = [String]()
static let defaultMargin: CGFloat = 16.0
static let dashLineWidth: CGFloat = 1.0
static let xOffsetLimitLine: CGFloat = 50.0
static let yOffsetLimitLine = 4.0
// Line Chart
static let chartViewHeight: CGFloat = 315.0
static let lineWidth: CGFloat = 2.0
static let rightLeftAxisCount: Int = 8
static let chartMaxRange: Int = 120
// Bar Chart
static let granularity = 50.0
static let leftAxisOICount: Int = 6
static let groupSpace = 0.4
static let barWidth = 0.2
static let barSpace = 0.03
static let xRangeMaximum = 5.0
static let yAxisDuration = 0.5
static let barDashPhase = 5.0
static let barDashLineWidth = 1.0
static var xAxisValueForPCRAndMAX: [String] = {
["9:15", "10:15", "11:15", "12:15", "01:15", "02:15", "03:15"]
}()
}
}
// MARK: - OiLineChartValuexAxisFormatter
private class OiLineChartValuexAxisFormatter: AxisValueFormatter {
We are facing mainly two issue
`final class OIAnalysisLargeValueFormatter: AxisValueFormatter {
}`
2. In positive negative chart it is not showing Zero position as Axis in between.
'Data Building :- > OiAnalysisChartDataBuilder func processOIChartDataSet(strikes: [Strike], spotPrice: Double) -> ViewController.Content { var dataEntries = [[BarChartDataEntry]]() var callEntries = [BarChartDataEntry]() var putEntries = [BarChartDataEntry]() var xAxisValues = [String]()
}'
CHART CODE:
'extension ViewController {
}
// MARK: - Constant Helper
private typealias ConstantHelper = ViewController private extension ConstantHelper {
enum Constants {
}
}
// MARK: - OiLineChartValuexAxisFormatter
private class OiLineChartValuexAxisFormatter: AxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String { let index = Int(value) < titleValues.count ? Int(value) : Int(value / 20) return titleValues[index] }
var titleValues = [String]()
init(values: [String]) { self.titleValues = values }
}
// MARK: - OiBarChartValueXAxisFormatter
private class OiBarChartValueXAxisFormatter: AxisValueFormatter {
var titleValues = [String]()
init(values: [String]) { self.titleValues = values }
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
}
}
// MARK: - ChartDataHelper Helper
private typealias ChartDataHelper = ViewController private extension ChartDataHelper {
func getBarChartDataSet(_ chartDataEntries: [BarChartDataEntry], color: UIColor, label: String, highlightEnabled: Bool) -> BarChartDataSet { let chartDataSet = BarChartDataSet(entries: chartDataEntries, label: label) chartDataSet.colors = [color] chartDataSet.drawIconsEnabled = false chartDataSet.drawValuesEnabled = false chartDataSet.highlightEnabled = highlightEnabled chartDataSet.highlightColor = .clear chartDataSet.highlightLineWidth = Constants.dashLineWidth chartDataSet.highlightLineDashPhase = Constants.barDashPhase chartDataSet.highlightLineDashLengths = [Constants.barDashPhase, Constants.barDashPhase] return chartDataSet }
func addSpotPriceLine(spotPrice: Double, values: [String]) { guard let value = values.first?.toDouble, let value2 = values[1].toDouble else { return } let limitPoint = ((spotPrice - value) / (value2 - value)) - 1.0 let limitLine = ChartLimitLine(limit: limitPoint, label: "Spot price: " + String(spotPrice)) limitLine.lineWidth = Constants.barDashLineWidth limitLine.lineColor = UIColor.gray limitLine.valueTextColor = UIColor.gray limitLine.lineDashLengths = [Constants.barDashPhase, Constants.barDashPhase] limitLine.labelPosition = .rightTop limitLine.xOffset = -Constants.xOffsetLimitLine limitLine.yOffset = -Constants.yOffsetLimitLine
}
}'
Charts Environment
Chart Version : 4.1.0
Xcode : 14.2
5.6
iPhone
Demo Project
https://github.com/lokesh-vyas/DemoChartBar
ℹ Please link to or upload a project we can download that reproduces the issue.