【问题标题】:Core-plot - plotting very small numbers on scatter plot核心图 - 在散点图上绘制非常小的数字
【发布时间】:2012-04-16 18:24:59
【问题描述】:

在散点图上,我的 x 坐标数据只是表示自纪元以来的时间(以秒为单位)的长数字,而 y 坐标数据如下所示:0.675088、0.670629、0.669599。我想在 y 轴上显示为主要刻度是这样的:0.64、0.65、0.66、0.67,但图表上当前显示的是:0.5、0.7、1.0、1.2。

首先,我不知道为什么我会得到上面提到的不规则滴答间隔(0.5 到 0.7 = 0.2、0.7 到 1.0 = 0.3?)但我猜是因为我正在试验标签政策并将其设置为 CPTAxisLabelingPolicyEqualDivisions - 有人可以解释一下所有这些标签政策的含义吗?

真正的问题是,考虑到我正在绘制非常小的数字,我应该在 y 轴上为 majorIntervalLength 使用什么样的值?目前我有 0.01,但是当我按 -n 的数量级调整这个值时,它实际上对我的图表没有任何影响。

这是我基于 Plot_Gallery_iOS DatePlot.m 的代码的 sn-p

-(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme
{
CGRect bounds = layerHostingView.bounds;

NSDate *refDate = [NSDate dateWithTimeIntervalSince1970:0];
NSTimeInterval oneDay = 24 * 60 * 60;

CPTGraph *graph = [[[CPTXYGraph alloc] initWithFrame:bounds] autorelease];
[self addGraph:graph toHostingView:layerHostingView];
[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];

[self setTitleDefaultsForGraph:graph withBounds:bounds];
[self setPaddingDefaultsForGraph:graph withBounds:bounds];

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;

NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
NSTimeInterval threeMonthsago = now - (90*oneDay);

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(threeMonthsago) length:CPTDecimalFromFloat(now - threeMonthsago)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.6f) length:CPTDecimalFromFloat(0.7f)];

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = refDate;

// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x          = axisSet.xAxis;
x.majorIntervalLength         = CPTDecimalFromFloat(oneDay*30);
x.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.6f);
x.minorTicksPerInterval       = 7;
x.labelFormatter            = timeFormatter;
x.labelRotation             = M_PI / 4;
x.preferredNumberOfMajorTicks = 5;

CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength         = CPTDecimalFromDouble(0.001);
y.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
y.preferredNumberOfMajorTicks = 5;
y.minorTicksPerInterval       = 10;
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(now);
//    y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.6f) length:CPTDecimalFromFloat(0.1f)];
CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = @"Date Plot";

// [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]];

CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
lineStyle.lineWidth              = .5f;
lineStyle.lineColor              = [CPTColor greenColor];
dataSourceLinePlot.dataLineStyle = lineStyle;

// Auto scale the plot space to fit the plot data
// Extend the ranges by 30% for neatness
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]];
CPTMutablePlotRange *xRange = [[plotSpace.xRange mutableCopy] autorelease];
CPTMutablePlotRange *yRange = [[plotSpace.yRange mutableCopy] autorelease];
[xRange expandRangeByFactor:CPTDecimalFromDouble(1.3)];
[yRange expandRangeByFactor:CPTDecimalFromDouble(1.3)];
plotSpace.xRange = xRange;
plotSpace.yRange = yRange;

dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
 }

更新:

通过这样做设置自定义 y 主要刻度来解决问题:

y.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;

NSSet *majorTickLoc = [NSSet setWithObjects:[NSDecimalNumber numberWithFloat:0.64f], [NSDecimalNumber numberWithFloat:0.65f], [NSDecimalNumber numberWithFloat:0.66f], [NSDecimalNumber numberWithFloat:0.67f],[NSDecimalNumber numberWithFloat:0.68f],nil];
y.majorTickLocations = majorTickLoc;

但数字似乎仍然是四舍五入的,即只有一个小数位 0.6、0.7、0.7 等,结果证明这只是标签。在标签上设置数字格式化程序:

NSNumberFormatter *yFormatter = [[NSNumberFormatter alloc] init];
[yFormatter setMinimumFractionDigits:4];
[yFormatter setMaximumFractionDigits:4];
[yFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
y.labelFormatter = yFormatter;

【问题讨论】:

    标签: numbers core-plot


    【解决方案1】:

    试试下面的代码:

    y.majorIntervalLength = CPTDecimalFromString(@"0.1")
    

    【讨论】:

    • 我不确定这是否能回答我的问题?我想要以下数字作为 y 主要刻度:0.64、0.65、0.66、0.67,但我不知道如何实现。
    【解决方案2】:

    为 y 轴设置 labelFormatter

    NSNumberFormatter *newFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    newFormatter.minimumIntegerDigits = 1;
    newFormatter.maximumFractionDigits = 2;
    newFormatter.minimumFractionDigits = 2;
    y.labelFormatter = newFormatter;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 2022-07-28
      • 2015-05-21
      相关资源
      最近更新 更多