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 display the x Axis values on a LineChartView? #1625

Closed
castillejoale opened this issue Oct 8, 2016 · 6 comments
Closed

How can I display the x Axis values on a LineChartView? #1625

castillejoale opened this issue Oct 8, 2016 · 6 comments

Comments

@castillejoale
Copy link

castillejoale commented Oct 8, 2016

Hello, I am able to displays the yAxis values but I haven't figured out how to show the xAxis values.

Here is a pictures of my graph:

screen shot 2016-10-08 at 3 11 08 am

Here is my code so far, as you can see I have been trying everything:

The chart is a LineChartView


        // X
        let xs = Array(1..<20).map { return Double($0) }
        let data = LineChartData(xVals: xs)


        // Y1
        let ys1 = xs.map { i in return sin(Double(i / 2.0 / 3.141 * 1.5)) }
        let yse1 = ys1.enumerate().map { idx, i in return ChartDataEntry(value: i, xIndex: idx) }
        let ds1 = LineChartDataSet(yVals: yse1, label: "Hello")
        ds1.drawCirclesEnabled = false;
        ds1.colors = [UIColor.redColor()]
        data.addDataSet(ds1)

        // Y2
        let ys2 = xs.map { i in return cos(Double(i / 2.0 / 3.141)) }
        let yse2 = ys2.enumerate().map { idx, i in return ChartDataEntry(value: i, xIndex: idx) }
        let ds2 = LineChartDataSet(yVals: yse2, label: "World")
        ds2.drawCirclesEnabled = false;
        ds2.colors = [UIColor.blueColor()]
        data.addDataSet(ds2)

        // Y3
        let ys3 = xs.map { i in return sin(Double(i / 2.0 / 3.141 * 0.5)) }
        let yse3 = ys3.enumerate().map { idx, i in return ChartDataEntry(value: i, xIndex: idx) }
        let ds3 = LineChartDataSet(yVals: yse3, label: "World")
        ds3.drawCirclesEnabled = false;
        ds3.colors = [UIColor.greenColor()]
        data.addDataSet(ds3)

        //Chart
        data.setDrawValues(false)
        self.degreesLineChart.data = data
        self.degreesLineChart.gridBackgroundColor = NSUIColor.whiteColor()
        self.degreesLineChart.leftAxis.enabled = false
        self.degreesLineChart.descriptionText = ""

        self.degreesLineChart.xAxis.enabled = true
        self.degreesLineChart.xAxis.labelPosition = .BothSided
        self.degreesLineChart.xAxis.drawGridLinesEnabled = true
        self.degreesLineChart.xAxis.labelFont = UIFont.init(name: "HelveticaNeue-UltraLight", size: 19.0)!
        self.degreesLineChart.xAxis.labelTextColor = UIColor.greenColor()
        self.degreesLineChart.xAxis.labelHeight = 50.0
        self.degreesLineChart.xAxis.labelWidth = 200.0
        self.degreesLineChart.xAxis.drawLabelsEnabled = true


        self.degreesLineChart.animate(xAxisDuration: 0.0, yAxisDuration: 1.0)

Thank you so much

P.S. I am using tag v2.2.5, swift 2.3 in xcode 8.

@castillejoale castillejoale changed the title How can I display the x Axis values? How can I display the x Axis values on a LineChartView? Oct 8, 2016
@yushan023
Copy link

yushan023 commented Oct 9, 2016

image @protocol
image
image

xVals is array.Save Axis value.

@liuxuan30
Copy link
Member

liuxuan30 commented Oct 10, 2016

#1474
Short answer: try ChartsDemo

@castillejoale
Copy link
Author

Hello,

I wasn't able to make it work. This is what I did

  1. I have implemented this protocol: ChartXAxisValueFormatter

  2. Assigned the delegate:

self.degreesLineChart.xAxis.valueFormatter = self

  1. Implemented the protocol method: (It never gets called)
    func stringForXValue(index: Int, original: String, viewPortHandler: ChartViewPortHandler) -> String {

        return "Hello"

    }

I also tried to add the values directly to the axis like this, but it doesn't work either:

self.degreesLineChart.xAxis.values = ["0.0", "0.2"]

Any other tips?

@castillejoale
Copy link
Author

@liuxuan30 The first thing I did was to try ChartsDemo, but it seems like the xAxis is being added automatically.

@liuxuan30
Copy link
Member

liuxuan30 commented Oct 11, 2016

If you really tried ChartsDemo and played with it, you will find @interface DateValueFormatter did the job (conform to IAxisValueFormatter), and I don't know why you write stringForXValue while it's stringForValue

and then xAxis.valueFormatter = [[DateValueFormatter alloc] init];

I don't know where is ChartXAxisValueFormatter neither

@MrDee93
Copy link

MrDee93 commented Dec 28, 2016

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