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

Scaling Yaxis #199

Closed
365SplendidSuns opened this issue Jul 9, 2015 · 23 comments
Closed

Scaling Yaxis #199

365SplendidSuns opened this issue Jul 9, 2015 · 23 comments

Comments

@365SplendidSuns
Copy link

When using chartHolder.leftAxis.axiMinimum and chartHolder.rightAxis.axisMinimum the scales on the y axis no longer stretch to the top of the chart as in the pic below
screenshot 2015-07-09 18 31 10

Also, using chartHolder.autoScaleMinMaxEnabled = true as an alternative gives an EXC_BAD_INSTRUCTION on line 93 of ChartDataSet.swift

@danielgindi
Copy link
Collaborator

Did you notifyDataSetChanged()?

@365SplendidSuns
Copy link
Author

I tried adding the line

chartHolder.notifyDataSetChanged()

right after setting the minimum axis values. It just seems to set the minimum back to 0.

@danielgindi
Copy link
Collaborator

That's because you are attempting to modify properties that are automatically calculated.
You should modify the custom versions of those. Namely customAxisMin and customAxisMax

@365SplendidSuns
Copy link
Author

Hi Daniel,

Sorry to bother you. As you've closed the issue you're obviously confident
that everything works as it should but I've been stuck on this for ages and
would really appreciate it if you could help me out getting these Y-Axis to
look as they should.

I tried using .customAxisMin as you suggested but this didn't seems to do
anything - the axis starts at 0 and stretches to the top exactly as it
would with no attempt to set the min value on the axis.

This is my code as it is

        // Y1

        var y1DataEntries: [ChartDataEntry] = getDataEntries(xAxisValues,

setId: y1SetId)

        let lineChartDataSetY1 = LineChartDataSet(yVals: y1DataEntries,

label: axisUnitsData["y1"]!.name)

        lineChartDataSetY1.axisDependency = .Left

        lineChartDataSetY1.colors = [UIColor.redColor()]




        // Y2

        var y2DataEntries: [ChartDataEntry] = getDataEntries(xAxisValues,

setId: y2SetId)

        let lineChartDataSetY2 = LineChartDataSet(yVals: y2DataEntries,

label: axisUnitsData["y2"]!.name)

        lineChartDataSetY2.axisDependency = .Right

        lineChartDataSetY2.colors = [UIColor.greenColor()]



        // Set Data

        lineChartData = LineChartData(xVals: xAxisValues, dataSets:

[lineChartDataSetY1, lineChartDataSetY2])

        chartHolder.data = lineChartData



        //chartHolder.autoScaleMinMaxEnabled = true





        // Set Axis Scales

        chartHolder.leftAxis.customAxisMin = 11 //minY1

        chartHolder.rightAxis.customAxisMin = 7 //minY2



        //chartHolder.notifyDataSetChanged()

Thanks a lot for your help

Best,

Adam

On Thu, Jul 9, 2015 at 7:34 PM, Daniel Cohen Gindi <notifications@github.com

wrote:

Closed #199 #199.


Reply to this email directly or view it on GitHub
#199 (comment).

@danielgindi
Copy link
Collaborator

Still you need to notifyDataSetChanged(), or change the properties before you set the data

@365SplendidSuns
Copy link
Author

I tried both of those and it makes no difference

@danielgindi
Copy link
Collaborator

Maybe post a sample project so we can actually see what's happening?

On Thu, Jul 9, 2015 at 10:09 PM, 365SplendidSuns notifications@github.com
wrote:

I tried both of those and it makes no difference


Reply to this email directly or view it on GitHub
#199 (comment)
.

@365SplendidSuns
Copy link
Author

Here is all my code involved with making the chart. It's far from the
finished article...

@IBAction func drawChartButtonPressed(sender: AnyObject) {

    chartHolder.hidden = false

    chartToolbar.hidden = false

    drawChart()



}




