2015-05-11 54 views
0

我正在嘗試繪製直方圖。 x軸的範圍應該是大約881到1281(我已經驗證了我使用的位置是881,當爲xRange構造CPTPlotRange時,範圍是400)。軸繪製與正確的範圍,但我的酒吧結束不久的2000(數據空間),即使我的數據源方法881和1281儘管設置了xRange,數據繪製在錯誤的地方

這之間發送位置值是代碼:

- (void) 
viewDidLoad 
{ 
    [super viewDidLoad]; 

    InputVariable   temp(1081.0, 50.0); 
    self.hist = new Histogram(temp.mean(), temp.stdDev(), 5.0); 
    for (Histogram::SizeT i = 0; i < 1000; ++i) 
    { 
     double v = temp.randomValue(); 
     self.hist->add(v); 
    } 

    const Histogram& hist = *self.hist; 

    CPTXYGraph* graph = [[CPTXYGraph alloc] initWithFrame: self.chart1.bounds]; 
    graph.fill    = [CPTFill fillWithColor: [CPTColor lightGrayColor]]; 
    graph.cornerRadius  = 4.0; 
    self.chart1.hostedGraph = graph; 

    self.graph = graph; 

    // Plot area 

    graph.plotAreaFrame.fill   = [CPTFill fillWithColor:[CPTColor lightGrayColor]]; 
    graph.plotAreaFrame.paddingTop = 4.0; 
    graph.plotAreaFrame.paddingBottom = 50.0; 
    graph.plotAreaFrame.paddingLeft = 50.0; 
    graph.plotAreaFrame.paddingRight = 4.0; 
    graph.plotAreaFrame.cornerRadius = 4.0; 
    graph.plotAreaFrame.masksToBorder = NO; 

    graph.plotAreaFrame.axisSet.borderLineStyle = [CPTLineStyle lineStyle]; 

    graph.plotAreaFrame.plotArea.fill = [CPTFill fillWithColor:[CPTColor whiteColor]]; 

    // Setup plot space 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.xRange  = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(hist.lowValue()) length:CPTDecimalFromDouble(hist.range())]; 
    plotSpace.yRange  = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(hist.maxBinCount() * 1.2)]; 
    //plotSpace.yScaleType = CPTScaleTypeLog; 

    // Line styles 
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle]; 
    axisLineStyle.lineWidth = 2.0; 
    //axisLineStyle.lineCap = kCGLineCapRound; 

    CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle]; 
    majorGridLineStyle.lineWidth = 0.75; 
    majorGridLineStyle.lineColor = [CPTColor redColor]; 

    CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle]; 
    minorGridLineStyle.lineWidth = 0.25; 
    minorGridLineStyle.lineColor = [CPTColor blueColor]; 

    // Text styles 
    CPTMutableTextStyle *axisTitleTextStyle = [CPTMutableTextStyle textStyle]; 
    axisTitleTextStyle.fontName = @"Helvetica Neue Bold"; 
    axisTitleTextStyle.fontSize = 14.0; 

    // Axes 
    // Label x axis with a fixed interval policy 
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 
    CPTXYAxis *x   = axisSet.xAxis; 
    x.separateLayers    = NO; 
    x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0); 
    x.majorIntervalLength   = CPTDecimalFromDouble(0.5); 
    x.minorTicksPerInterval  = 4; 
    x.tickDirection    = CPTSignNone; 
    x.axisLineStyle    = axisLineStyle; 
    x.majorTickLength    = 12.0; 
    x.majorTickLineStyle   = axisLineStyle; 
    //x.majorGridLineStyle   = majorGridLineStyle; 
    x.minorTickLength    = 8.0; 
    //x.minorGridLineStyle   = minorGridLineStyle; 
    x.title      = @"Temperature"; 
    x.titleTextStyle    = axisTitleTextStyle; 
    x.titleOffset     = 25.0; 
    //x.alternatingBandFills  = @[[[CPTColor redColor] colorWithAlphaComponent:0.1], [[CPTColor greenColor] colorWithAlphaComponent:0.1]]; 
    x.labelingPolicy    = CPTAxisLabelingPolicyAutomatic; 

    // Label y with an automatic label policy. 
    //axisLineStyle.lineColor = [CPTColor greenColor]; 

    CPTXYAxis *y = axisSet.yAxis; 
    y.separateLayers  = YES; 
    y.orthogonalCoordinateDecimal = plotSpace.xRange.location;//CPTDecimalFromDouble(hist.lowValue()); 
    y.minorTicksPerInterval = 9; 
    y.tickDirection   = CPTSignNone; 
    y.axisLineStyle   = axisLineStyle; 
    y.majorTickLength  = 12.0; 
    y.majorTickLineStyle = axisLineStyle; 
    //y.majorGridLineStyle = majorGridLineStyle; 
    //y.minorTickLength  = 8.0; 
    //y.minorGridLineStyle = minorGridLineStyle; 
    y.title     = @"Number of Samples"; 
    y.titleTextStyle  = axisTitleTextStyle; 
    y.titleOffset   = 40.0; 
    //y.alternatingBandFills = @[[[CPTColor blueColor] colorWithAlphaComponent:0.1], [NSNull null]]; 
    y.labelingPolicy  = CPTAxisLabelingPolicyAutomatic; 

    //CPTFill *bandFill = [CPTFill fillWithColor:[[CPTColor darkGrayColor] colorWithAlphaComponent:0.5]]; 
    //[y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(7.0) length:CPTDecimalFromDouble(1.5)] fill:bandFill]]; 
    //[y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1.5) length:CPTDecimalFromDouble(3.0)] fill:bandFill]]; 


    // Set up the bar plot… 

    //CPTXYPlotSpace *barPlotSpace = [[CPTXYPlotSpace alloc] init]; 
    CPTXYPlotSpace *barPlotSpace = (CPTXYPlotSpace *) self.graph.defaultPlotSpace; 

    //barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(200.0)]; 
    //barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(15.00)]; 
    //[self.graph addPlotSpace:barPlotSpace]; 

    CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars: false]; 
    barPlot.dataSource  = self; 

    barPlot.barsAreHorizontal = false; 
    barPlot.barWidth  = CPTDecimalFromDouble(1.0); 
    barPlot.barBasesVary = false; 
    barPlot.baseValue  = CPTDecimalFromDouble(0.0); 
    barPlot.barOffset  = CPTDecimalFromDouble(hist.lowValue()); 
    barPlot.identifier  = @"Bar Plot"; 
    //barPlot.plotRange  = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(7.0)]; 

    CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle]; 
    whiteTextStyle.color = [CPTColor whiteColor]; 
    //barPlot.labelTextStyle = whiteTextStyle; 

    [self.graph addPlot:barPlot toPlotSpace:barPlotSpace]; 
} 


