核心图 - 平均水平线条形图

时间:2012-03-21 10:20:49

标签: ios graph core-plot bar-chart

有没有办法使用核心图框架将平均水平线(或任何单线)添加到条形图图形中?

感谢。

3 个答案:

答案 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];

然后添加数据源方法,在我的情况下,我已将上面代码中的数据源设置为self,所以我在同一个文件中定义数据源方法:

-(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( plot.identifier==@"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;
}

答案 1 :(得分:1)

是。将散点图添加到图形中,并为其提供两个数据点 - 在所需行的每一端一个。

答案 2 :(得分: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”部分

相关问题