Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings for Labels in Swift 3 #1732

Closed
olliecart opened this issue Oct 27, 2016 · 2 comments
Closed

Strings for Labels in Swift 3 #1732

olliecart opened this issue Oct 27, 2016 · 2 comments

Comments

@olliecart
Copy link

Below is my code. I am trying to figure out how to have the value from the array "Labels" appear at the bottom of the table. I have values down there, just not what I want.
The old version had a value called xVals or xValues.
I cannot get the thing to build when adding xVals or trying to give the x any value other than a double.
The dev environment won't allow me to insert anything but doubles.

` func displayRankingBars(){
//ok I need to mkae a ["string", [int, int]]

    let labels = ["3 Week", "2 Week", "Last Week", "This Week"]
    let myScores = [85, 90, 95, 80]
    let groupScores = [90, 91, 90, 91]


    var myDataEntries : [BarChartDataEntry] = []
    var groupDataEntries : [BarChartDataEntry] = []

    for i in 0..<labels.count {
        let dataEntry1 = BarChartDataEntry(x: Double(i), yValues: [Double(myScores[i])], label: "Here") //labels[i]
        myDataEntries.append(dataEntry1)
        let dataEntry2 = BarChartDataEntry(x: Double(i + 20), y: Double(groupScores[i]))

        groupDataEntries.append(dataEntry2)
    }



    let chartDataSetMine = BarChartDataSet(values: myDataEntries, label: "My Score")
    chartDataSetMine.colors = [UIColor.orange]

    let chartDataSetGroup = BarChartDataSet(values: groupDataEntries, label: "Group Average")
    chartDataSetGroup.colors = [UIColor.green]

    var dataSet : [BarChartDataSet] = []
    dataSet.append(chartDataSetMine)
    dataSet.append(chartDataSetGroup)

    chartDataSetGroup.drawValuesEnabled = false




    historyView.fitBars = true

    let data = BarChartData(dataSets: dataSet)


    data.barWidth = 1.5
    data.groupBars(fromX: 0.1, groupSpace: 0.6, barSpace: 0.9)

    historyView.animate(xAxisDuration: 2.0)
    historyView.xAxis.centerAxisLabelsEnabled = true

    historyView.animate(yAxisDuration: 2.0)


    historyView.data = data

    historyView.xAxis.labelPosition = XAxis.LabelPosition.bottom
    historyView.xAxis.drawLabelsEnabled = true

    historyView.rightAxis.enabled = false
    historyView.leftAxis.enabled = false

    historyView.xAxis.drawGridLinesEnabled = false
    historyView.xAxis.drawAxisLineEnabled = false
    //historyView.xAxis.drawLabelsEnabled = false

    let disAccess = historyView.xAxis

    disAccess.forceLabelsEnabled = true

}`
@nickjwallin
Copy link

I just successfully converted a couple LineChartView charts to Charts 3.0. Here's what I did, I think it should help:

Before I was building arrays of xVals (NSMutableArray<NSString*>) and yVals (NSMutableArray<ChartDataEntry*>). My strings were the x-axis labels. My y-axis (leftAxis) had a valueFormatter which was an NSNumberFormatter that simple converted the double into a nice-looking number string, like "40".

With Charts 3.0, first thing you have to do is assign your axes to have a valueFormatter that is something which implements the IChartAxisValueFormatter. I just had the view controller which had the chart implement the protocol, which is just the - (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis method. So I had _lineChartView.xAxis.valueFormatter = self (and same for leftAxis). I set my LineChartView's xAxis and leftAxis properties to be properties on my view controller as well. In the -stringForValue:axis: method, I had:

if (axis == _leftAxis) {
  // make the NSString to display for the left axis
} else {
  // make the NSString to display x-axis
}

Okay, what do we put in those comments? Whatever you were putting as strings for the values, you're going to have to keep some sort of array of strings, or dictionary of doubles to strings, or calculate the string in that method. I'll explain further. When you are building your ChartDataEntry objects, which only take in an x double and a y double, you'll just go through your data points (say, a for loop for (NSInteger i = 0; i < count; i++)), and build them with x being i (converted to double) and y being the value.

In your -stringForValue:axis: method, you will be given the value (which is the double value you set when you made the ChartDataEntry) and you can convert that to an NSInteger, and access the correct string value in your NSMutableArray, for example. Here is mine:

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis
{
    if (axis == _yAxis)
    {
        return [_numberFormatter stringFromNumber:[NSNumber numberWithDouble:value]];
    }
    else
    {
        NSInteger index = [NSNumber numberWithDouble:value].integerValue;
        Score *score = [self scoreForIndex:index]; // or this could return an NSString to display...
        return [_monthFormatter stringFromDate:score.date]; // ...but i just calculated it here
    }
}

As you can see, we just have to access the string values in a different place.

I had to to play with my chart axes' granularityEnabled and granularity to get the same look as before.

Hope that helps! Sorry it's not written in Swift, but it should be simple enough to figure out.

@liuxuan30
Copy link
Member

thanks @nickjwallin seems I can close it. Feel free to reopen if not resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants