gpt4 book ai didi

objective-c - 如何使用 Core Plot 设置图表宽度?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:41 24 4
gpt4 key购买 nike

我有一个文件所有者设置为此 View 的 nib 文件,它只有一个 View 设置了自定义类 CPTGraphHostingView。它通过一个导出连接到这个 View 。此 View 加载,但它显示的是一个非常小的条形图。我真的不确定为什么。此外,任何添加到 IB View 中的内容都会颠倒过来。

图表在哪一点设置了宽度?我假设使用 self.view.bounds 只会让图表用完所有可用的屏幕空间。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:@"PollResultView" bundle:nil];
self.title = @"Results";
return self;
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
[super viewDidLoad];

graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];

CPTXYPlotSpace *barPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(100)];
barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(30)];

[graph addPlotSpace:barPlotSpace];

CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 1.0f;
majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.75f];

CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 1.0f;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.25f];

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;

CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromInteger(10);
x.minorTicksPerInterval = 9;
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.labelOffset = 10.0f;
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f)
length:CPTDecimalFromFloat(10.0f)];
x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)
length:CPTDecimalFromFloat(100.0f)];
x.title = @"X Axis";
x.titleOffset = 30.0f;
x.titleLocation = CPTDecimalFromFloat(5.0f);
x.plotSpace = barPlotSpace;

CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromInteger(10);
y.minorTicksPerInterval = 9;
y.orthogonalCoordinateDecimal = CPTDecimalFromInteger(-0.5);
y.preferredNumberOfMajorTicks = 8;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.labelOffset = 10.0f;
y.labelRotation = M_PI/2;
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)
length:CPTDecimalFromFloat(100.0f)];
y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f)
length:CPTDecimalFromFloat(10.0f)];
y.title = @"Y Axis";
y.titleOffset = 30.0f;
y.titleLocation = CPTDecimalFromInteger(55);
y.plotSpace = barPlotSpace;

graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil];

CPTMutableLineStyle *barLineStyle = [[[CPTMutableLineStyle alloc] init] autorelease];
barLineStyle.lineWidth = 1.0f;
barLineStyle.lineColor = [CPTColor whiteColor];

CPTBarPlot *barPlot = [[[CPTBarPlot alloc] init] autorelease];
barPlot.lineStyle = barLineStyle;
barPlot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.0f
green:0.0f
blue:0.5f
alpha:0.5f]];
barPlot.barBasesVary = YES;
barPlot.barWidth = CPTDecimalFromFloat(0.5f); // bar is 50% of the available space
barPlot.barCornerRadius = 10.0f;
barPlot.barsAreHorizontal = NO;

CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
barPlot.barLabelTextStyle = whiteTextStyle;
// barPlot.delegate = self;
barPlot.dataSource = self;
barPlot.identifier = @"Bar Plot 1";

[graph addPlot:barPlot toPlotSpace:barPlotSpace];

CPTBarPlot *barPlot2 = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor]
horizontalBars:NO];
barPlot2.lineStyle = barLineStyle;
barPlot2.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.0f
green:1.0f
blue:0.5f
alpha:0.5f]];
barPlot2.barBasesVary = YES;
barPlot2.barWidth = CPTDecimalFromFloat(1.0f);
barPlot2.barCornerRadius = 2.0f;
barPlot2.barsAreHorizontal = NO;
//barPlot2.delegate = self;
barPlot2.dataSource = self;
barPlot2.identifier = @"Bar Plot 2";

[graph addPlot:barPlot2 toPlotSpace:barPlotSpace];

CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
theLegend.numberOfRows = 2;
theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]];
theLegend.borderLineStyle = barLineStyle;
theLegend.cornerRadius = 10.0;
theLegend.swatchSize = CGSizeMake(20.0, 20.0);
whiteTextStyle.fontSize = 16.0;
theLegend.textStyle = whiteTextStyle;
theLegend.rowMargin = 10.0;
theLegend.paddingLeft = 12.0;
theLegend.paddingTop = 12.0;
theLegend.paddingRight = 12.0;
theLegend.paddingBottom = 12.0;

NSArray *plotPoint = [NSArray arrayWithObjects:[NSNumber numberWithInteger:0], [NSNumber numberWithInteger:95], nil];
CPTPlotSpaceAnnotation *legendAnnotation = [[[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:barPlotSpace anchorPlotPoint:plotPoint] autorelease];
legendAnnotation.contentLayer = theLegend;
legendAnnotation.contentAnchorPoint = CGPointMake(0.0f, 1.0f);
[graph.plotAreaFrame.plotArea addAnnotation:legendAnnotation];

graphHostingView.hostedGraph = graph;
}

最佳答案

托管 View 将使图形自动填充其边界。只要托管 View 的大小正确地适合其容器,初始化它时使用什么都没有关系。这里发生了几件事:

  1. 您在白色背景上使用白色网格线。他们很难这样看。 :-) 更改线条颜色或在图形上设置不同颜色的填充。

  2. 在 y 轴上复制/粘贴错误:

    x.axisLineStyle = nil; x.majorTickLineStyle = nil; x.minorTickLineStyle = nil;

    应该是

    y.axisLineStyle = nil; y.majorTickLineStyle = nil; y.minorTickLineStyle = nil;

  3. 无需设置坐标轴的可见范围——反正您也不会绘制它们。

  4. 也无需设置 gridLinesRange——默认值是您正在使用的绘图范围。

  5. 使用 barBasesVary,确保您的数据源也为 CPTBarPlotFieldBarBase 字段提供数据。

  6. 使用绘图空间注释时,图例的 anchor 必须在绘图空间范围内(当前为 x:[0, 100] 和 y:[0, 30]),否则不会可见。

  7. 您需要在 graph.plotAreaFrame 上设置填充以使轴标签和标题可见。您还可以调整 graph 上的填充以设置绘图区域框架外的整体边距。

埃里克

关于objective-c - 如何使用 Core Plot 设置图表宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7060403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com