Charts: Default zoom level at a position.

Created on 4 Aug 2015  ·  8Comments  ·  Source: danielgindi/Charts

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

Most helpful comment

@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)

All 8 comments

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:https://github.com/danielgindi/ios-charts/issues/226

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.

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.

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()

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];

nice catch!

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];

@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)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PrashantKT picture PrashantKT  ·  3Comments

kwstasna picture kwstasna  ·  3Comments

valeIT picture valeIT  ·  3Comments

cilasgimenez picture cilasgimenez  ·  4Comments

guoyutaog picture guoyutaog  ·  3Comments