回答
一種方式是通過使用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;
}
您無法以這種方式比較字符串:plot.identifier == @「horizontalLineForAverage」更改爲[plot.identifier isEqual:@「horizontalLineForAverage」] – 2013-03-28 01:10:09
@AndrewS編譯器將引用唯一化爲相同的字符串。請看這個答案:http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison – 2013-03-28 07:59:34
我更喜歡這個埃裏克的方法,因爲它平等對待情節,他們共享相同的起點和終點。如果這些點改變,平均線將跟隨。 – 2015-02-12 12:17:59
是的。在圖上添加一個散點圖並給它兩個數據點 - 在所需線的每一端。這樣做的
hello @Eric Skroch我不明白這一行num =(NSDecimalNumber *)myDataAverageValue;你會用一些例子告訴我,我可以用myDataAverageValue替換。謝謝.. – Warewolf 2012-08-13 05:49:22
這是繪圖的y值,用'NSDecimalNumber'編碼。 – 2012-08-13 11:20:28
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」部分
- 1. 水平條形圖上的核心散點圖散點圖
- 2. 水平條形圖
- 3. 核心圖中的iOS水平條形圖顯示兩個值
- 4. 將UIButtons對齊到水平核心圖的條形圖CPTBarPlot
- 5. ggplot2的水平條形圖
- 6. 刪除水平條形圖中的頂部水平線(Chart.js 2)
- 7. 水平條形圖標籤
- 8. Plotly.js - 劇情倒轉水平條形圖
- 9. 核心情節條形圖動畫
- 10. 顯示值 - jQuery的情節水平堆積條形圖
- 11. D3水平堆積條形圖線
- 12. matlab:二維線情節水平線
- 13. Qt:3D水平條形圖
- 14. D3水平條形圖
- 15. morris.js水平條形圖
- 16. 水平顯示條形圖
- 17. 核心情節alternatingBandFills「跳躍」水平滾動
- 18. 水平/垂直線的情節
- 19. ggplot添加兩個平均線情節
- 20. Matplotlib情節只有水平線步驟情節
- 21. 如何獲得具有核心數據的列的平均值?
- 22. 具有facet_grid,free_x不起作用的水平條形圖
- 23. 具有雙色表示的水平單條形圖
- 24. 僅允許在覈心圖條形圖中進行水平滾動?
- 25. SQL的平均水平
- 26. 具有兩條紅色水平線的Intellij圖標
- 27. Ruby on Rails平均水平?
- 28. 分頁太多的水平條形圖
- 29. 情節水平系列
- 30. Excel - 條形圖與每個條上的水平參考線
,因爲我有問題沒有提供答案... – dorin 2012-04-12 12:24:52
是的,你是對的,我看過你的問題,發現你很不幸。 – 2012-04-12 13:17:39