

And the problem is?..
@danielgindi X -axis , incorrectly displayed for time sometimes
yyyy/MM
1 2 2 2
Newbie to programming...would you be able to post sample code to implement your time axis?
Class IndexChartController : IAxisValueFormatter {
override func viewDidLoad() {
super.viewDidLoad()
combinedChartView....
combinedChartView.xAxis.valueFormatter = self
}
}
ValueFormatter :
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return xVals[Int(value) % xVals.count]
}
func setTrendLineSource(_ trendLines:[IndexTrendLineModel]) {
self.trendLineSource = trendLines
self.xVals.removeAll()
self.combChartView.data = nil
DispatchQueue.global().async(execute: {
var yVals = [ChartDataEntry]()
var yValsBar = [BarChartDataEntry]()
var i = 0
for tModel in trendLines {
if let d = tModel.data , d.count > 0 {
let lineEntry = ChartDataEntry(x: Double(i), y: d[0])
yVals.append(lineEntry)
let barEntry = BarChartDataEntry(x: Double(i), y: d[1] / 1000000.00)
yValsBar.append(barEntry)
}
if let date = tModel.dt {
self.xVals.append(String().dealTimeString("yyyyMMdd", outFormat: "yyyy/MM", timeString: date))
}
i += 1
}
let yAxis = self.combChartView.leftAxis
yAxis.labelCount = 4
yAxis.drawAxisLineEnabled = false
yAxis.gridLineDashLengths = [1.0,1.0]
let xAxis = self.combChartView.xAxis
xAxis.drawLabelsEnabled = true
xAxis.gridLineDashLengths = [1.0,1.0]
xAxis.avoidFirstLastClippingEnabled = true
xAxis.labelCount = 4
let lineChartDataSet = LineChartDataSet(values: yVals, label: "")
lineChartDataSet.lineWidth = 1
lineChartDataSet.colors = [UIColor.red]
lineChartDataSet.drawCircleHoleEnabled = false
lineChartDataSet.drawValuesEnabled = false
lineChartDataSet.drawCirclesEnabled = false
lineChartDataSet.drawFilledEnabled = false
lineChartDataSet.highlightColor = UIColor.darkGray
lineChartDataSet.highlightLineWidth = 0.5
lineChartDataSet.drawFilledEnabled = true
lineChartDataSet.fillColor = UIColor.red
lineChartDataSet.fillAlpha = 0.2
DispatchQueue.main.async(execute: {
var dataSets = [IChartDataSet]()
dataSets.append(lineChartDataSet)
let lineData = LineChartData.init(dataSets: dataSets)
let data:CombinedChartData = CombinedChartData()
data.lineData = lineData
self.combChartView.data = data
let barSet = BarChartDataSet.init(values: yValsBar, label: "")
barSet.drawValuesEnabled = false
barSet.colors = [UIColor(red: 122/255, green: 180/255, blue: 238/255, alpha: 1)]
barSet.highlightLineWidth = 1
barSet.highlightColor = UIColor.darkGray
var barSets = [IChartDataSet]()
barSets.append(barSet)
let barChartData = BarChartData( dataSets: barSets)
self.barChartView.data = barChartData
})
})
}
@ksaroka this is sample code
Thanks a bunch for posting...I'm trying to implement a string x axis from a realm database...
first the formatter is invoked at the beginning with a variable called "date":
@objc(ChartFormatter)
public class ChartFormatter: NSObject, IAxisValueFormatter{
var date: [String]!
public func stringForValue(_ value: Double, axis: AxisBase?) -> String{
return date[Int(value)]
}
}
next in viewDidLoad the results of a Realm database are imported:
let placement=realm.objects(Entry.self).value(forKey: "date") as! [String]
let q1=realm.objects(Entry.self).value(forKey: "placement") as! [Double]
self.setChart1(dataPoints: q1, chartInfo: chartInfo, chartDescription: chartDescription)
finally in setChart modified the code to adapt to Charts 3
func setChart1(dataPoints: [Double], chartInfo: String, chartDescription: String) {
let formatter:ChartFormatter = ChartFormatter()
let xaxis:XAxis = XAxis()
var dataEntries: [ChartDataEntry] = Array()
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: dataPoints[i])
dataEntries.append(dataEntry)
formatter.stringForValue(Double(i), axis: xaxis)
}
xaxis.valueFormatter=formatter
barChart1.xAxis.valueFormatter=xaxis.valueFormatter
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "")
let lineChartData = LineChartData(dataSet: lineChartDataSet)
Only problem is that it doesn't graph! I get an error "Thread 1: EXC_BAD_INSTRUCTION in the formatter:
return date[Int(value)]
If i just set the date variable in formatter with actual values it graphs OK, but it seems that I'm missing something integral to getting the data from the database into that variable.
Nevermind! Figured it out. For anybody using Realm Databases I implemented the following code in the formatter....probably fundamental error on scopes on my part.
To implement:
@objc(ChartFormatter)
public class ChartFormatter: NSObject, IAxisValueFormatter {
var placement: [String]!
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let realm=try! Realm()
let placement=realm.objects(Entry.self).value(forKey: "placement") as! [String]
print(placement)
return date[Int(value)]
}}
You solved the problem yet ? @ksaroka
Yes, the problem has been solved. Posted code above, thanks for your help 👍
@MonkeyRing can @ksaroka's code solve yours?
@liuxuan30 Prior to this, I changed another way to solve. His program has not been tested. But thx your reply .
I'd say if possible please give your own solution here and close the issue, thanks :)
@liuxuan30
In the controller, there are two different charts is a time-chart.
A chart is the area, I just set up a CombinedChartView, and then set by setting different data sources to display different chart styles.
However I found to Timeline sometimes not displayed correctly, i guess perhaps the process of switching the data source data garbled mess, my solution is to separate them to set up. Put two CombinedChartView, carry out their duties. So my problem is solved.
Most helpful comment
@ksaroka this is sample code