วันศุกร์ที่ 11 กุมภาพันธ์ พ.ศ. 2554

Create Chart with CorePlot (BarChart)

การสร้าง BarChart ด้วย CorePlot 

เริ่มจากการสร้าง Project ผมใช้ CorePlotBarChart
ทำการ Add Framework ดูวิธีจาก CorePlotPieChart นะครับ

สร้าง BarChartViewController
ใน BarChartViewController.h

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@interface BarChartViewController : UIViewController <CPPlotDataSource, CPBarPlotDelegate , CPBarPlotDataSource> {
IBOutlet CPGraphHostingView *_barChartView;
CPXYGraph *_barChart;
NSMutableArray *_dataForBarChart;
}

-(void)constructBarChart;
-(void)genData;

@end



ใน BarChartViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
_barChartView = [[CPGraphHostingView alloc] initWithFrame:CGRectMake(0, 20, 350, 500)];

[self.view addSubview:_barChartView];
[self genData];
[self constructBarChart];
}



-(void)genData{
_dataForBarChart = [[NSMutableArray alloc] init];
for (int i = 0; i < 5; ++i) {
[_dataForBarChart addObject:[NSNumber numberWithInteger:10*i*i+50]];
}
}



-(void)constructBarChart{
//Create Bar Chart
    _barChart = [[CPXYGraph alloc] initWithFrame:CGRectZero];
//Select Theme
CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
    [_barChart applyTheme:theme];
    _barChartView.hostedGraph = _barChart;
    _barChart.plotAreaFrame.masksToBorder = NO;
    _barChart.paddingLeft = 70.0;
_barChart.paddingTop = 20.0;
_barChart.paddingRight = 20.0;
_barChart.paddingBottom = 80.0;
//Init Range of X axis (0 - 10) and Y axis (0 - 300)
    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)_barChart.defaultPlotSpace;
    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(300.0f)];
    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(10.0f)];
    //Create X axis 
CPXYAxisSet *axisSet = (CPXYAxisSet *)_barChart.axisSet;
    CPXYAxis *x = axisSet.xAxis;
    x.axisLineStyle = nil;
    x.majorTickLineStyle = nil;
    x.minorTickLineStyle = nil;
    x.majorIntervalLength = CPDecimalFromString(@"5");
//Position of X Label on Y axis
    x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
x.title = @"X Axis";
    x.titleLocation = CPDecimalFromFloat(5.0f);
x.titleOffset = 55.0f;
//Create Label for X axis
x.labelRotation = M_PI/4;
x.labelingPolicy = CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:0], [NSDecimalNumber numberWithInt:1], 
[NSDecimalNumber numberWithInt:2], [NSDecimalNumber numberWithInt:3], [NSDecimalNumber numberWithInt:4], nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength;
newLabel.rotation = M_PI/4;
[customLabels addObject:newLabel];
[newLabel release];
}
x.axisLabels =  [NSSet setWithArray:customLabels];
//Create Y axis
CPXYAxis *y = axisSet.yAxis;
    y.axisLineStyle = nil;
    y.majorTickLineStyle = nil;
    y.minorTickLineStyle = nil;
    y.majorIntervalLength = CPDecimalFromString(@"50");
//Positionof Y Label on X axis
    y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
y.title = @"Y Axis";
y.titleOffset = 45.0f;
    y.titleLocation = CPDecimalFromFloat(150.0f);
//Create Plot
    CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor darkGrayColor] horizontalBars:NO];
    barPlot.baseValue = CPDecimalFromString(@"0");
    barPlot.dataSource = self;
    barPlot.barOffset = 0.25;
    barPlot.identifier = @"Bar Plot";
    [_barChart addPlot:barPlot toPlotSpace:plotSpace];
}



Delegate and DataSource

-(void)barPlot:(CPBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index{
NSLog(@"Select At Record Index %d", index);
}

-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{
return [_dataForBarChart count];
}

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSDecimalNumber *num = nil;

switch ( fieldEnum ) {
//Position of Plot
case CPBarPlotFieldBarLocation:
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
break;
//Height of Plot
case CPBarPlotFieldBarLength:
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:[[_dataForBarChart objectAtIndex:index] intValue]];
break;
}
return num;
}



ใน CorePlotBarChartAppDelegate.h


#import <UIKit/UIKit.h>
#import "BarChartViewController.h"

@interface CorePlotBarChartAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
BarChartViewController *_barChartViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end



ใน CorePlotBarChartAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    
_barChartViewController = [[BarChartViewController alloc] init];
[window addSubview:_barChartViewController.view];
    [self.window makeKeyAndVisible];
    
    return YES;
}



Build & Run

วันอาทิตย์ที่ 6 กุมภาพันธ์ พ.ศ. 2554

How to open .doc .xls .ppt in iPhone

ถ้าเราต้องการเขียนโปรแกรมเปิด .doc .xls .ppt ใร iPhone
เราสามารถใช้ UIWebView ได้ครับ

ก็เริ่มจากสร้าง Project ใหม่ (ผมใช้ชื่อ OpenDoc)
นำ File .doc ที่ต้องการเปิดใส่ไว้ใน Project (ผมใช้ sample.doc)


ใน OpenDocAppDelegate.m


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;


เพิ่ม Code สร้าง UIWebView


UIWebView *_webView = [[UIWebView alloc] initWithFrame:window.frame];


กำหนด Path ของ File ที่ต้องการเปิดและ Add Subview ลงใน Window


NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample.doc" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
[window addSubview:_webView];



Build & Run