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

How can I call func stringForValue(_ value: Double, axis: AxisBase?) -> String #3696

Closed
sinqer opened this issue Oct 18, 2018 · 1 comment
Closed

Comments

@sinqer
Copy link

sinqer commented Oct 18, 2018

What did you do?

I want to call "func stringForValue(_ value: Double, axis: AxisBase?) -> String"

What did you expect to happen?

func stringForValue not work, what can I do

my code is

class ReportDataViewController: ParentViewController {

@IBOutlet weak var heartRateLabel: UILabel!
@IBOutlet weak var heartRateTittle: UILabel!
@IBOutlet weak var heartRateRed: UILabel!
@IBOutlet weak var heartMonth: UILabel!
@IBOutlet weak var chartView: LineChartView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    chartView.delegate = self
    heartRateTittle.layer.cornerRadius = 4
    heartRateTittle.layer.borderColor = rgb(136, G: 136, B: 136).cgColor
    heartRateTittle.layer.borderWidth = 1
    
    
    
    HeartLineChart()
    
    
}

func HeartLineChart() {
    
    var dataEntries = [ChartDataEntry]()
    var entries2 = [ChartDataEntry]()
    var redentries = [ChartDataEntry]()
    var yellowentries = [ChartDataEntry]()
    var greenentries = [ChartDataEntry]()
    
    
    
    let ChartData = Realm.getAll(type: MUploadReport.self)
    let calender = Calendar.current
    let lastDay = calender.date(bySettingHour: 0, minute: 0, second: 0, of: Date())
    var firstDayMinusOneDay = calender.date(byAdding: .day, value: -7, to: lastDay!)
    firstDayMinusOneDay = calender.date(bySettingHour: 23, minute: 59, second: 59, of: firstDayMinusOneDay!)
    
    let lastDayAddOneDay = calender.date(byAdding: Calendar.Component.day, value: 1, to: lastDay!)
    
    for result in ChartData {
        
        if result.measureDate.isGreaterThanDate(firstDayMinusOneDay!)
            && result.measureDate.isLessThanDate(lastDayAddOneDay!)
            
        {
            
            var datestring: String?
            
            let measureDate = calender.date(bySettingHour: 0, minute: 0, second: 0, of: result.measureDate)
            
            let dif = calender.dateComponents([.day], from: lastDay! , to: measureDate!)
            
            let heartrate:Int = Int(result.HeartRate)
            
            datestring = SKFunc.dateToString(measureDate!, Formatter: "dd")
            
            let datefloat = (datestring! as NSString).doubleValue
            
            if heartrate > 100 && heartrate < 40 {
                
                let entry = ChartDataEntry.init(x: Double(datefloat), y: Double(heartrate))
                
                dataEntries.append(entry)
                
                let entry2 = ChartDataEntry.init(x: Double(datefloat), y: Double(heartrate))
                
                redentries.append(entry2)
                
               
                
                
            } else {
                
                let entry = ChartDataEntry.init(x: Double(datefloat), y: Double(heartrate))
                
                dataEntries.append(entry)
                }
        }
        
    }
    
    
    
    
    let chartDataSet = LineChartDataSet(values: dataEntries, label: "")
    chartDataSet.colors = [UIColor.clear]
  
    
    //目前折线图只包括1根折线
    let chartData = LineChartData(dataSets: [chartDataSet])
    chartView.data = chartData
    
    
    
    
    
    let xAxis = chartView.xAxis
    xAxis.labelPosition = .bottom
    xAxis.labelFont = .systemFont(ofSize: 12)
    xAxis.axisLineWidth = 0
    xAxis.gridLineWidth = 0
    //chartView.xAxis.labelRotationAngle = 50 //刻度文字倾斜角度
    xAxis.axisMaximum = 7.5
    xAxis.axisMinimum = 0.5
    
    
    
    
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        
        var dayArray: [String] = []
        
        var today3 = Date()
        let dayint3:TimeInterval = 24*60*60
        today3 -= dayint3*7
        
        for _ in 0...6 {
            
            today3 += dayint3
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd"
            let stringTime = dateFormatter.string(from: today3)
            dayArray.append(stringTime)
            
        }
        
        let array3 = "Today"
        dayArray.append(array3)

        
        return dayArray[Int(value) % dayArray.count]
    }
    
}

}

Charts version/Branch/Commit Number:
**Xcode version:9.4
**Swift version:4.2
**Platform(s) running Charts:3.1.1
macOS version running Xcode:

Demo Project

ℹ Please link to or upload a project we can download that reproduces the issue.

@pmairoldi
Copy link
Collaborator

You need to create a class that implements the IAxisValueFormatter protocol and then set it to the axis's valueFormatter property.

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

2 participants