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

Default zoom level at a position. #267

Closed
tleafs opened this issue Aug 3, 2015 · 8 comments
Closed

Default zoom level at a position. #267

tleafs opened this issue Aug 3, 2015 · 8 comments

Comments

@tleafs
Copy link

tleafs commented Aug 3, 2015

First of all, great library. I wish i found this library sooner, I am still learning all the features. There is one basic feature, however I need some help to get it setup which is to plot a simple line chart or bar chart that has 100 data points, but instead of display all these 100 data point when the chart shows up, is there a way to default to show the last 10 data points or the first 10 data points and then user can scroll to view the rest. Is this feature available and how to set it up? Thnx

@liuxuan30
Copy link
Member

I found it's similar with my old post. What I was asking is to restore the scale/scroll offset state while jumping back the the chart. You can take a look at this:#226

@tleafs
Copy link
Author

tleafs commented Aug 4, 2015

Thanks for your quick response, I did saw that post before, but still not quit sure what to set to achieve the desired goal. I only have 1 VC so no need to carry over the 2nd VC. if there is same code will be greatly appreciated.

@liuxuan30
Copy link
Member

I am not sure, but it's not a easy job to can come up a solution very quickly.

If I were you I would first look at panGestureRecognized and pinchGestureRecognized to understand how the zoom/scroll is implemented. Generally it needs to do some calculation about the touch location and get the new scale/translation value and do some CGAffineTransform and refresh the matrix via viewPortHandler.refresh()

There are many APIs may help in ChartTransformer and ChartViewPortHandler. To convert the data values and screen pixels to each other.

You have to think about how to calculate the first/last 10 data points translation value given the scale level. I can guess if I could get the specific data point value given the scale level, try do some calculation to let the viewPortHandler.contentRect to only contain the data you need (via first data and last data point x value), so we can calculate the bounds and offset, and refresh the matrix.

Welcome better answers:) I just give some seconds to think about it and continue my work.

@liuxuan30
Copy link
Member

Of cause you need to understand how we create the matrix via:

self._viewPortHandler.restrainViewPort(offsetLeft: left, offsetTop: top, offsetRight: right, offsetBottom: bottom)
prepareOffsetMatrix()
prepareValuePxMatrix()

@tleafs
Copy link
Author

tleafs commented Aug 4, 2015

I was able to achieve this after playing with it a bit. It is actually pretty straight forward.

chart.data = data;   // This is the MAIN reason, data for the chart has to be set first before the next 2 steps otherwise they will just get ignored like nothing has been set.

// set the max x data point to 10, which is what i wanted showing only 10 data points for the line or bar chart, from 100 data points.
[chart setVisibleXRangeMaximum:10];
// move the chart view to 90 index of X, which is the last 10 entries I want to see. and it works as expected.
[chart moveViewToX:90];

@tleafs tleafs closed this as completed Aug 4, 2015
@liuxuan30
Copy link
Member

nice catch!

@spauldingim
Copy link

If you do not want to 'lock' the view to only displaying 10 data points you could use the following in viewDidLoad.
float xScale = totalPoints / initialPointsDisplayed;
[chart zoom:xScale scaleY:1.0 x:0.0 y:0.0];
[self.chartView moveViewToX:totalPoints - initialPointsDisplayed];

@MarksCode
Copy link

@spauldingim I was getting an issue when resetting my bar chart's data where it would zoom in too far when calling your code above again.

Fixed it by first zooming out each time:
barChartView.zoom(scaleX: 0, scaleY: 0, x: 0, y: 0)

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