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

2019-04-16 19:47发布

问题:

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