如何替换iOS图表中的标签值? [英] how to replace the values of labels in iOS-charts?

查看:210
本文介绍了如何替换iOS图表中的标签值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用IOS图表绘制下载速度。我给图表函数数组double,它有字节值。它构建图表。但我想在图表标签中显示兆字节,千字节或字节的值。我可以通过帮助 NSByteFormatter 在这些值中转换字节。但是有一个问题,构建图表的函数需要一个值的数组(例如字节数组),并在图表上的标签中输出相同的值,我试图找到一个解决方案来做到这一点,但我发现无。

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?

绘制图表的函数

   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
}


推荐答案

您不能更改图表正在使用的值来绘制;但是,您可以提供自己的 NSNumberFormatter 实现,以获取所需的格式化值。 yAxis 有valueFormatter,因此 dataSet

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

是,你的子类dataSet传递你的值文本数组,并重写 renderAxisLabels drawValues 来绘制你想要的文本。

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

这篇关于如何替换iOS图表中的标签值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