2012-07-11 61 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