2012-12-17 69 views
3

有沒有人有關於將對角線的座標值設置爲對角線的想法?如何在圖表上點擊時顯示說明iOS

編輯:我成功地顯示對角線的oX軸的值。也許這可以幫助別人:

label.rotation = M_PI/4; 

另一個問題是,如果有人可以給我如何做一個描述出現在散點圖點附近,我點擊一些鏈接或教程。

這是我的散點圖代碼,我畫上圖:

-(void)initPlot { 
[self configureHost]; 
[self configureGraph]; 
[self configurePlots]; 
[self configureAxes]; 
} 

-(void)configureHost { 

self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 40 , 480, 280)]; 
self.hostView.allowPinchScaling = YES; 
[self.view addSubview:self.hostView]; 

} 

-(void)configureGraph { 

graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds]; 
[graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]]; 
self.hostView.hostedGraph = graph; 
// 2 - Set graph title 
NSString *title = @"Valori"; 
graph.title = title; 
// 3 - Create and set text style 
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle]; 
titleStyle.color = [CPTColor whiteColor]; 
titleStyle.fontName = @"Helvetica-Bold"; 
titleStyle.fontSize = 16.0f; 
graph.titleTextStyle = titleStyle; 
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop; 
graph.titleDisplacement = CGPointMake(0.0f, 10.0f); 
// 4 - Set padding for plot area 
[graph.plotAreaFrame setPaddingLeft:30.0f]; 
[graph.plotAreaFrame setPaddingBottom:30.0f]; 
// 5 - Enable user interactions for plot space 
plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 
plotSpace.allowsUserInteraction = YES; 

} 

-(void)configurePlots { 

// 1 - Get graph and plot space 
graph = self.hostView.hostedGraph; 
plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 
// 2 - Create the three plots 

aaplPlot = [[CPTScatterPlot alloc] init]; 
aaplPlot.dataSource = self; 
aaplPlot.identifier = CPDTickerSymbolAAPL; 
CPTColor *aaplColor = [CPTColor redColor]; 
[graph addPlot:aaplPlot toPlotSpace:plotSpace]; 


msftPlot = [[CPTScatterPlot alloc] init]; 
msftPlot.dataSource = self; 
msftPlot.identifier = CPDTickerSymbolMSFT; 
CPTColor *msftColor = [CPTColor clearColor]; 
[graph addPlot:msftPlot toPlotSpace:plotSpace]; 
// 3 - Set up plot space 

NSArray *array = [[NSArray alloc] initWithObjects:aaplPlot, msftPlot, nil]; 
[plotSpace scaleToFitPlots:array]; 

CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy]; 
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)]; 
plotSpace.xRange = xRange; 
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy]; 
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)]; 
plotSpace.yRange = yRange; 
// 4 - Create styles and symbols 
CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy]; 
aaplLineStyle.lineWidth = 2.5; 
aaplLineStyle.lineColor = aaplColor; 
aaplPlot.dataLineStyle = aaplLineStyle; 
CPTMutableLineStyle *aaplSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
aaplSymbolLineStyle.lineColor = aaplColor; 
CPTPlotSymbol *aaplSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
aaplSymbol.fill = [CPTFill fillWithColor:aaplColor]; 
aaplSymbol.lineStyle = aaplSymbolLineStyle; 
aaplSymbol.size = CGSizeMake(6.0f, 6.0f); 
aaplPlot.plotSymbol = aaplSymbol; 


CPTMutableLineStyle *msftLineStyle = [msftPlot.dataLineStyle mutableCopy]; 
msftLineStyle.lineWidth = 2.0; 
msftLineStyle.lineColor = msftColor; 
msftPlot.dataLineStyle = msftLineStyle; 
CPTMutableLineStyle *msftSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
msftSymbolLineStyle.lineColor = msftColor; 
CPTPlotSymbol *msftSymbol = [CPTPlotSymbol diamondPlotSymbol]; 
msftSymbol.fill = [CPTFill fillWithColor:msftColor]; 
msftSymbol.lineStyle = msftSymbolLineStyle; 
msftSymbol.size = CGSizeMake(6.0f, 6.0f); 
msftPlot.plotSymbol = msftSymbol; 

} 

