2012-03-21 21 views
6

コアプロットフレームワークを使用して棒グラフに平均水平線(または任意の単線)を追加する方法はありますか?コアプロット - 平均水平線の棒グラフ

ありがとうございました。

+0

私は答えが提供されていない質問があるので... – dorin

+0

あなたは正しいです、私はあなたの質問を見て、あなたが非常に不幸に思った。 –

答えて

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"]に変更します。 –

+0

@AndrewSコンパイラは同じ文字列への参照を一意にします。この回答を参照してください:http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison –

+0

私は同じ開始点と終了点を共有するプロットを等しく扱うので、これはEricのアプローチに比べて好きです。これらの点が変化すると、平均線が続きます。 –

1

はい。グラフに散布図を追加し、2つのデータ点、つまり目的の線の両端に1つのデータ点を与えます。これを行うための

+0

こんにちは@Eric Skroch私はこの行を理解していないnum =(NSDecimalNumber *)myDataAverageValue;私はいくつかの例を教えてくれるでしょう、私はmyDataAverageValueで置き換えることができます。ありがとう.. – Warewolf

+0

プロットのY値で、NSDecimalNumberでエンコードされています。 –

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"セクションを参照してください

関連する問題