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 to create y-axis for empty label ? #3735

Closed
c1800054work opened this issue Nov 7, 2018 · 4 comments
Closed

How to create y-axis for empty label ? #3735

c1800054work opened this issue Nov 7, 2018 · 4 comments

Comments

@c1800054work
Copy link

c1800054work commented Nov 7, 2018

Following is using “Charts” for swift 4.0 to show line chart. But the last label will not show normally , and i want to add empty label x-axis to fix this issue.

https://i.stack.imgur.com/hq9sh.jpg

I try to add " " to chartView.xAxis.valueFormatter, but it doesn't add an x-axis.

following is my code:

`x1 = [10.0, 10.0, 11.0, 11.0, 12.0, 12.0, 13.0, 13.0, 14.0, 14.0, 15.0, 15.0, 16.0, 16.0]
x2 = [20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0, 23.5, 24.0, 24.5, 25.0, 25.5, 26.0, 26.0]
times = ["8:00"," ","9:00"," ","10:00"," ","11:00"," ","12:00"," ","13:00"," ","14:00"," "]

setChart()

func setChart(){

let lineDefault = UIColor(red: 140.0/255.0, green: 234.0/255.0, blue: 255.0/255.0, alpha: 1.0)

let data = LineChartData()
var lineChartEntry1 = [ChartDataEntry]()

for i in 0..<x1.count {
    lineChartEntry1.append(ChartDataEntry(x: Double(i), y: Double(x1[i])))
}
let line1 = LineChartDataSet(values: lineChartEntry1, label: "a")
line1.drawCirclesEnabled = false
line1.drawValuesEnabled = false
line1.lineWidth = 2
line1.setColor(lineDefault)
data.addDataSet(line1)
if (x2.count > 0) {
    var lineChartEntry2 = [ChartDataEntry]()
    for i in 0..<x2.count {
        lineChartEntry2.append(ChartDataEntry(x: Double(i), y: Double(x2[i])))
    }
    let line2 = LineChartDataSet(values: lineChartEntry2, label: "b")
    line2.drawCirclesEnabled = false
    line2.drawValuesEnabled = false
    line2.lineWidth = 2
    line2.setColor(UIColor.yellow)
    data.addDataSet(line2)
}

let chartFormatter = LineChartFormatter(labels: times)
let xAxis = XAxis()
xAxis.valueFormatter = chartFormatter
mapLineChartView.xAxis.valueFormatter = xAxis.valueFormatter
mapLineChartView.data = data

}

private class LineChartFormatter: NSObject, IAxisValueFormatter {

var labels: [String] = []

func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    return labels[Int(value)]
}

init(labels: [String]) {
    super.init()
    self.labels = labels
}

}`

**Xcode version:10.0
**Swift version:4.0

@c1800054work
Copy link
Author

the solution is same with #2608

@c1800054work
Copy link
Author

c1800054work commented Nov 7, 2018

Hi

it will draw the line if there has data and the title is nil.
But there is no y-axis to the line.

the chartview like same with this stackoverflow
https://stackoverflow.com/questions/53187049/how-to-create-empty-label-by-using-charts

Does it possible to show y-axis line?

@c1800054work c1800054work reopened this Nov 7, 2018
@c1800054work c1800054work changed the title How to create empty label by using “Charts”? How to create y-axis for empty label ? Nov 7, 2018
@liuxuan30
Copy link
Member

if you want extra axis labels, meaning you are not satisfied what the auto calculation gives, you can use

    @objc open func setLabelCount(_ count: Int, force: Bool)
    {
        self.labelCount = count
        forceLabelsEnabled = force
    }

to set a proper label count. The formula of interval is (max - min) / count

@jayrawat
Copy link

@liuxuan30 can you give any example how you are setting label count by interval as you have mentioned. I want to not use auto calculation & rather set label count based on min/max data

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

3 participants