-(void)configureAxes { 

// 1 - Create styles 
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle]; 
axisTitleStyle.color = [CPTColor whiteColor]; 
axisTitleStyle.fontName = @"Helvetica-Bold"; 
axisTitleStyle.fontSize = 12.0f; 
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle]; 
axisLineStyle.lineWidth = 2.0f; 
axisLineStyle.lineColor = [CPTColor whiteColor]; 
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init]; 
axisTextStyle.color = [CPTColor whiteColor]; 
axisTextStyle.fontName = @"Helvetica-Bold"; 
axisTextStyle.fontSize = 11.0f; 
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle]; 
tickLineStyle.lineColor = [CPTColor whiteColor]; 
tickLineStyle.lineWidth = 2.0f; 
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle]; 
tickLineStyle.lineColor = [CPTColor blackColor]; 
tickLineStyle.lineWidth = 1.0f; 
// 2 - Get axis set 
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet; 
// 3 - Configure x-axis 
CPTAxis *x = axisSet.xAxis; 
x.title = @"Ziua Lunii"; 
x.titleTextStyle = axisTitleStyle; 
x.titleOffset = 15.0f; 
x.axisLineStyle = axisLineStyle; 
x.labelingPolicy = CPTAxisLabelingPolicyNone; 
x.labelTextStyle = axisTextStyle; 
x.majorTickLineStyle = axisLineStyle; 
x.majorTickLength = 4.0f; 
x.tickDirection = CPTSignNegative; 
CGFloat dateCount = [[[CPDStockPriceStore sharedInstance] datesInMonth] count]; 
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount]; 
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount]; 
NSInteger i = 0; 
for (NSString *date in [[CPDStockPriceStore sharedInstance] datesInMonth]) { 
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date textStyle:x.labelTextStyle]; 
    CGFloat location = i++; 
    label.tickLocation = CPTDecimalFromCGFloat(location); 
    label.offset = x.majorTickLength; 
    if (label) { 
     [xLabels addObject:label]; 
     [xLocations addObject:[NSNumber numberWithFloat:location]]; 
    } 
} 
x.axisLabels = xLabels; 
x.majorTickLocations = xLocations; 
// 4 - Configure y-axis 
CPTAxis *y = axisSet.yAxis; 
y.title = @"Pret"; 
y.titleTextStyle = axisTitleStyle; 
y.titleOffset = -50.0f; 
y.axisLineStyle = axisLineStyle; 
y.majorGridLineStyle = gridLineStyle; 
y.labelingPolicy = CPTAxisLabelingPolicyNone; 
y.labelTextStyle = axisTextStyle; 
y.labelOffset = 23.0f; 
y.majorTickLineStyle = axisLineStyle; 
y.majorTickLength = 4.0f; 
y.minorTickLength = 2.0f; 
y.tickDirection = CPTSignPositive; 
NSInteger majorIncrement = 100; 
NSInteger minorIncrement = 50; 
CGFloat yMax = 1300.0f; // should determine dynamically based on max price 
NSMutableSet *yLabels = [NSMutableSet set]; 
NSMutableSet *yMajorLocations = [NSMutableSet set]; 
NSMutableSet *yMinorLocations = [NSMutableSet set]; 
for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) { 
    NSUInteger mod = j % majorIncrement; 
    if (mod == 0) { 
     CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle]; 
     NSDecimal location = CPTDecimalFromInteger(j); 
     label.tickLocation = location; 
     label.offset = - y.majorTickLength - y.labelOffset; 
     if (label) { 
      [yLabels addObject:label]; 
     } 
     [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]]; 
    } else { 
     [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]]; 
    } 
} 
y.axisLabels = yLabels; 
y.majorTickLocations = yMajorLocations; 
y.minorTickLocations = yMinorLocations; 

} 

回答

0

您可以使用-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index用於此目的,如examples of Core Plot

+0

幾個核心繪圖示例應用程序的證明演示如何做到這一點,包括* Plot Gallery *應用程序中的三個散點圖。 –

+0

@tilo謝謝你! –

相關問題