- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我添加了一个 bar chart到 Storyboard,但我无法为我的数据条目正确设置标签。
这是我的代码:
var names = ["aaa", "bbb", "ccc", "ddd"]
var values = [230.0, 280.0, 450.0, 340.0]
setChart(dataPoints: names, values: values)
设置图表函数:
func setChart(dataPoints: [String], values: [Double])
{
let formatter = BarChartFormatter()
formatter.setValues(values: dataPoints)
let xaxis:XAxis = XAxis()
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "موجودی")
let chartData = BarChartData(dataSet: chartDataSet)
xaxis.valueFormatter = formatter
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.chartDescription?.enabled = false
barChartView.legend.enabled = true
barChartView.rightAxis.enabled = false
barChartView.data = chartData
}
最后是格式化程序:
@objc(BarChartFormatter)
public class BarChartFormatter: NSObject, IAxisValueFormatter
{
var names = [String]()
public func stringForValue(_ value: Double, axis: AxisBase?) -> String
{
return names[Int(value)]
}
public func setValues(values: [String])
{
self.names = values
}
}
但效果不佳,如下所示:
如此处所示,它添加了 6 个标签而不是 4 个标签,并且它也有重复项。
我已经阅读了 this solution ,但是,如您所见,它还存在一些问题。
我该如何解决这个问题?
最佳答案
我觉得你可以尝试设置以下属性:
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1.0 //default granularity is 1.0, but it is better to be explicit
barChartView.xAxis.decimals = 0
源代码告诉你很多关于上面的属性。 https://github.com/danielgindi/Charts/blob/master/Source/Charts/Components/AxisBase.swift
关于ios - 如何在 ios-charts 中为 BarChartView 添加 XAxis 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960302/
我正在使用ios-charts Daniel Gindi 的库尝试创建条形图。 我以编程方式创建一个具有红色背景的 BarChartView,并用硬编码数据填充它。 View 的大小正确,但条形图无法
我有一个 BarChartView,它有 31 个条,因此有 31 个标签。我添加了所有内容并根据示例进行了设置,它按预期工作 - 除了 BarChartView 仅显示每个第三个标签。所以我尝试打印
我正在尝试增加 ChartBarView 中的条形宽度。这是我的代码: let groupSpace = 0.01 let barSpace = 0.01 let barWidth = 0.32 //
我正在尝试在我的应用程序中使用图表框架。我已经使用迦太基为此构建了二进制文件并将其添加到我的应用程序中,其中包含两个文件: - Charts-Swift.h - Charts.h 然后我将 Vie
我添加了一个 bar chart到 Storyboard,但我无法为我的数据条目正确设置标签。 这是我的代码: var names = ["aaa", "bbb", "ccc", "ddd"] var
我是一名优秀的程序员,十分优秀!