2012-03-21 14 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 == @ "क्षैतिजलाइनफॉरएवरेज" में परिवर्तन [साजिश। पहचानकर्ता isEqual: @ "क्षैतिजलाइनफॉरएवरेज"] –

+0

@ एंड्रयूएस कंपाइलर एक ही स्ट्रिंग के संदर्भों को विशिष्ट करता है। कृपया यह उत्तर देखें: http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison –

+0

मैं इसे एरिक के दृष्टिकोण से पसंद करता हूं, क्योंकि यह प्लॉट समान रूप से व्यवहार करता है, जहां वे समान शुरुआत और अंत बिंदु साझा करते हैं। यदि ये बिंदु बदलते हैं, तो औसत रेखा का पालन किया जाएगा। –

1

हां। ग्राफ में स्कैटर प्लॉट जोड़ें और इसे दो डेटा पॉइंट दें- वांछित रेखा के प्रत्येक छोर पर। ऐसा करने का

+0

हैलो @ एरिक स्क्रोच मैं इस पंक्ति num = (NSDecimalNumber *) myDataAverageValue को समझ नहीं पा रहा हूं; क्या आप मुझे कुछ उदाहरण के साथ बताएंगे, मैं अपने डेटाएवरवैलव्यू के साथ क्या बदल सकता हूं। धन्यवाद .. – Warewolf

+0

यह प्लॉट के लिए वाई-वैल्यू है, जिसे '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"

के "एक्सिसडेमो" खंड का संदर्भ लें