核心图:将数据点添加到折线图时遇到问题

发布于 2024-12-04 18:21:44 字数 317 浏览 0 评论 0原文

目前,我正在使用散点图示例代码中的以下代码:

// Axes
CPTXYAxisSet *xyAxisSet = (id)graph.axisSet;
CPTXYAxis *xAxis = xyAxisSet.xAxis;
CPTMutableLineStyle *lineStyle = [xAxis.axisLineStyle mutableCopy];
lineStyle.lineCap = kCGLineCapButt;

但我的问题是,该图有点类似于条形图。轴上的 X 和 Y 值都只从 0 到 1。如何使两个轴显示 [-5,5] 之间的值?

Currently I am using the following code from the scatter plot example code:

// Axes
CPTXYAxisSet *xyAxisSet = (id)graph.axisSet;
CPTXYAxis *xAxis = xyAxisSet.xAxis;
CPTMutableLineStyle *lineStyle = [xAxis.axisLineStyle mutableCopy];
lineStyle.lineCap = kCGLineCapButt;

My problem is though, the graph somewhat resembles a bar graph. X and Y values on the axes both only go from 0 to 1. How to make both axes show the values from [-5,5]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左秋 2024-12-11 18:21:44

您需要设置绘图空间的 xRange 和 yRange 以设置绘图将显示的值的范围。例如:

plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(-5.0) length: CPTDecimalFromDouble(10.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(-5.0) length: CPTDecimalFromDouble(10.0)];

要显示实际的点,您需要设置图形的 dataSource 对象,并且它需要响应数据源方法:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot;
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;

如果您只有一个图,则可以忽略 numberForPlot: 的第一个参数。当需要点的 x 轴值时,第二个参数将为 0;当需要点的 Y 轴值时,第二个参数将为 1。索引是绘图的点数。因此,假设您有一个名为 myDataArray 的 NSArray 中的数据,其中该数组中的每个对象都是 x,y 值的另一个 NSArray,您将得到如下内容:

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

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;
{
    return [[myDataArray objectAtIndex: index] objectAtIndex: fieldEnum];
}

您可以使用固定数据设置数据数组,例如:

NSArray *myDataArray = [NSArray arrayWithObjects: 
    [NSArray arrayWithObjects: [NSNumber numberWithInt: 2], [NSNumber numberWithInt: 3], nil],
    [NSArray arrayWithObjects: [NSNumber numberWithInt: 3], [NSNumber numberWithInt: 2], nil],
    [NSArray arrayWithObjects: [NSNumber numberWithInt: 5], [NSNumber numberWithInt: 5], nil],
    nil];

You need to set the xRange and yRange for the plotspace to set the range of values the plot will show. Something like:

plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(-5.0) length: CPTDecimalFromDouble(10.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(-5.0) length: CPTDecimalFromDouble(10.0)];

To get actual points to show up, you need to set up the graph's dataSource object, and it needs to respond to the datasource methods:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot;
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;

If you only have one plot, you can ignore the first argument to numberForPlot:. The second argument will be 0 when it wants the x axis value for the point, and it will be 1 when it wants the Y axis value for the point. Index is the point number for your plot. So assuming you have the data in an NSArray called myDataArray, where each object in that array is another NSArray of x,y values, you'll have something like:

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

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;
{
    return [[myDataArray objectAtIndex: index] objectAtIndex: fieldEnum];
}

You can set up the data array with fixed data like:

NSArray *myDataArray = [NSArray arrayWithObjects: 
    [NSArray arrayWithObjects: [NSNumber numberWithInt: 2], [NSNumber numberWithInt: 3], nil],
    [NSArray arrayWithObjects: [NSNumber numberWithInt: 3], [NSNumber numberWithInt: 2], nil],
    [NSArray arrayWithObjects: [NSNumber numberWithInt: 5], [NSNumber numberWithInt: 5], nil],
    nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文