how to replace the values of labels in iOS-charts?

限于喜欢 提交于 2019-12-06 16:12:01

问题


I use IOS-charts for drawing of downloading speed. I give to chart function array of double which has byte values. It build chart. But I want to show in chart labels value in megabytes, kilobytes or byte. I can transform byte in these value with help NSByteFormatter. But there is one problem, a function that builds charts takes a single array of values (such as an array of bytes), and outputs the same values in the labels on the chart, I tried to find a solution to do this, but I found none. How can i do this?

Function which draw a chart

   func setSendChart(description: [String], value: [Double]) {
    var chartDataEntryArray = [ChartDataEntry]()

    for item in 0..<description.count {
        let chartEntry = ChartDataEntry(value: value[item], xIndex: item)
        chartDataEntryArray.append(chartEntry)
    }

    let dataSet = LineChartDataSet(yVals: chartDataEntryArray, label: "")
    dataSet.drawCircleHoleEnabled = false
    dataSet.drawCirclesEnabled = false
    dataSet.drawCubicEnabled = true
    dataSet.drawFilledEnabled = true
    dataSet.drawValuesEnabled = false

    dataSet.fillColor = UIColor(red: 47/255, green: 206/255, blue: 255/255, alpha: 1.0)

    let chartData = LineChartData(xVals: description, dataSet: dataSet)
    sendChartView.data = chartData
}

回答1:


You can't change the value the chart is using to draw; however, you could provide your own NSNumberFormatter implementation to get the formatted value you want. yAxis has valueFormatter, so does dataSet

Another way is, you subclass dataSet to pass your value text array, and override renderAxisLabels or drawValues to draw the text you want.

I would prefer the first one since it does not touch the library code



来源:https://stackoverflow.com/questions/34885486/how-to-replace-the-values-of-labels-in-ios-charts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!