2013-07-19 5 views
1

Я использую диаграммы с использованием основного графика. Но с последних трех дней я не реализую графы Grouped в iOS. Сгруппированные гистограммы представляют собой несколько гистограмм по оси X.Как построить сгруппированную гистограмму в основном графике?

- (void)generateLayout 
{ 
    graph        = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; 
    [graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];// YOU CAN CHANGE THE THEME COLOR FROM HERE........ 
    self.hostedGraph     = graph; 
    graph.plotAreaFrame.masksToBorder = NO; 
    graph.paddingLeft     = 10.0f; 
    graph.paddingTop     = 210.0f;  // ALTER THE SIZE OF GRAPH FROM TOP POSITION........ 
    graph.paddingRight     = 80.0f; 
    graph.paddingBottom     = 90.0f; 

    CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle]; 
    borderLineStyle.lineColor    = [CPTColor blackColor];    // COLOR THE BOUNDARY OF THE GRAPH.... 
    borderLineStyle.lineWidth    = 2.0f; 



    // GRAPH STYLE/SHAPE CHANGE IN X OR Y COORDINATE........... 
    graph.plotAreaFrame.borderLineStyle  = borderLineStyle; 
    graph.plotAreaFrame.paddingTop   = 10.0; 
    graph.plotAreaFrame.paddingRight  = 10.0; 
    graph.plotAreaFrame.paddingBottom  = 80.0; 
    graph.plotAreaFrame.paddingLeft   = 70.0; 


    plotSpace.allowsUserInteraction=YES; 
    plotSpace.globalXRange   = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(-1) 
                    length:CPTDecimalFromInt([dates count]+3)]; 


    plotSpace.delegate    = self; 
    NSLog(@"set count=======%d",sets.count);// 6 
    plotSpace.yRange    = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) 
                    length:CPTDecimalFromInt(200)]; 
    plotSpace.xRange    = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(-1)// IT WILL TAKE X TO AND FROM 0 OR OTHER COORDIANTE..... 
                    length:CPTDecimalFromInt(7)]; 

    CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle]; 
    majorGridLineStyle.lineWidth   = 2.75; 
    majorGridLineStyle.lineColor   = [[CPTColor blackColor] colorWithAlphaComponent:0.5]; 
    CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle]; 
    minorGridLineStyle.lineWidth   = 2.25; 
    minorGridLineStyle.lineColor   = [[CPTColor blackColor] colorWithAlphaComponent:0.5]; 

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 




    CPTXYAxis *x     = axisSet.xAxis; 

    x.orthogonalCoordinateDecimal = CPTDecimalFromInt(0); 
    x.majorIntervalLength   = CPTDecimalFromInt(1);//majorIntervalLength defines the number of units between “big” ticks on the axis. In this case it’s set to show one every 10 units. 
    x.minorTicksPerInterval   = 0; 
    x.labelingPolicy    = CPTAxisLabelingPolicyNone; 
    x.majorGridLineStyle   = majorGridLineStyle; 
    x.axisConstraints    = [CPTConstraints constraintWithLowerOffset:0.0]; 



    //X labels 
    int labelLocations = 0; 
    NSMutableArray *customXLabels = [NSMutableArray array]; 
    for (NSString *day in dates) 
    { 
     CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:day textStyle:x.labelTextStyle]; 
     newLabel.tickLocation = [[NSNumber numberWithInt:labelLocations] decimalValue]; 
     newLabel.offset   = x.labelOffset + x.majorTickLength; 
     [customXLabels addObject:newLabel]; 
     labelLocations++; 
     [newLabel release]; 
    } 
    x.axisLabels     = [NSSet setWithArray:customXLabels]; 

    CPTXYAxis *y   = axisSet.yAxis; 
    y.title     = @"Calories burnt (kcal)"; 
    y.titleOffset   = 50.0f;       //SET THE X-AXIS FOR THE LABEL(CALORIES BURNT) 
    y.labelingPolicy  = CPTAxisLabelingPolicyAutomatic; 
    y.majorGridLineStyle = majorGridLineStyle; 
    y.minorGridLineStyle = minorGridLineStyle; 
    y.axisConstraints  = [CPTConstraints constraintWithLowerOffset:0.0]; 
    y.orthogonalCoordinateDecimal=CPTDecimalFromFloat(0.0f); 


    CPTXYAxis *y2 = (CPTXYAxis *)[[CPTXYAxis alloc] initWithFrame:CGRectZero]; 
    CPTXYPlotSpace *plotSpace2 = [[CPTXYPlotSpace alloc] init]; 
    plotSpace2.xRange = plotSpace.xRange; 
    plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(60)]; 
    [graph addPlotSpace:plotSpace2]; 
    y2.plotSpace = plotSpace2; 
    y2.coordinate = CPTCoordinateY; 
    y2.orthogonalCoordinateDecimal = CPTDecimalFromFloat(6.2); 
    y2.majorIntervalLength = CPTDecimalFromFloat(5.0f); 
    y2.minorTicksPerInterval  = 0; 

    y2.title      = @"Temperature"; 
    y2.titleLocation    = CPTDecimalFromInteger(30.0); //THIS IS USED TO SET THE TITLE OF THE SECOND Y-AXIS.. 
    y2.titleTextStyle    =x.titleTextStyle; 
    y2.titleOffset     =-35.0f; 

    graph.axisSet.axes = [NSArray arrayWithObjects :x,y,y2,nil]; 

    CPTMutableLineStyle *barLineStyle = [[[CPTMutableLineStyle alloc] init] autorelease]; 
    barLineStyle.lineWidth    = 1.0; 
    barLineStyle.lineColor    = [CPTColor clearColor]; 
    CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle]; 
    whiteTextStyle.color    = [CPTColor whiteColor]; 



    for (NSString *set in [[sets allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]) 
    { 
     CPTBarPlot *plot  = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO]; 
     plot.lineStyle   = barLineStyle; 
     CGColorRef color  = ((UIColor *)[sets objectForKey:set]).CGColor; 
     plot.fill    = [CPTFill fillWithColor:[CPTColor colorWithCGColor:color]]; 
     if (firstPlot) 
     { 
      plot.barBasesVary = NO; 
      firstPlot   = NO; 
     } 
     else 
     { 
      plot.barBasesVary = YES; 
     } 
     plot.barWidth   = CPTDecimalFromFloat(0.5f); 
     plot.barsAreHorizontal = NO; 
     plot.dataSource   = self; 
     plot.identifier   = set; 
     [graph addPlot:plot toPlotSpace:plotSpace]; 
    } 


    CPTLegend *theLegend  = [CPTLegend legendWithGraph:graph]; 
    theLegend.numberOfRows = sets.count; 
    theLegend.fill   = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]]; 
    theLegend.borderLineStyle = barLineStyle; 
    theLegend.cornerRadius = 10.0; 
    theLegend.swatchSize  = CGSizeMake(15.0, 15.0); 
    whiteTextStyle.fontSize = 18.0; 
    theLegend.textStyle  = whiteTextStyle; 
    theLegend.rowMargin  = 5.0; 
    theLegend.paddingLeft  = 10.0; 
    theLegend.paddingTop  = 10.0; 
    theLegend.paddingRight = 10.0; 
    theLegend.paddingBottom = 10.0; 
    graph.legend    = theLegend; 
    graph.legendAnchor  = CPTRectAnchorTopLeft; 
    graph.legendDisplacement = CGPointMake(80.0, -200.0); 


} 
+0

Вы хотите, чтобы укладывать графики один поверх другого, или вы хотите, чтобы различные графики были сгруппированы по базовой линии, например, 123 123 123? –

+0

Мне нужны разные сюжеты, сгруппированные по базовой линии, точно так же, как, например, 123 123 123? –

ответ

0

Используйте свойство barOffset для смещения участков с данного места расположения. Например, с тремя графиками вы можете дать на первом графике смещение -0,2, второе - смещение нуля (по умолчанию), а третье - смещение +0.2, предполагая, что ваши бары являются единичными единицами.

Смежные вопросы