Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add labels for XAxis for BarChartView in ios-charts

I added a bar chart to the storyboard, but I cannot properly set labels for my data entries.

here is my code:

var names = ["aaa", "bbb", "ccc", "ddd"]
var values = [230.0, 280.0, 450.0, 340.0]

setChart(dataPoints: names, values: values)

setChart function:

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
}

and finally the formatter:

@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
    }
}

but it didn't work well as shown below:

enter image description here

as shown here, it add 6 labels instead 4 labels, and it also has duplicates.

I already read this solution, however, as you can see, it has some issues yet.

How can I solve this problem?

like image 837
Mehdi Mirzaei Avatar asked Oct 10 '16 14:10

Mehdi Mirzaei


People also ask

How do I get the label on the category axis?

The category axis, which is the default x-axis for line and bar charts, uses the index as internal data format. For accessing the label, use this.getLabelForValue (value). API: getLabelForValue

How do I style labels in Highcharts?

CSS styles for the label. Use whiteSpace: 'nowrap' to prevent wrapping of category labels. Use textOverflow: 'none' to prevent ellipsis (dots). In styled mode, the labels are styled with the .highcharts-axis-labels class. Whether to use HTML to render the labels. Defaults to false.

What is the best third-party charts Library for iOS?

iOS-Charts is a very flexible, easy-to-use third-party library, allowing you to highly customise your chart. I recently use iOS-Charts at work.

What is the Xaxis value in the tooltip?

If undefined, the xaxis tooltip uses the default “X” value used in general tooltip. xaxis: { tooltip: { formatter: function(val, opts) { return val + "..."


Video Answer


1 Answers

I think you can try to set the following properties:

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

The source code tells you a lot about the properties above. https://github.com/danielgindi/Charts/blob/master/Source/Charts/Components/AxisBase.swift

like image 169
Hong Wei Avatar answered Nov 14 '22 12:11

Hong Wei