func drawChart() {



    // General options

    chartHolder.noDataText = "No Records To Chart"

    chartHolder.descriptionText =  ""

    chartHolder.xAxis.labelPosition = .Bottom

    //chartHolder.animate(xAxisDuration: 1.0, yAxisDuration: 1.0,

easingOption: .EaseInBounce)

    chartHolder.rightAxis.drawGridLinesEnabled = false

    chartHolder.leftAxis.drawGridLinesEnabled = false




    /// Min and Max y axis values

    var minY1: Double! = 0

    var maxY1: Double! = 0

    var minY2: Double! = 0

    var maxY2: Double! = 0





    // Draw Chart

    // Get default axis units

    var axisUnitsData = defaultAxisUnit()



    // All possible xValues

    var xAxisValues: [String] = getXAxisValues()



    // data entries

    if let y1SetId = axisUnitsData["y1"]!.setId {



        minY1 = axisMin(y1SetId)

        maxY1 = axisMax(y1SetId)



        var y1DataEntries: [ChartDataEntry] = getDataEntries(xAxisValues,

setId: y1SetId)

        let lineChartDataSetY1 = LineChartDataSet(yVals: y1DataEntries,

label: axisUnitsData["y1"]!.name)

        lineChartDataSetY1.axisDependency = .Left

        lineChartDataSetY1.colors = [UIColor.redColor()]









        // If there is y2 data then add that - Draw chart

        let lineChartData: LineChartData!

        if let y2SetId = axisUnitsData["y2"]!.setId {



            minY2 = axisMin(y2SetId)

            maxY2 = axisMax(y2SetId)



            var y2DataEntries: [ChartDataEntry] =

getDataEntries(xAxisValues,
setId: y2SetId)

            let lineChartDataSetY2 = LineChartDataSet(yVals:

y2DataEntries, label: axisUnitsData["y2"]!.name)

            lineChartDataSetY2.axisDependency = .Right

            lineChartDataSetY2.colors = [UIColor.greenColor()]

            lineChartData = LineChartData(xVals: xAxisValues, dataSets:

[lineChartDataSetY1, lineChartDataSetY2])

        } else {

            lineChartData = LineChartData(xVals: xAxisValues, dataSet:

lineChartDataSetY1)

        }



        chartHolder.leftAxis.customAxisMin = 11 //minY1

        //chartHolder.leftAxis.customAxisMax = maxY1



        chartHolder.rightAxis.customAxisMin = 7 //minY2

        //chartHolder.rightAxis.customAxisMax = maxY2



        chartHolder.data = lineChartData







        //chartHolder.autoScaleMinMaxEnabled = true





        // Axis options

        // Left







        //chartHolder.notifyDataSetChanged()















    }

}



func axisMin(setId: Int) -> Double {



    var minVal: Double = 0.0

    if let firstVal: Double = exercise.trackingRecords[0].records

[setId]!.record.toDouble() {

        minVal = firstVal

    }





    for tr in exercise.trackingRecords {

        if let rec: Double = tr.records[setId]!.record.toDouble() {

            if(rec < minVal) {

                minVal = rec

            }

        }

    }



    minVal = minVal - (minVal * 0.1)



    return round(minVal)

}



func axisMax(setId: Int) -> Double {



    var maxVal: Double = 0.0

    if let firstVal: Double = exercise.trackingRecords[0].records

[setId]!.record.toDouble() {

        maxVal = firstVal

    }





    for tr in exercise.trackingRecords {

        if let rec: Double = tr.records[setId]!.record.toDouble() {

            if(rec > maxVal) {

                maxVal = rec

            }

        }

    }



    maxVal = maxVal + (maxVal * 0.1)



    return round(maxVal)

}



func getDataEntries(xAxisValues: [String], setId: Int) -> [

ChartDataEntry] {

    var dataEntries: [ChartDataEntry] = []



    for tr in exercise.trackingRecords {



        // Get xIndex of this record (All Y values are unique)

        if let index = find(xAxisValues, tr.time.timestampToShortDate())

{

            // If the record is a number (ie a double)

            if let rec: Double = tr.records[setId]!.record.toDouble() {

                let dataEntry = ChartDataEntry(value: rec, xIndex:

index)

                dataEntries.append(dataEntry)

            }

        }



    }



    return dataEntries

}



func getXAxisValues() -> [String] {



    let oneDay: Int = 60*60*24



    var values: [String] = []



    // Get the first and last day

    var firstDayTimestamp: Int = 9999999999

    var lastDayTimestamp: Int = 0

    for tr in exercise.trackingRecords {

        if(tr.time > lastDayTimestamp) {

            lastDayTimestamp = tr.time

        }

        if(tr.time < firstDayTimestamp) {

            firstDayTimestamp = tr.time

        }

    }



    // Put days between first and last into values array

    var iTime = firstDayTimestamp

    while(iTime.timestampToShortDate() != (lastDayTimestamp + oneDay).

timestampToShortDate()) {

        values.append(iTime.timestampToShortDate())

        iTime = iTime + oneDay

    }



    return values

}


