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

Resize chart due to data update #517

Closed
jibax opened this issue Oct 29, 2015 · 10 comments
Closed

Resize chart due to data update #517

jibax opened this issue Oct 29, 2015 · 10 comments

Comments

@jibax
Copy link

jibax commented Oct 29, 2015

Hello,

I have the same problem as mlorenzeCDS who posted it 26 days ago here #440 .

What I want to do is to to add values to my linechart when the user scrolls and reach the end of the chart. The chart is inside a UIView, which is contained a UIScrollView so I would like to update the UIView's size in order to keep the chart readability.

Initial situation :
http://hpics.li/a2c9f2a

After reaching the end of the chart :
http://hpics.li/a53b75e

My questions are :

  • How can I update the size (width) of the chart ?
  • Is there a function that update the datas when there is an add (instead of redraw it)

Thanks a lot for your help!

@liuxuan30
Copy link
Member

are you looking for #263?
if you change the size, just change its frame/bound. this should trigger a needsDisplay and redraw the whole line chart

@danielgindi
Copy link
Collaborator

The chart automatically updates when there's a size change

On Thu, Oct 29, 2015 at 12:11 PM, Xuan notifications@github.com wrote:

are you looking for #263
#263?
if you change the size, just change its frame/bound. this should trigger a
needsDisplay and redraw the whole line chart


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

@jibax
Copy link
Author

jibax commented Oct 29, 2015

Thank you for your quick answer

@liuxuan30 : It's a bit different, but I will try this function thanks. When I reach the end of the chart I do this but it looks that it doesn't perform this change. What is wrong ? :

    // Add the yAxis values

    self.months.appendContentsOf(month2)

     // Add the xAxis values (2 lines)
     self.values1.appendContentsOf(unitsSol2)
     self.values2.appendContentsOf(unitsSol2)

    // changing the frame , I multiply by two the width because I double the values. 
    var  frm : CGRect = self.lineChartView.frame
    frm.size.width = self.lineChartView.frame.width * 2
    self.lineChartView.frame = frm;

    // This function performs the settings of the charts (same here : http://www.appcoda.com/ios-charts-api-tutorial/ ) 
    self.setChart(months, values1: self.values1 , values2: self.values2)

@danielgindi : I've edited my post so you can see that the chart is not resized. What I want is to keep the same scale. Is it possible ?

@liuxuan30
Copy link
Member

@jibax seems you did some wrapping; what you mean it doesn't perform this change? If you called chartView.data = yourData or notiftyDataChanged, it should redraw the whole chart and you will see the new data rendered.

For the frame, you should see the new width. Are you using auto layout? To me the code totally fine.

@jibax
Copy link
Author

jibax commented Oct 30, 2015

@liuxuan30 Thank you for your answer. I edited my code and it is working now. The problem is about constraints because I would love to display my chart in landscape. The LineChartView is contained inside a UIScrollView (full width but not in full width) : How is reacting the chart with constraints ? Because mine disappears as soon as I apply constraints to LineChartView. Thank you

@danielgindi
Copy link
Collaborator

The problem is not with the chart, but with the fact that constraints in
UIScrollView are complex :)

In my app, for example, I have a horizontal scroll view containing multiple
charts created dynamically (arbitrary number of charts). I set their
constraints to "width = scrollview.superview.width", and then lay them out
with simple x spacing constraints.

And all of those charts are inside an extra view inside the scrollview,
which helped with letting the scrollview figure out the contentSize.

‏בתאריך יום שישי, 30 באוקטובר 2015, jibax notifications@github.com כתב:

@liuxuan30 https://github.com/liuxuan30 Thank you for your answer. I
edited my code and it is working now. The problem is about constraints
because I would love to display my chart in landscape. The LineChartView is
contained inside a UIScrollView (full width but not in full width) : How is
reacting the chart with constraints ? Because mine disappears as soon as I
apply constraints to LineChartView. Thank you


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

@jibax
Copy link
Author

jibax commented Nov 2, 2015

@danielgindi Yes Daniel you are right, constraints with UIScrollView are really complex. My last question is, is there a maximum number of values that the chart can take in account ? Because my chart disappears when there is more than 160 values or it is a problem with the view ? Thank you

@liuxuan30
Copy link
Member

No, but the word 'disappears' is not clear enough to guess what happened.

@liuxuan30
Copy link
Member

closing as no update and further issue; reopen if you have update

@nathanmrtns
Copy link

nathanmrtns commented Jan 17, 2018

Hi, I believe I have the same bug but I can't fix it.
Here is what happens:
chart bug

The first scenario is when the chart is drawn by the first time and its max value is as shown in the image above.
The scenario 2 happens when I'm scenario 1 and I set the new data to be drawn. It seems if the data shown before has a smaller max value than the data drawn after, the automatic offset calculated doesn't work.
The scenario 3 happens if I'm in scenario 2 and set the same data, then the chart offset works respecting the view bounds.

Is there any function to call before drawing the chart again?
I already tried chart.clear() , chart.notifyDataSetChanged() and nothing seems to work.

[EDIT] - I made it work setting the leftAxis.maxValue before setting the chart data.

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

4 participants