ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.59k stars 6k forks source link

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

Closed castillejoale closed 8 years ago

castillejoale commented 8 years ago

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.

yushan023 commented 8 years ago

image @protocol image
image

xVals is array.Save Axis value.

liuxuan30 commented 8 years ago

1474

Short answer: try ChartsDemo

castillejoale commented 8 years ago

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

3) 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 commented 8 years ago

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

liuxuan30 commented 8 years ago

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 commented 7 years ago

http://stackoverflow.com/questions/40470604/linechartdata-without-x-values-on-constructor-charts-swift-3 The answer to this question solved the issue for me