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

Work around for scalable xaxis for line chart #593

Closed
travisstenerson opened this issue Dec 5, 2015 · 3 comments
Closed

Work around for scalable xaxis for line chart #593

travisstenerson opened this issue Dec 5, 2015 · 3 comments

Comments

@travisstenerson
Copy link

Hi guys. I'm new here and I am coming up with a concept to get around the non scalable x axis.

First, I love this graphing library thank you so much to everyone who worked hard to make this.

My experience with swift, and programming experience for that matter, is very very limited, so excuse me if I sound inexperienced. I literally learned how to alter source code last night to change my linerenderer. I am posting here because I know some of you have posted about this and I figured maybe you might want to help a bit by pointing me in the right direction for some of this. If I figure it out entirely I'll post what I've done.

I plan to feed my chart X axis an array of dates, but skipping dates is possible, so I have to represent this in the chart, of course. Here is my plan.

I have an array of Y vals, and an array of x vals that are dates(or any value). I'll convert those dates to integers, starting at 0. The final value on the xaxis will be an integer representation of the final date (in days in my case)

I just run a loop adding values to a new array data, every time I hit an assigned y value on my y array.
For spots along the new x array that are blank, I run a simple slope*change in x + previous y value.
And I'll make an equivalent array of flags(bool) to say when a data point is hit..

like this

func returnMidPointY(x1: Int, x2: Int, y1: Double, y2: Double, midX: Int) -> Double{
    let slope = (y2-y1)/(Double(x2-x1))
    let midY = (Double(midX-x1)*slope + y1)
    return midY

}

var data = [Double]()
var flag = [Bool]()
var count = 0

for i in 0..<(xvals[xvals.count-1]+1){
    if (i == xvals[count])
    {
        data.append(readings[count])
        flag.append(true)
// following if statement is to prevent trying to call count+1 below when array end has been reached
        if (count<xvals.count-1){
            count++}
    }
    else
    {
        let thisY = returnMidPointY(xvals[count-1],x2: xvals[count],y1: readings[count-1],y2: readings[count], midX: i)
        data.append(thisY)
        flag.append(false)
    }
}

That flag array will let me denote where on my xaxis I display a value. Then I will use the flag to alter the circle radius to the same width as my line render. Then I'll use the flag to alter the datavalue display.

Its these last three I need pointers on. I'm going to search through the source code myself and find where I can do this, but if anyone can point me in the right direction, I'd be very appreciative.

Thanks!

@travisstenerson
Copy link
Author

I figured it all out.

The above worked well. I added a public [bool] to LineChartDataSet, used that array in both the DrawCircles and DrawValues function in LineChartRenderer as a flag to say when to display a value or a circle.

The values will exceed the maximum that are viewable in the LineChartView, so you have to alter drawvalues to ignore that, and then use the flag to say when to display a value.

@liuxuan30
Copy link
Member

maybe you want to post screenshots what you want to do. Usually if you don't have a y for the specific x, you just don't add value into the dataSet, and the library will ignore it as well, and addLineTo the next (valid) point. Seems you don't have to calculate it.

@travisstenerson
Copy link
Author

ugh. thats a lot easier. thanks. didn't realize you could do this.

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

2 participants