Skip to content

Commit 773f5a7

Browse files
committed
Add ability to take screen shot bigger (or smaller) than screen
Move locationForPointAtIndex to datasource Fix minor doc issues. Fixed case where label is partially onscreen, but point is not. Don't allow zooming infinitely small
1 parent 30dfcd8 commit 773f5a7

2 files changed

Lines changed: 33 additions & 15 deletions

File tree

Classes/BEMSimpleLineGraphView.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ IB_DESIGNABLE @interface BEMSimpleLineGraphView : UIView <UIGestureRecognizerDel
6969
@return The snapshot of the graph as a UIImage object. */
7070
- (UIImage *)graphSnapshotImage NS_AVAILABLE_IOS(7_0);
7171

72+
/** Allows a higher-resolution snapshot of the graph while the app is in the foreground.
73+
@param size in pixels for your image.
74+
@return The snapshot of the graph as a UIImage object. */
75+
- (UIImage *)graphSnapshotImage: (CGSize) size NS_AVAILABLE_IOS(7_0);
76+
7277

7378
/** Takes a snapshot of the graph.
7479
@param appIsInBackground If your app is currently in the background state, pass YES to \p appIsInBackground. Otherwise, when your app is in the foreground you should take advantage of more efficient APIs by passing NO to \p appIsInBackground.
@@ -414,6 +419,13 @@ IB_DESIGNABLE @interface BEMSimpleLineGraphView : UIView <UIGestureRecognizerDel
414419

415420
@optional
416421

422+
/** The horizontal position for a point at the given index. It corresponds to the X-axis value of the Graph.
423+
@discussion if this is not implemented, points will be evenly spaced along x-Axis. If you sometimes want evenly spaced and sometimes not, just return index when you want even spacing.
424+
@param graph The graph object requesting the point value.
425+
@param index The index from left to right of a given point (X-axis). The first value for the index is 0.
426+
@return The X-axis value at a given index. */
427+
- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph locationForPointAtIndex:(NSInteger)index;
428+
417429

418430
//------- X AXIS -------//
419431

@@ -435,7 +447,7 @@ IB_DESIGNABLE @interface BEMSimpleLineGraphView : UIView <UIGestureRecognizerDel
435447
/** The string to display on the label on the Y-axis at a given value.
436448
@discussion this overrids yAxisPrefixOnLineGraph and yAxisSuffixfixOnLineGraph.
437449
@param graph The graph object which is requesting the label on the specified Y-Axis position.
438-
@param location The value for a label in the same units/range as valueForPointAtIndex.
450+
@param value The value for a label in the same units/range as valueForPointAtIndex.
439451
*/
440452
- (nullable NSString *)lineGraph:(nonnull BEMSimpleLineGraphView *)graph labelOnYAxisForValue:(CGFloat)value;
441453

@@ -612,13 +624,6 @@ IB_DESIGNABLE @interface BEMSimpleLineGraphView : UIView <UIGestureRecognizerDel
612624
@return Array of graph indices to place X-Axis labels */
613625
- (NSArray <NSNumber *> *)incrementPositionsForXAxisOnLineGraph:(BEMSimpleLineGraphView *)graph;
614626

615-
//should be in datasource, not delegate
616-
/** The horizontal position for a point at the given index. It corresponds to the X-axis value of the Graph.
617-
@param graph The graph object requesting the point value.
618-
@param index The index from left to right of a given point (X-axis). The first value for the index is 0.
619-
@return The X-axis value at a given index. */
620-
- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph locationForPointAtIndex:(NSInteger)index;
621-
622627
/** The total number of X-axis labels on the line graph.
623628
@discussion Calculates the total wdith of the graph and evenly spaces the labels based on the graph width. If this and lineGraph:locationForPointAtIndex: are implemented, labels may diverge from data points
624629
@param graph The graph object which is requesting the number of labels.
@@ -629,9 +634,9 @@ IB_DESIGNABLE @interface BEMSimpleLineGraphView : UIView <UIGestureRecognizerDel
629634
@discussion Calculates the total wdith of the graph and evenly spaces the labels based on the graph width. Only relevant if lineGraph:locationForPointAtIndex: is implemented. If implemented, it diverges labels from data points
630635
@param oldScale The current scale level. 1.0 = autosized to fit all points. 2.0 would show half the chart
631636
@param newScale New scale level requested by pinchZoom.
632-
@param displayMinXValue The smallest datapoint that will be included in the chart (either index or value, depending on VariableXAxis).
637+
@param displayMinXValue The smallest datapoint that will be included in the chart (either index or value, depending on whether locationForPointAtIndex is implemented).
633638
@param displayMaxXValue The largest datapoint that will be included in the chart.
634-
@return YES if zoom is ok; No if zoom is prevented. */
639+
@return YES if pan/zoom is ok; No if pan/zoom is prevented. */
635640
-(BOOL) lineGraph:(BEMSimpleLineGraphView *)graph shouldScaleFrom:(CGFloat)oldScale to:(CGFloat)newScale showingFromXMinValue:(CGFloat)displayMinXValue toXMaxValue:(CGFloat)displayMaxXValue;
636641

