Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 51acae13a1 | |||
| 03217243d9 | |||
| 072e45902d | |||
| 326037ce93 | |||
| f447549666 | |||
| eba1d39ba3 | |||
| 8d28b65add | |||
| e72a54356e | |||
| 86440913ba | |||
| c1ac9e4a2f | |||
| 6f185f9e53 | |||
| d142629dd3 | |||
| 84d9c0f5da | |||
| 5f400d3c91 | |||
| 409b58c53c | |||
| 0b6108dea8 |
@@ -39,7 +39,7 @@
|
||||
*
|
||||
* @return The y-axis height of the supplied bar index (x-axis)
|
||||
*/
|
||||
- (NSInteger)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSInteger)index;
|
||||
- (CGFloat)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSInteger)index;
|
||||
|
||||
@optional
|
||||
|
||||
@@ -90,16 +90,16 @@
|
||||
- (NSInteger)barPaddingForBarChartView:(JBBarChartView *)barChartView;
|
||||
|
||||
/**
|
||||
* The color of all bars within the chart.
|
||||
* A UIView subclass representing the bar at a particular index.
|
||||
*
|
||||
* Default: black color
|
||||
* Default: solid black UIView
|
||||
*
|
||||
* @param barChartView The origin chart
|
||||
* @param index The 0-based index of a given bar (left to right, x-axis)
|
||||
*
|
||||
* @return The color to be used on each of the bars within the chart.
|
||||
* @return A UIView subclass. The view will automatically be resized by the chart during creation (ie. no need to set the frame).
|
||||
*/
|
||||
- (UIColor *)barColorForBarChartView:(JBBarChartView *)barChartView atIndex:(NSInteger)index;
|
||||
- (UIView *)barViewForBarChartView:(JBBarChartView *)barChartView atIndex:(NSInteger)index;
|
||||
|
||||
/**
|
||||
* The selection color to be overlayed on a bar during touch events.
|
||||
|
||||
+11
-13
@@ -78,6 +78,9 @@ static UIColor *kJBBarChartViewDefaultBarColor = nil;
|
||||
|
||||
- (void)reloadData
|
||||
{
|
||||
// reset cached max height
|
||||
self.cachedMaxHeight = kJBBarChartViewUndefinedMaxHeight;
|
||||
|
||||
/*
|
||||
* The data collection holds all position information:
|
||||
* constructed via datasource and delegate functions
|
||||
@@ -93,7 +96,7 @@ static UIColor *kJBBarChartViewDefaultBarColor = nil;
|
||||
NSMutableDictionary *dataDictionary = [NSMutableDictionary dictionary];
|
||||
for (NSInteger index=0; index<dataCount; index++)
|
||||
{
|
||||
[dataDictionary setObject:[NSNumber numberWithInt:(int)[self.delegate barChartView:self heightForBarViewAtAtIndex:index]] forKey:[NSNumber numberWithInt:(int)index]];
|
||||
[dataDictionary setObject:[NSNumber numberWithFloat:[self.delegate barChartView:self heightForBarViewAtAtIndex:index]] forKey:[NSNumber numberWithInt:(int)index]];
|
||||
}
|
||||
self.chartDataDictionary = [NSDictionary dictionaryWithDictionary:dataDictionary];
|
||||
};
|
||||
@@ -129,23 +132,18 @@ static UIColor *kJBBarChartViewDefaultBarColor = nil;
|
||||
NSMutableArray *mutableBarViews = [NSMutableArray array];
|
||||
for (NSNumber *key in [[self.chartDataDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)])
|
||||
{
|
||||
UIView *barView = [[UIView alloc] init];
|
||||
if ([self.dataSource respondsToSelector:@selector(barColorForBarChartView:atIndex:)])
|
||||
UIView *barView = nil; // since all bars are visible at once, no need to cache this view
|
||||
if ([self.dataSource respondsToSelector:@selector(barViewForBarChartView:atIndex:)])
|
||||
{
|
||||
barView.backgroundColor = [self.dataSource barColorForBarChartView:self atIndex:index];
|
||||
barView = [self.dataSource barViewForBarChartView:self atIndex:index];
|
||||
}
|
||||
else
|
||||
{
|
||||
barView = [[UIView alloc] init];
|
||||
barView.backgroundColor = kJBBarChartViewDefaultBarColor;
|
||||
}
|
||||
}
|
||||
CGFloat height = [self normalizedHeightForRawHeight:[self.chartDataDictionary objectForKey:key]];
|
||||
barView.frame = CGRectMake(xOffset, self.bounds.size.height - height - self.footerView.frame.size.height + self.headerPadding, [self barWidth], height + kJBBarChartViewPopOffset - self.headerPadding);
|
||||
|
||||
barView.layer.shadowColor = [UIColor blackColor].CGColor;
|
||||
barView.layer.shadowOffset = CGSizeMake(0, 0);
|
||||
barView.layer.shadowOpacity = 0.4;
|
||||
barView.layer.shadowRadius = 1.0;
|
||||
|
||||
barView.frame = CGRectMake(xOffset, self.bounds.size.height - height - self.footerView.frame.size.height + self.headerPadding, [self barWidth], height + kJBBarChartViewPopOffset - self.headerPadding);
|
||||
[mutableBarViews addObject:barView];
|
||||
|
||||
// Add new bar
|
||||
@@ -222,7 +220,7 @@ static UIColor *kJBBarChartViewDefaultBarColor = nil;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ceil(((value - minHeight) / (maxHeight - minHeight)) * [self availableHeight]);
|
||||
return ((value - minHeight) / (maxHeight - minHeight)) * [self availableHeight];
|
||||
}
|
||||
|
||||
- (CGFloat)maxHeight
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "JBChartView"
|
||||
s.version = "1.0.2"
|
||||
s.version = "1.1.3"
|
||||
s.summary = "Jawbone's iOS-based charting library for both line and bar graphs."
|
||||
s.homepage = "https://github.com/Jawbone/JBChartView"
|
||||
|
||||
@@ -8,7 +8,7 @@ Pod::Spec.new do |s|
|
||||
s.author = { "Terry Worona" => "tworona@jawbone.com" }
|
||||
s.source = {
|
||||
:git => "https://github.com/Jawbone/JBChartView.git",
|
||||
:tag => "v1.0.2"
|
||||
:tag => "v1.1.3"
|
||||
}
|
||||
|
||||
s.platform = :ios, '7.0'
|
||||
|
||||
@@ -34,11 +34,11 @@
|
||||
* the chart will automatically normalize all values between the overal min and max heights.
|
||||
*
|
||||
* @param lineChartView The origin chart
|
||||
* @param index The 0-based index of a given line height (left to right, x-axis)
|
||||
* @param index The 0-based index of a given line height (left to right, x-axis)
|
||||
*
|
||||
* @return The y-axis value of the supplied line index (x-axis)
|
||||
*/
|
||||
- (NSInteger)lineChartView:(JBLineChartView *)lineChartView heightForIndex:(NSInteger)index;
|
||||
- (CGFloat)lineChartView:(JBLineChartView *)lineChartView heightForIndex:(NSInteger)index;
|
||||
|
||||
@optional
|
||||
|
||||
@@ -47,7 +47,7 @@
|
||||
* and the selection must occur within the bounds of the chart.
|
||||
*
|
||||
* @param lineChartView The origin chart
|
||||
* @param index The 0-based index of a selection point (left to right, x-axis)
|
||||
* @param index The 0-based index of a selection point (left to right, x-axis)
|
||||
*/
|
||||
- (void)lineChartView:(JBLineChartView *)lineChartView didSelectChartAtIndex:(NSInteger)index;
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
* For selection start events, see: didSelectChartAtIndex...
|
||||
*
|
||||
* @param lineChartView The origin chart
|
||||
* @param index The 0-based index of a selection point. Index will be -1 if the touch ends outside of the view's bounds.
|
||||
* @param index The 0-based index of a selection point. Index will be -1 if the touch ends outside of the view's bounds.
|
||||
*/
|
||||
- (void)lineChartView:(JBLineChartView *)lineChartView didUnselectChartAtIndex:(NSInteger)index;
|
||||
|
||||
|
||||
+17
-11
@@ -75,7 +75,7 @@ static UIColor *kJBLineChartViewDefaultLineColor = nil;
|
||||
@property (nonatomic, assign) BOOL selectionViewVisible;
|
||||
|
||||
// View quick accessors
|
||||
- (CGFloat)normalizedHeightForRawHeight:(NSInteger)rawHeight;
|
||||
- (CGFloat)normalizedHeightForRawHeight:(CGFloat)rawHeight;
|
||||
- (CGFloat)availableHeight;
|
||||
- (CGFloat)maxHeight;
|
||||
- (CGFloat)minHeight;
|
||||
@@ -117,6 +117,9 @@ static UIColor *kJBLineChartViewDefaultLineColor = nil;
|
||||
|
||||
- (void)reloadData
|
||||
{
|
||||
// reset cached max height
|
||||
self.cachedMaxHeight = kJBLineChartViewUndefinedMaxHeight;
|
||||
|
||||
/*
|
||||
* Subview rectangle calculations
|
||||
*/
|
||||
@@ -138,11 +141,10 @@ static UIColor *kJBLineChartViewDefaultLineColor = nil;
|
||||
for (NSInteger index=0; index<[self dataCount]; index++)
|
||||
{
|
||||
JBLineChartPoint *chartPoint = [[JBLineChartPoint alloc] init];
|
||||
NSInteger rawHeight = [self.delegate lineChartView:self heightForIndex:index];
|
||||
CGFloat rawHeight = [self.delegate lineChartView:self heightForIndex:index];
|
||||
CGFloat normalizedHeight = [self normalizedHeightForRawHeight:rawHeight];
|
||||
yOffset = mainViewRect.size.height - normalizedHeight;
|
||||
|
||||
//yOffset = mainViewRect.size.height - yOffset;
|
||||
chartPoint.position = CGPointMake(xOffset, yOffset);
|
||||
|
||||
[mutableChartData addObject:chartPoint];
|
||||
@@ -203,7 +205,7 @@ static UIColor *kJBLineChartViewDefaultLineColor = nil;
|
||||
|
||||
#pragma mark - View Quick Accessors
|
||||
|
||||
- (CGFloat)normalizedHeightForRawHeight:(NSInteger)rawHeight
|
||||
- (CGFloat)normalizedHeightForRawHeight:(CGFloat)rawHeight
|
||||
{
|
||||
CGFloat minHeight = [self minHeight];
|
||||
CGFloat maxHeight = [self maxHeight];
|
||||
@@ -213,7 +215,7 @@ static UIColor *kJBLineChartViewDefaultLineColor = nil;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ceil(((rawHeight - minHeight) / (maxHeight - minHeight)) * [self availableHeight]);
|
||||
return ((rawHeight - minHeight) / (maxHeight - minHeight)) * [self availableHeight];
|
||||
}
|
||||
|
||||
- (CGFloat)availableHeight
|
||||
@@ -223,16 +225,20 @@ static UIColor *kJBLineChartViewDefaultLineColor = nil;
|
||||
|
||||
- (CGFloat)maxHeight
|
||||
{
|
||||
NSAssert([self.delegate respondsToSelector:@selector(lineChartView:heightForIndex:)], @"JBLineChartView // delegate must implement - (NSInteger)lineChartView:(JBLineChartView *)lineChartView heightForIndex:(NSInteger)index");
|
||||
NSInteger maxHeight = 0;
|
||||
for (NSInteger index=0; index<[self dataCount]; index++)
|
||||
if (self.cachedMaxHeight == kJBLineChartViewUndefinedMaxHeight)
|
||||
{
|
||||
if (([self.delegate lineChartView:self heightForIndex:index]) > maxHeight)
|
||||
NSAssert([self.delegate respondsToSelector:@selector(lineChartView:heightForIndex:)], @"JBLineChartView // delegate must implement - (NSInteger)lineChartView:(JBLineChartView *)lineChartView heightForIndex:(NSInteger)index");
|
||||
CGFloat maxHeight = 0;
|
||||
for (NSInteger index=0; index<[self dataCount]; index++)
|
||||
{
|
||||
maxHeight = [self.delegate lineChartView:self heightForIndex:index];
|
||||
if (([self.delegate lineChartView:self heightForIndex:index]) > maxHeight)
|
||||
{
|
||||
maxHeight = [self.delegate lineChartView:self heightForIndex:index];
|
||||
}
|
||||
}
|
||||
self.cachedMaxHeight = maxHeight;
|
||||
}
|
||||
return maxHeight;
|
||||
return self.cachedMaxHeight;
|
||||
}
|
||||
|
||||
- (CGFloat)minHeight
|
||||
|
||||
@@ -65,7 +65,7 @@ NSString * const kJBBarChartViewControllerNavButtonViewKey = @"view";
|
||||
NSMutableArray *mutableChartData = [NSMutableArray array];
|
||||
for (int i=0; i<kJBBarChartViewControllerNumBars; i++)
|
||||
{
|
||||
[mutableChartData addObject:[NSNumber numberWithInteger:MAX(kJBBarChartViewControllerMinBarHeight, arc4random() % kJBBarChartViewControllerMaxBarHeight)]]; // fake height
|
||||
[mutableChartData addObject:[NSNumber numberWithFloat:MAX(kJBBarChartViewControllerMinBarHeight, arc4random() % kJBBarChartViewControllerMaxBarHeight)]]; // fake height
|
||||
}
|
||||
_chartData = [NSArray arrayWithArray:mutableChartData];
|
||||
}
|
||||
@@ -120,9 +120,9 @@ NSString * const kJBBarChartViewControllerNavButtonViewKey = @"view";
|
||||
|
||||
#pragma mark - JBBarChartViewDelegate
|
||||
|
||||
- (NSInteger)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSInteger)index
|
||||
- (CGFloat)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSInteger)index
|
||||
{
|
||||
return [[self.chartData objectAtIndex:index] intValue];
|
||||
return [[self.chartData objectAtIndex:index] floatValue];
|
||||
}
|
||||
|
||||
#pragma mark - JBBarChartViewDataSource
|
||||
@@ -137,9 +137,11 @@ NSString * const kJBBarChartViewControllerNavButtonViewKey = @"view";
|
||||
return kJBBarChartViewControllerBarPadding;
|
||||
}
|
||||
|
||||
- (UIColor *)barColorForBarChartView:(JBBarChartView *)barChartView atIndex:(NSInteger)index
|
||||
- (UIView *)barViewForBarChartView:(JBBarChartView *)barChartView atIndex:(NSInteger)index
|
||||
{
|
||||
return (index % 2 == 0) ? kJBColorBarChartBarBlue : kJBColorBarChartBarGreen;
|
||||
UIView *barView = [[UIView alloc] init];
|
||||
barView.backgroundColor = (index % 2 == 0) ? kJBColorBarChartBarBlue : kJBColorBarChartBarGreen;
|
||||
return barView;
|
||||
}
|
||||
|
||||
- (UIColor *)selectionBarColorForBarChartView:(JBBarChartView *)barChartView
|
||||
|
||||
@@ -14,13 +14,14 @@
|
||||
#import "JBLineChartFooterView.h"
|
||||
#import "JBChartInformationView.h"
|
||||
|
||||
#define ARC4RANDOM_MAX 0x100000000
|
||||
|
||||
// Numerics
|
||||
CGFloat const kJBLineChartViewControllerChartHeight = 250.0f;
|
||||
CGFloat const kJBLineChartViewControllerChartHeaderHeight = 75.0f;
|
||||
CGFloat const kJBLineChartViewControllerChartHeaderPadding = 20.0f;
|
||||
CGFloat const kJBLineChartViewControllerChartFooterHeight = 20.0f;
|
||||
NSInteger const kJBLineChartViewControllerNumChartPoints = 27;
|
||||
NSInteger const kJBLineChartViewControllerMaxPointValue = 100; // max random value
|
||||
|
||||
// Strings
|
||||
NSString * const kJBLineChartViewControllerNavButtonViewKey = @"view";
|
||||
@@ -60,7 +61,7 @@ NSString * const kJBLineChartViewControllerNavButtonViewKey = @"view";
|
||||
NSMutableArray *mutableChartData = [NSMutableArray array];
|
||||
for (int i=0; i<kJBLineChartViewControllerNumChartPoints; i++)
|
||||
{
|
||||
[mutableChartData addObject:[NSNumber numberWithInteger:arc4random() % kJBLineChartViewControllerMaxPointValue]];
|
||||
[mutableChartData addObject:[NSNumber numberWithFloat:((double)arc4random() / ARC4RANDOM_MAX)]]; // random number between 0 and 1
|
||||
}
|
||||
_chartData = [NSArray arrayWithArray:mutableChartData];
|
||||
}
|
||||
@@ -127,15 +128,15 @@ NSString * const kJBLineChartViewControllerNavButtonViewKey = @"view";
|
||||
|
||||
#pragma mark - JBLineChartViewDelegate
|
||||
|
||||
- (NSInteger)lineChartView:(JBLineChartView *)lineChartView heightForIndex:(NSInteger)index
|
||||
- (CGFloat)lineChartView:(JBLineChartView *)lineChartView heightForIndex:(NSInteger)index
|
||||
{
|
||||
return [[self.chartData objectAtIndex:index] intValue];
|
||||
return [[self.chartData objectAtIndex:index] floatValue];
|
||||
}
|
||||
|
||||
- (void)lineChartView:(JBLineChartView *)lineChartView didSelectChartAtIndex:(NSInteger)index
|
||||
{
|
||||
NSNumber *valueNumber = [self.chartData objectAtIndex:index];
|
||||
[self.informationView setValueText:[NSString stringWithFormat:@"%d", [valueNumber intValue]] unitText:kJBStringLabelMm];
|
||||
[self.informationView setValueText:[NSString stringWithFormat:@"%.2f", [valueNumber floatValue]] unitText:kJBStringLabelMm];
|
||||
[self.informationView setTitleText:[NSString stringWithFormat:@"%d", [kJBStringLabel1987 intValue] + (int)index]];
|
||||
[self.informationView setHidden:NO animated:YES];
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ Simply add the following line to your <code>Podfile</code>:
|
||||
Your Podfile should look something like:
|
||||
|
||||
platform :ios, '7.0'
|
||||
pod 'JBChartView', '~> 1.0.2'
|
||||
pod 'JBChartView', '~> 1.1.3'
|
||||
|
||||
### The Old School Way
|
||||
|
||||
@@ -52,7 +52,7 @@ Both JBChartView implementations have a similiar data source and delgate pattern
|
||||
|
||||
#### JBBarChartView
|
||||
|
||||
To initialze a <i>JBBarChartView</i>, you only need a few lines of code:
|
||||
To initialize a <i>JBBarChartView</i>, you only need a few lines of code:
|
||||
|
||||
JBBarChartView *barChartView = [[JBBarChartView alloc] init];
|
||||
barChartView.delegate = self;
|
||||
@@ -66,16 +66,16 @@ At a minimum, you need to inform the data source how many bars are in the chart:
|
||||
return ...; // number of bars in chart
|
||||
}
|
||||
|
||||
Secondly, you nee to inform the delegate the height of each bar (automatically normalized across the entire chart):
|
||||
Secondly, you need to inform the delegate the height of each bar (automatically normalized across the entire chart):
|
||||
|
||||
- (NSInteger)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSInteger)index
|
||||
- (CGFloat)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSInteger)index
|
||||
{
|
||||
return ...; // height of bar at index
|
||||
}
|
||||
|
||||
#### JBLineChartView
|
||||
|
||||
Similiarily, to initialze a JBLineChartView, you only need a few lines of code:
|
||||
Similiarily, to initialize a JBLineChartView, you only need a few lines of code:
|
||||
|
||||
JBLineChartView *lineChartView = [[JBLineChartView alloc] init];
|
||||
lineChartView.delegate = self;
|
||||
@@ -89,9 +89,9 @@ At a minimum, you need to inform the data source how many points are in the line
|
||||
return ...; // number of points in chart
|
||||
}
|
||||
|
||||
Secondly, you nee to inform the delegate the y-position of each point (automatically normalized across the entire chart):
|
||||
Secondly, you need to inform the delegate the y-position of each point (automatically normalized across the entire chart):
|
||||
|
||||
- (NSInteger)lineChartView:(JBLineChartView *)lineChartView heightForIndex:(NSInteger)index
|
||||
- (CGFloat)lineChartView:(JBLineChartView *)lineChartView heightForIndex:(NSInteger)index
|
||||
{
|
||||
return ...; // y-position of poinnt at index (x-axis)
|
||||
}
|
||||
@@ -116,9 +116,9 @@ Lastly, any JBChartView subclass can be collapsed or expanded programmatically v
|
||||
|
||||
#### JBBarChartView
|
||||
|
||||
The color of a chart's bar can be customized via the <i>optional</i> protocol:
|
||||
By default, a chart's bars will be black and flat. They can be customized by supplying a UIView subclass through the <i>optional</i> protocol:
|
||||
|
||||
- (UIColor *)barColorForBarChartView:(JBBarChartView *)barChartView atIndex:(NSInteger)index
|
||||
- (UIView *)barViewForBarChartView:(JBBarChartView *)barChartView atIndex:(NSInteger)index
|
||||
{
|
||||
return ...; // color of line in chart
|
||||
}
|
||||
@@ -176,4 +176,4 @@ A JBLineChartView visuaul overview can be found <a href="https://raw.github.com/
|
||||
|
||||
## License
|
||||
|
||||
Usage is provided under the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License</a> (v2.0). See <a href="https://github.com/Jawbone/JBChartView/blob/master/LICENSE">LICENSE</a> for full details.
|
||||
Usage is provided under the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License</a> (v2.0). See <a href="https://github.com/Jawbone/JBChartView/blob/master/LICENSE">LICENSE</a> for full details.
|
||||
|
||||
Reference in New Issue
Block a user