2012-03-21 80 views
6

有沒有方法使用核心情節框架將平均水平線(或任何一條線)添加到條形圖圖表中?核心情節 - 具有平均水平線的條形圖

謝謝。

+0

,因爲我有問題沒有提供答案... – dorin 2012-04-12 12:24:52

+0

是的,你是對的,我看過你的問題,發現你很不幸。 – 2012-04-12 13:17:39

回答

6

一種方式是通過使用CPTScatterPlot:

添加以下幾行代碼中你已經初始化,並添加你的條形圖(或什麼都實際數據情節)到您的圖形之後。

// Before following code, initialize your data, actual data plot and add plot to graph 

CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease]; 
CPTMutableLineStyle * lineStyle      = [CPTMutableLineStyle lineStyle]; 
lineStyle.lineWidth    = 3.f; 
lineStyle.lineColor    = [CPTColor blackColor]; 
lineStyle.dashPattern   = [NSArray arrayWithObjects:[NSNumber numberWithFloat:3.0f], [NSNumber numberWithFloat:3.0f], nil]; 
dataSourceLinePlot.dataLineStyle = lineStyle; 
dataSourceLinePlot.identifier = @"horizontalLineForAverage"; 
dataSourceLinePlot.dataSource = self; 
[barChart addPlot:dataSourceLinePlot toPlotSpace:plotSpace]; 

然後添加數據源的方法,在我的情況我已在上面的代碼中自行數據源,所以我定義在同一文件中的數據源的方法:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{ 
// Note this method will return number of records for both my actual plot, and for scattered plot which is used to draw horizontal average line. For latter, this will decide the horizontal length of your line 
    return [myDataArray count]; 
} 

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

     // If method is called to fetch data about drawing horizontal average line, then return your generated average value. 
    if([email protected]"horizontalLineForAverage") 
    { 
     if(fieldEnum == CPTScatterPlotFieldX) 
     { 
        // this line will remain as it is 
      num =(NSDecimalNumber *)[NSDecimalNumber numberWithDouble:index]; 
     } 
     else 
     { 
      num = (NSDecimalNumber *) myDataAverageValue;// Here you generate average value for location of horizontal line. You should edit this line only; 
     } 
    } 
// handle other cases and return data for other plots  
    return num; 
} 
+0

您無法以這種方式比較字符串:plot.identifier == @「horizo​​ntalLineForAverage」更改爲[plot.identifier isEqual:@「horizo​​ntalLineForAverage」] – 2013-03-28 01:10:09

+0

@AndrewS編譯器將引用唯一化爲相同的字符串。請看這個答案:http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison – 2013-03-28 07:59:34

+0

我更喜歡這個埃裏克的方法,因爲它平等對待情節,他們共享相同的起點和終點。如果這些點改變,平均線將跟隨。 – 2015-02-12 12:17:59

1

是的。在圖上添加一個散點圖並給它兩個數據點 - 在所需線的每一端。這樣做的

+0

hello @Eric Skroch我不明白這一行num =(NSDecimalNumber *)myDataAverageValue;你會用一些例子告訴我,我可以用myDataAverageValue替換。謝謝.. – Warewolf 2012-08-13 05:49:22

+0

這是繪圖的y值,用'NSDecimalNumber'編碼。 – 2012-08-13 11:20:28

0
CPTFill *bandFill = [CPTFill fillWithColor:[[CPTColor blackColor] colorWithAlphaComponent:1]]; 
    [y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(800) length:CPTDecimalFromDouble(1.5)] fill:bandFill]]; 

-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate 
{ 
    if (self.segment.selectedSegmentIndex == 2) { 
     if (coordinate == CPTCoordinateY) { 

      //NSLog(@"%f=>%f",self.yRange.lengthDouble,newRange.lengthDouble); 

      CPTGraph* graph = space.graph; 
      CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 
      CPTXYAxis *y = axisSet.yAxis; 
      NSArray *bands = y.backgroundLimitBands; 
      for (CPTLimitBand *band in bands) { 
       [y removeBackgroundLimitBand:band]; 
      } 

      CPTFill *bandFill = [CPTFill fillWithColor:[[CPTColor blackColor] colorWithAlphaComponent:1]]; 
      [y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(800) length:CPTDecimalFromDouble(1.5 * newRange.lengthDouble/1200)] fill:bandFill]]; 
     } 

    } 

    return newRange; 

} 

請參考官方示例「Plot_Gallery_iOS」的「AxisDemo」部分