Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X -axis - time , sometimes incorrectly displayed ( many of the data points ) #1528

Closed
monkeyRing opened this issue Sep 22, 2016 · 12 comments
Closed

Comments

@monkeyRing
Copy link

what's the problem ?

snip20160922_2
snip20160922_1

@danielgindi
Copy link
Collaborator

And the problem is?..

@monkeyRing
Copy link
Author

@danielgindi X -axis , incorrectly displayed for time sometimes
yyyy/MM
1 2 2 2

@ksaroka
Copy link

ksaroka commented Sep 22, 2016

Newbie to programming...would you be able to post sample code to implement your time axis?

@monkeyRing
Copy link
Author

monkeyRing commented Sep 23, 2016

   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

@ksaroka
Copy link

ksaroka commented Sep 23, 2016

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.

@ksaroka
Copy link

ksaroka commented Sep 23, 2016

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)]
        }}

@liuxuan30 liuxuan30 reopened this Sep 26, 2016
@monkeyRing
Copy link
Author

You solved the problem yet ? @ksaroka

@ksaroka
Copy link

ksaroka commented Sep 26, 2016

Yes, the problem has been solved. Posted code above, thanks for your help 👍

@liuxuan30
Copy link
Member

@monkeyRing can @ksaroka's code solve yours?

@monkeyRing
Copy link
Author

@liuxuan30 Prior to this, I changed another way to solve. His program has not been tested. But thx your reply .

@liuxuan30
Copy link
Member

I'd say if possible please give your own solution here and close the issue, thanks :)

@monkeyRing
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants