尽管设置了xRange,但数据却被绘制在错误的位置

时间:2015-05-11 19:56:44

标签: core-plot

我试图绘制直方图。 x轴范围应该是大约881到1281(在我为xRange构建CPTPlotRange时,我已经确认我使用的位置为881,范围为400)。轴绘制的范围正确,但我的条形图最终接近2000(数据空间),即使我的数据源方法发送的位置值介于881和1281之间。

以下是代码:

- (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

知道我做错了什么吗?谢谢!

1 个答案:

答案 0 :(得分:1)

对于此应用程序,barOffset应为零(0)。偏移量用于从数据源给定的位置移动条形。最常见的用途是在同一图表上绘制多个条形图时将条形图分开,这些条形图可能在公共位置有重叠条形。