網頁

2012年6月26日 星期二

Core-Plot


How To Draw Graphs with Core Plot, Part 1




一個統計圖的Open Source Core-plot,
http://code.google.com/p/core-plot/ 

提供了折線圖、pie chart和bar chart等等...

如何使用這個open source
首先先把core-plot download下來:

1. 把Binaries >IOS內的 CorePlotHeaders 資料夾和libCorePlot-CocoaTouch.a拉近Project內,

2. 把內建的QuartzCore.framework加入project



3. 在Project Bulild Settings裡面,Other Linker Flags內加入"-ObjC",如果是非Xcode4.2的版本則要加入"-all_load"



若無執行步驟3 會出現以下錯誤訊息

PS.教學範例範例中,若想報表可以重復變化 要將填入data時的static拿掉

    //static
     NSArray *prices = nil;



How To Draw Graphs with Core Plot, Part 2

拉UIView建關係時 custom class記得改成克制的name (預設UIView)
.m IBOutlet CPTGraphHostingView *hostView; 的hostView才會跑出來

    
-(void)configureChart {    
    
    // 1 - Get reference to graph
 CPTGraph *graph = self.hostView.hostedGraph;   
  // 2 - Create chart
 CPTPieChart *pieChart = [[CPTPieChart alloc] init];
 pieChart.dataSource = self;
 pieChart.delegate = self;
 pieChart.pieRadius = (self.hostView.bounds.size.height * 0.85) / 2; //0.7 
 pieChart.identifier = graph.title;
 pieChart.startAngle = M_PI_4;
 pieChart.sliceDirection = CPTPieDirectionClockwise;    

    pieChart.labelOffset = -150; //<-- Display label on pie chart 
...
}

在Bar上add images

Place an image behind my labels and in front of my plot


    
-(void)barPlot:(CPTBarPlot *)plot :(NSUInteger)index
{


 // 3 - Create annotation, if necessary

 if (!self.priceAnnotation) {
  NSNumber *x = [NSNumber numberWithInt:0];
  NSNumber *y = [NSNumber numberWithInt:0];
  NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
  self.priceAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:plot.plotSpace anchorPlotPoint:anchorPoint];    
        
        
        NSLog(@"self.priceAnnotation =%@",self.priceAnnotation);
    
 }

    
    //add
    
    CGRect imageRect = CGRectMake(0.0, 0.0, 130, 77);
    CPTLayer *imagebgLayer = [[CPTLayer alloc] initWithFrame:imageRect];
    imagebgLayer.backgroundColor = [UIColor clearColor].CGColor; 
    
    self.priceAnnotation.contentLayer =imagebgLayer;
    

    CPTAnnotationHostLayer *newHostLayer = [[CPTAnnotationHostLayer alloc]
                                            initWithFrame:CGRectMake(0, 0, 129, 70)];
    UIImage * annotationBG = [UIImage imageNamed:@"content_pass.png"];    
    newHostLayer.backgroundColor = [UIColor colorWithPatternImage:annotationBG].CGColor;
    [imagebgLayer  addSublayer: newHostLayer];
    
  
 // 6 - Get plot index based on identifier
 NSInteger plotIndex = 1; 
   

 // 7 - Get the anchor point for annotation
    
    int  indexi=1;
    
    CGFloat x =  indexi -0.06f +  CPDBarInitialX + (plotIndex * CPDBarWidth) ; 
    NSNumber *anchorX = [NSNumber numberWithFloat:x];    
    
    CGFloat y =  5.0f;
 NSNumber *anchorY = [NSNumber numberWithFloat:y]; 
    


    
    self.priceAnnotation.anchorPlotPoint = [NSArray arrayWithObjects:anchorX, anchorY, nil]; 
        
        // 8 - Add the annotation 
    [plot.graph.plotAreaFrame.plotArea addAnnotation:self.priceAnnotation]; 
        

    [viewHistoryScroll addSubview:self.hostView ];
    
    [self.view addSubview:viewHistoryScroll];
 
   

}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。