2014-09-30 83 views
2

我正在繪製條形圖上的2個數據集,我希望它們像直方圖,所以任何人都可以儘快指導我解決問題。核心圖:在條形圖中減少條形圖之間的距離ios

我有1數據集的2數據集(谷歌和蘋果)。我沒有得到1數據集的酒吧之間的任何空間,但我得到2太空間數據集太多。

這裏是我的代碼,

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{ 
    return [[[CPDStockPriceStore sharedInstance] datesInWeek] count]; 
} 

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{ 
    if ((fieldEnum == CPTBarPlotFieldBarTip) && (index < [[[CPDStockPriceStore sharedInstance] datesInWeek] count])) 
    { 
     if ([plot.identifier isEqual:CPDTickerSymbolAAPL]) 
     { 
      NSLog(@"apple index:%@",[[[CPDStockPriceStore sharedInstance] weeklyPrices:CPDTickerSymbolAAPL] objectAtIndex:index]); 
      return [[[CPDStockPriceStore sharedInstance] weeklyPrices:CPDTickerSymbolAAPL] objectAtIndex:index]; 
     } 
     else if ([plot.identifier isEqual:CPDTickerSymbolGOOG]) 
     { 
      NSLog(@"google index:%@",[[[CPDStockPriceStore sharedInstance] weeklyPrices:CPDTickerSymbolGOOG] objectAtIndex:index]); 
      return [[[CPDStockPriceStore sharedInstance] weeklyPrices:CPDTickerSymbolGOOG] objectAtIndex:index]; 
     } 
    } 
    return [NSDecimalNumber numberWithUnsignedInteger:index]; 



-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self initPlot]; 
} 

#pragma mark - Chart behavior 
-(void)initPlot 
{ 
    self.hostView.allowPinchScaling = YES; 
    [self configureGraph]; 
    [self configurePlots]; 
    [self configureAxes]; 
    graphData=[[CPDStockPriceStore sharedInstance] weeklyPrices:CPDTickerSymbolAAPL]; 
    NSLog(@"google:%@",graphData); 

} 

-(void)configureGraph { 
    // 1 - Create the graph 
    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds]; 

    graph.plotAreaFrame.masksToBorder = NO; 
    self.hostView.hostedGraph = graph; 
    // 2 - Configure the graph 
    [graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]]; 
    graph.paddingBottom = 30.0f; 
    graph.paddingLeft = 30.0f; 
    graph.paddingTop = -1.0f; 
    graph.paddingRight = -5.0f; 
    CGFloat xMin = 0.0f; 
    CGFloat xMax = [[[CPDStockPriceStore sharedInstance] datesInWeek] count]; 
    CGFloat yMin = 0.0f; 
    CGFloat yMax = 800.0f; // should determine dynamically based on max price 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin) length:CPTDecimalFromFloat(xMax)]; 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax)]; 

} 

-(void)configurePlots 
{ 
    // 1 - Set up the three plots 
    self.aaplPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor colorWithComponentRed:105.0f/255.0f green:252.0f/255.0f blue:144.0f/255.0f alpha:1.0] horizontalBars:NO]; 
    self.aaplPlot.identifier = CPDTickerSymbolAAPL; 

    self.googPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor colorWithComponentRed:103.0f/255.0f green:103.0f/255.0f blue:103.0f/255.0f alpha:1.0] horizontalBars:NO]; 
    self.googPlot.identifier = CPDTickerSymbolGOOG; 

    // 2 - Set up line style 
    CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init]; 
    barLineStyle.lineColor = [CPTColor lightGrayColor]; 
    barLineStyle.lineWidth = 0.5; 

    // 3 - Add plots to graph 
    CPTGraph *graph = self.hostView.hostedGraph; 
    CGFloat barX = CPDBarInitialX; 
    NSArray *plots =[NSArray arrayWithObjects:self.aaplPlot, self.googPlot, nil]; 
    for (CPTBarPlot *plot in plots) 
    { 
     plot.dataSource = self; 
     plot.delegate = self; 
     plot.barWidth = CPTDecimalFromDouble(CPDBarWidth); 
     plot.barOffset = CPTDecimalFromDouble(barX); 
     plot.lineStyle = barLineStyle; 
     [graph addPlot:plot toPlotSpace:graph.defaultPlotSpace]; 
     barX += CPDBarWidth; 
    } 

} 

-(void)configureAxes 
{ 
    // 1 - Configure styles 
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle]; 
    axisTitleStyle.color = [CPTColor whiteColor]; 
    axisTitleStyle.fontName = @"Helvetica-Bold"; 
    axisTitleStyle.fontSize = 12.0f; 
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle]; 
    axisLineStyle.lineWidth = 2.0f; 
    axisLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:1]; 
    // 2 - Get the graph's axis set 
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet; 
    // 3 - Configure the x-axis 
    axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone; 
    axisSet.xAxis.title = @"Days of Week (Mon - Fri)"; 
    axisSet.xAxis.titleTextStyle = axisTitleStyle; 
    axisSet.xAxis.titleOffset = 10.0f; 
    axisSet.xAxis.axisLineStyle = axisLineStyle; 
    // 4 - Configure the y-axis 
    axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone; 
    axisSet.yAxis.title = @"Price"; 
    axisSet.yAxis.titleTextStyle = axisTitleStyle; 
    axisSet.yAxis.titleOffset = 5.0f; 
    axisSet.yAxis.axisLineStyle = axisLineStyle; 

} 

-(void)hideAnnotation:(CPTGraph *)graph 
{ 
    if ((graph.plotAreaFrame.plotArea) && (self.priceAnnotation)) { 
     [graph.plotAreaFrame.plotArea removeAnnotation:self.priceAnnotation]; 
     self.priceAnnotation = nil; 
    } 

} 
+0

什麼是'CPDBarInitialX'和'CPDBarWidth'? – 2014-10-01 00:39:26

+0

CGFloat const CPDBarWidth = 0.15f; CGFloat const CPDBarInitialX = 0.1f; – 2014-10-01 05:36:11

回答

0

隨着CPDBarWidth = 0.15,兩家酒吧僅佔據30%的連續杆位置之間的空間。增加barWidth以減少相鄰鋼筋之間的間距。

相關問題