// Get default units for each axis

func defaultAxisUnit() -> Dictionary<String, Recordset>{

    var units: Dictionary<String, Recordset> = Dictionary()



    var first: Bool = true;

    for set in self.exercise.recordsets {



        if(first) {

            units["y1"] = set

        } else {

            var y2IsSet: Bool = false

            if let setTest = units["y2"] {

                y2IsSet = true

            }



            if((units["y1"]!.units != set.units) && !y2IsSet) {

                units["y2"] = set

            }

        }

        first = false;

    }


    return units

}

On Thu, Jul 9, 2015 at 8:11 PM, Daniel Cohen Gindi <notifications@github.com

wrote:

Maybe post a sample project so we can actually see what's happening?

On Thu, Jul 9, 2015 at 10:09 PM, 365SplendidSuns <notifications@github.com

wrote:

I tried both of those and it makes no difference


Reply to this email directly or view it on GitHub
<
#199 (comment)

.


Reply to this email directly or view it on GitHub
#199 (comment)
.

@danielgindi
Copy link
Collaborator

danielgindi commented Jul 9, 2015 via email

@365SplendidSuns
Copy link
Author

Yes, sorry, my last post was a bit of a mess

Here is a simple sample project with the same problem

https://github.com/365SplendidSuns/Testing-Charts

@365SplendidSuns
Copy link
Author

Did you have a chance to take a look at the sample project I put together for you? iOS Charts is much better than the other charting packages I've seen so it would be great if this issue could be fixed.

@danielgindi
Copy link
Collaborator

Haven't had the time yet, sorry. Anyway in the meanwhile you could look at
the demos to learn how to do it correctly.

On Mon, Jul 13, 2015 at 6:57 PM, 365SplendidSuns notifications@github.com
wrote:

Did you have a chance to take a look at the sample project I put together
for you? iOS Charts is much better than the other charting packages I've
seen so it would be great if this issue could be fixed.


Reply to this email directly or view it on GitHub
#199 (comment)
.

@avishaan
Copy link
Contributor

I seem to be having a similar issue.

@365SplendidSuns, I was able to build your project but it wouldn't run in my simulator. What version of Xcode did you use on that project?

@365SplendidSuns
Copy link
Author

I'm using XCode 6.3

@avishaan
Copy link
Contributor

This is working for me, my problem was using axisMinimum vs customAxisMin.

Where in the readme does it say use customAxisMin btw? I have been using the Android readme and kind of struggling.

@365SplendidSuns
Copy link
Author

So, the sample project is now working in your simulator and the axis range is setting correctly without you making any alterations to the code in the project?

@avishaan
Copy link
Contributor

I mean't my own project is working. I can't get your project to work in Xcode 6.4 Sorry.

@rajeshm20
Copy link

Thanks Everyone on this issue....some way it helped me to solve my problem...@365SplendidSuns thanks for your sample project...it helped me to understand Charts better in swift 2.2.

@rajeshm20
Copy link

rajeshm20 commented Aug 6, 2016

And Heartfelt Thanks to @danielgindi for this awesome Charts.

@wessamabdrabo
Copy link

@danielgindi Hej!

Could you check this out? http://stackoverflow.com/questions/42090783/iso-charts-line-chart-right-axis-min-and-max-values-not-set-properly
Struggling with it for days. Setting the right axis axisMaximum and axisMinimum seem not to work at all. While setting it for the left axis with same dataset and setup works perfectly. Any tips would help.

@liuxuan30
Copy link
Member

@wessamabdrabo have you set your data set axisDependency to right axis?

@dsoike
Copy link

dsoike commented Mar 14, 2017

I was dealing with the same problem. The source for me was lineChart.setVisibleYRangeMinimum(minYRange: <Double>, axis: .left).

With leftAxis.axisMinimum = 0, leftAxis.axisMaximum = 10, and minYRange = 3, everything was working fine.

When changing leftAxis.axisMaximum to 1 (in order to show different data), the minYRange = 3 was preventing my leftAxis from updating properly. Setting minYRange = 0.15 did the trick.

@InfinityCode777
Copy link

Hi, thank you so much @365SplendidSuns, this is exactly what I am looking for.

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

8 participants