637642
//----- Y AXIS -----//

Classes/BEMSimpleLineGraphView.m

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ - (void)drawXAxis {
822822
NSInteger numberLabels = [self.delegate numberOfXAxisLabelsOnLineGraph: self];
823823
if (numberLabels <= 0) numberLabels = 1;
824824
NSMutableArray <NSValue *> * labelLocs = [NSMutableArray arrayWithCapacity:(NSUInteger)numberLabels];
825-
if ([self.delegate respondsToSelector:@selector(lineGraph:locationForPointAtIndex: )]) {
825+
if ([self.dataSource respondsToSelector:@selector(lineGraph:locationForPointAtIndex: )]) {
826826
CGFloat step = xAxisWidth/(numberLabels-1);
827827
CGFloat positionOnXAxis = 0;
828828
for (NSInteger i = 0; i < numberLabels; i++) {
@@ -882,7 +882,7 @@ - (void)drawXAxis {
882882
NSMutableArray <UIView *> *newXAxisLabels = [NSMutableArray arrayWithCapacity:axisIndices.count];
883883

884884
@autoreleasepool {
885-
BOOL usingLocation = [self.delegate respondsToSelector:@selector(lineGraph:locationForPointAtIndex: )];
885+
BOOL usingLocation = [self.dataSource respondsToSelector:@selector(lineGraph:locationForPointAtIndex: )];
886886
BOOL locationLabels = usingLocation && [self.dataSource respondsToSelector:@selector(lineGraph:labelOnXAxisForLocation:)];
887887
BOOL indexLabels = !usingLocation && [self.dataSource respondsToSelector:@selector(lineGraph:labelOnXAxisForIndex:)];
888888
CGFloat valueRangeWidth = (self.maxXValue - self.minXValue) / self.zoomScale;
@@ -914,7 +914,7 @@ - (void)drawXAxis {
914914
UILabel *labelXAxis = [self xAxisLabelWithText:xAxisLabelText atLocation:positionOnXAxis reuseNumber: index];
915915
[newXAxisLabels addObject:labelXAxis];
916916

917-
if (CGRectContainsRect(self.backgroundXAxis.bounds, labelXAxis.frame)){
917+
if (positionOnXAxis >= 0 && positionOnXAxis <= xAxisWidth) {
918918
[self.backgroundXAxis addSubview:labelXAxis];
919919
if (self.enableReferenceXAxisLines &&
920920
(allLabelLocations[index].CGPointValue.y < BEMNullGraphValue || self.interpolateNullValues)) {
@@ -1299,6 +1299,16 @@ - (UIImage *)graphSnapshotImage {
12991299
return [self graphSnapshotImageRenderedWhileInBackground:NO];
13001300
}
13011301

1302+
- (UIImage *)graphSnapshotImage: (CGSize) size {
1303+
CGRect testRect = CGRectMake(0, 0, size.height, size.width);
1304+
CGRect savedRect = self.frame;
1305+
self.frame = testRect;
1306+
1307+
UIImage * result = [self graphSnapshotImageRenderedWhileInBackground:NO];
1308+
self.frame = savedRect;
1309+
return result;
1310+
}
1311+
13021312
- (UIImage *)graphSnapshotImageRenderedWhileInBackground:(BOOL)appIsInBackground {
13031313
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
13041314

@@ -1440,6 +1450,9 @@ -(BOOL) handleZoom:(CGFloat) newScale orMovement:(CGFloat) newPanMovement checkD
14401450
if (newScale <= 1.0) {
14411451
newScale = 1.0;
14421452
newPanMovement = 0;
1453+
} else if (newScale > self.numberOfPoints/2) {
1454+
//don't allow scaling beyond showing less than 2 points (on average)
1455+
newScale = self.numberOfPoints/2;
14431456
}
14441457
//assumes we're zooming around fixed point self.zoomCenterValue which is currently displayed at self.zoomCenterLocation
14451458
//Now with newScale and newPanMovement,
@@ -1647,8 +1660,8 @@ - (BEMCircle *)closestDotFromTouchInputLine:(UIView *)touchInputLine {
16471660
dotValue = (int)(arc4random() % 10000);
16481661
#endif
16491662
CGFloat xValue = index;
1650-
if ([self.delegate respondsToSelector:@selector(lineGraph:locationForPointAtIndex:)]){
1651-
xValue = [self.delegate lineGraph:self locationForPointAtIndex:index];
1663+
if ([self.dataSource respondsToSelector:@selector(lineGraph:locationForPointAtIndex:)]){
1664+
xValue = [self.dataSource lineGraph:self locationForPointAtIndex:index];
16521665
}
16531666
[newDataPoints addObject:[NSValue valueWithCGPoint:CGPointMake(xValue, dotValue)]];
16541667

0 commit comments

Comments
 (0)