- (NSUInteger) 
numberOfRecordsForPlot: (CPTPlot*) inPlot 
{ 
    return self.hist->numBins(); 
} 

- (double) 
doubleForPlot: (CPTPlot*) inPlot 
    field: (NSUInteger) inField 
    recordIndex: (NSUInteger) inIdx 
{ 
    const Histogram& h = *self.hist; 

    if (inField == CPTBarPlotFieldBarLocation) 
    { 
     double v = h.binMid(inIdx); 
     NSLog(@"v: %f", v); 
     return v; 
    } 
    else if (inField == CPTBarPlotFieldBarTip) 
    { 
     return h[inIdx]; 
    } 

    return 0.0; 
} 

而結果圖:

Example plot

任何想法,我做錯了嗎?謝謝!

+0

我看到你已經嘗試添加另一個劇情空間。你確定圖表只有一個繪圖空間嗎? –

+0

所有使用的代碼都是你在那裏看到的。我從一個例子中拿出了這個(也許是我在網上找到的東西)。我試過了,沒有第二個繪圖空間(使用默認繪圖空間)。事實上,在上面的代碼中,我不做任何事情,只是將該圖添加到默認空間。 – Rick

回答

1

對於此應用程序,barOffset應爲零(0)。偏移量用於從數據源給定的位置移動小節。當在同一圖表上繪製多條條形圖時,最常見的用途是在同一位置分離條形圖,這些條形圖可能在公共位置具有重疊的條形。

+0

Augh,謝謝。我不確定爲什麼我這樣做,可能從我複製並嘗試複製它的示例中看到了一些內容。 – Rick

相關問題