如何为ios-chart上的每一列添加名称?

时间:2022-01-05 07:23:37

I am doing a BarChart with ios-chart library. As it is similar to MPAndroidChart I do not have a lot of problems creating them but I am not able to add a name/label above each column with the days of the week.

我正在用ios-chart库做一个条形图。由于它与MPAndroidChart相似,我创建它们并没有很多问题,但是我不能在每个列上添加一个名称/标签。

On Android, after setting the values to the respective BarDataSet I used:

在Android上,将值设置为我使用的相应的BarDataSet后:

BarData data = new BarData(labels, dataset); 

where labels are the name for each column and dataset is the BarDataSet.

标签是每个列的名称,数据集是BarDataSet。

The problem is that on Swift I cannot find that way to add the labels for each column. This is the code that I have by the moment (a simple example):

问题是,在Swift上,我无法找到为每个列添加标签的方法。这是我现在的代码(一个简单的例子):

@IBOutlet weak var barChartView: BarChartView!

var entries = [ChartDataEntry]()
entries.append(BarChartDataEntry(x: 1.0, yValues: [1.0], label: "Monday"))
entries.append(BarChartDataEntry(x: 2.0, yValues: [4.0], label: "Tuesday"))

let charDataSet = BarChartDataSet(values: entries, label: "legend")
let charData = BarChartData(dataSets: [charDataSet])
barChartView.data = charData

And here is the result:

结果是:

如何为ios-chart上的每一列添加名称?

but I am not able to put Monday and Tuesday values where 1.04, 1.12, 1.20... values appear. Further, I am not able to add the labels on BarChartData function (as on Android).

但是我不能把星期一和星期二的值设为1。04,1。12,1。20…值出现。此外,我不能在BarChartData函数(如Android)上添加标签。

Where should I add Monday and Tuesday values to be shown above of each column?

我应该在哪里添加周一和周二的值,以显示在每个列的上面?

Thanks in advance!

提前谢谢!

2 个解决方案

#1


3  

If you look at the answer found at the following link, you will see that someone has gotten the new Swift 2.3 version of Charts to permit the use of string labels on the x-axis:

如果您查看以下链接中的答案,您将看到有人获得了新的Swift 2.3版本的图表,允许在x轴上使用字符串标签:

https://*.com/a/39149070/6889264

https://*.com/a/39149070/6889264

You indicate that you are working with Swift 3.0, but the revised BarChartData() function syntax that created this issue looks to be the same between 2.3 and 3.0, so it seems likely the same solution applies.

您表示正在使用Swift 3.0,但是修改后的BarChartData()函数语法在2.3和3.0之间看起来是相同的,因此似乎应用了相同的解决方案。

EDIT

编辑

Just tested, that answer can be implemented in Swift 3.

经过测试,这个答案可以在Swift 3中实现。

#2


0  

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
let daysValues = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0]

setChart(days, values: daysValues)

var dataEntries: [BarChartDataEntry] = []

for i in 0..<dataPoints.count {
    let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
    dataEntries.append(dataEntry)
}

let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Days Value")
let chartData = BarChartData(xVals: days, dataSet: chartDataSet)
barChartView.data = chartData

I have taken this answer from https://www.appcoda.com/ios-charts-api-tutorial/ : that you can refer for more options and detailed description.

我从https://www.appcoda.com/ios-charts-api-tutorial/得到了这个答案:您可以参考更多的选项和详细的描述。

#1


3  

If you look at the answer found at the following link, you will see that someone has gotten the new Swift 2.3 version of Charts to permit the use of string labels on the x-axis:

如果您查看以下链接中的答案,您将看到有人获得了新的Swift 2.3版本的图表,允许在x轴上使用字符串标签:

https://*.com/a/39149070/6889264

https://*.com/a/39149070/6889264

You indicate that you are working with Swift 3.0, but the revised BarChartData() function syntax that created this issue looks to be the same between 2.3 and 3.0, so it seems likely the same solution applies.

您表示正在使用Swift 3.0,但是修改后的BarChartData()函数语法在2.3和3.0之间看起来是相同的,因此似乎应用了相同的解决方案。

EDIT

编辑

Just tested, that answer can be implemented in Swift 3.

经过测试,这个答案可以在Swift 3中实现。

#2


0  

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
let daysValues = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0]

setChart(days, values: daysValues)

var dataEntries: [BarChartDataEntry] = []

for i in 0..<dataPoints.count {
    let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
    dataEntries.append(dataEntry)
}

let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Days Value")
let chartData = BarChartData(xVals: days, dataSet: chartDataSet)
barChartView.data = chartData

I have taken this answer from https://www.appcoda.com/ios-charts-api-tutorial/ : that you can refer for more options and detailed description.

我从https://www.appcoda.com/ios-charts-api-tutorial/得到了这个答案:您可以参考更多的选项和详细的描述。