2012-07-11 65 views
1

大家好 有人可以帮我解决这个问题我也碰到过动态设置栏中的条形图宽度

我可以积与不平等条宽意义coreplote条形图说我的X轴间隔1个单位,我已经将条形图宽度设置为相同的1个单位。但是有一些点的x轴值在0.5单位变化,我希望条宽等于发生变化的宽度。数据正在动态变化。有没有其他解决方案可以使用,而且看起来仍然像条形图。使用栏的唯一原因是我需要填写剧情空间。

CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)barChart.defaultPlotSpace; 
     plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromInt(0) 
                 length:CPDecimalFromInt(rangeY)]; 
     plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-0.5f) 
                 length:CPDecimalFromFloat(rangeX)]; 

     CPXYAxisSet *axisSet = (CPXYAxisSet *)barChart.axisSet; 
     CPXYAxis *x = axisSet.xAxis; 
     x.axisLineStyle = nil; 
     x.majorTickLineStyle = nil; 
     x.minorTickLineStyle = nil; 
     x.majorIntervalLength = CPDecimalFromString(@"1"); 
     x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0"); 

     x.title = [[[tempCol colName] componentsSeparatedByString:@"*@"] objectAtIndex:0]; 
     x.titleLocation = CPDecimalFromFloat((rangeX-1)/2); 
     x.titleOffset = 25.0f; 

     // Define some custom labels for the data elements 
     x.labelRotation = 0; 
     x.labelingPolicy = CPAxisLabelingPolicyNone; 
     NSMutableArray *customTickLocations = [[NSMutableArray alloc] init]; 
     NSMutableArray *xAxisLabels = [[NSMutableArray alloc] init]; 
     for (int i = 0; i < [tempGraph.d2pArray count]; i++) { 
      tempD2P = [tempGraph.d2pArray objectAtIndex:i]; 
      [customTickLocations addObject:[NSNumber numberWithInt:i]]; 
      [xAxisLabels addObject:[tempD2P d2pName]]; 
     } 
     NSUInteger labelLocation = 0; 
     NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]]; 
     for (NSNumber *tickLocation in customTickLocations) { 
      CPAxisLabel *newLabel = [[CPAxisLabel alloc] 
            initWithText:[xAxisLabels objectAtIndex:labelLocation++] 
            textStyle:x.labelTextStyle]; 
      newLabel.tickLocation = [tickLocation decimalValue]; 
      newLabel.offset = 0; 
      newLabel.rotation = 0; 
      newLabel.alignment = CPAlignmentCenter;  
      [customLabels addObject:newLabel]; 
      [newLabel release]; 
      newLabel = nil; 
     } 
     [xAxisLabels release]; 
     xAxisLabels = nil; 
     [customTickLocations release]; 
     customTickLocations = nil; 
     x.axisLabels = [NSSet setWithArray:customLabels]; 

     CPXYAxis *y = axisSet.yAxis; 
     CPLineStyle *majorGridLineStyle = [CPLineStyle lineStyle]; 
     majorGridLineStyle.lineWidth = 0.75; 
     majorGridLineStyle.lineColor = [CPColor colorWithCGColor:[[UIColor grayColor] CGColor]]; 
     y.axisLineStyle = nil; 
     y.majorTickLineStyle = nil; 
     y.majorGridLineStyle = majorGridLineStyle; 
     y.minorTickLineStyle = nil; 
     y.majorIntervalLength = CPDecimalFromInt(rangeY/8); 
     y.orthogonalCoordinateDecimal = CPDecimalFromString(@"-0.5"); 
     y.title = [[[tempCol colName] componentsSeparatedByString:@"*@"] objectAtIndex:1]; 
     y.titleOffset = 30 + 5*log10(mult); 
     y.titleLocation = CPDecimalFromInt(rangeY/2); 
     NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
     [formatter setMaximumFractionDigits:0]; 
     y.labelFormatter = formatter; 
     [formatter release]; 
     formatter = nil; 
     char firstChar = 'F'; 
     CPBarPlot *barPlot; 
     // First bar plot 
     for (int i = 0; i < noOfBarCharts; i++) { 
      static CPTextStyle *whiteText = nil; 
      if (!whiteText) { 
       whiteText = [[CPTextStyle alloc] init]; 
       whiteText.color = [CPColor whiteColor]; 
       whiteText.fontSize = 14; 
      } 
      barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor colorWithComponentRed:[[[rgbArray objectAtIndex:0] objectAtIndex:i] floatValue]/255.0 
                        green:[[[rgbArray objectAtIndex:1] objectAtIndex:i] floatValue]/255.0 
                        blue:[[[rgbArray objectAtIndex:2] objectAtIndex:i] floatValue]/255.0 
                        alpha:1.0] 
              horizontalBars:NO]; 
      barPlot.baseValue = CPDecimalFromString(@"0"); 
      barPlot.dataSource = self; 
      barPlot.barOffset = ([tempGraph.graType rangeOfString:@"GROUPED"].location != NSNotFound)?(i+0.5)-(noOfBarCharts/2):0; 
      barPlot.labelOffset = 0; 
      barPlot.barWidth = 20; 
      barPlot.identifier = [NSString stringWithFormat:@"%d%c", [tempGraph.graID intValue], (firstChar - noOfBarCharts +1 + i)]; 
      barPlot.delegate = self; 




      [barChart addPlot:barPlot 
        toPlotSpace:plotSpace]; 
+0

添加您的阴谋代码 – Dustin 2012-07-11 16:26:39

+0

即时无法上传我的图应该看起来像什么图片@DustinRowland – Kashyap 2012-07-11 16:43:37

+0

当您发生更改时,您是否尝试过设置'barWidth'等于'majorIntervalLength'?看起来你只需要监控你的数据,并且每当你的时间间隔发生变化时,就相应地改变'barWidth'。 – Dustin 2012-07-11 16:49:33

回答

1

我想通了我自己的,而不是用柱状图我用步图中的散点图的伎俩:

Plot.interpolation = CPTScatterPlotInterpolationStepped; 

,并充满了阴谋:

Plot.areaFill = [CPTFill fillWithColor:[CPTColor blueColor]]; 
Plot.areaBaseValue = CPTDecimalFromInteger(0); 
+0

你能为我提供这样的代码示例吗?我处于同样的情况。 – Bhavesh 2014-11-13 06:47:51

0

如果您不在条上放置边框线,可以将条宽设置为条间距的一半,然后将宽条放大一倍。

+0

我很抱歉,但我明白你能解释一个示例代码 – Kashyap 2012-07-13 14:46:45