【问题标题】:Coreplot : Custom space between two points in graphCoreplot:图形中两点之间的自定义空间
【发布时间】:2019-11-21 11:31:57
【问题描述】:

如何在图中的数据点之间实现自定义空间,如下图所示,使用Objective c

CGRect bounds = self.graphView.bounds;
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:bounds];
self.graphView.hostedGraph = graph;

// graph.plotAreaFrame.paddingTop    += 25.0;
graph.plotAreaFrame.paddingLeft   += 55.0;
graph.plotAreaFrame.paddingBottom += 70.0;
graph.plotAreaFrame.paddingRight  += 20.0;

// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor =  self.line1Color;
//[[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];

CPTLineCap *lineCap = [CPTLineCap sweptArrowPlotLineCap];
lineCap.size = CGSizeMake(15.0, 15.0);

// Axes
// Label x axis with a fixed interval policy
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x          = axisSet.xAxis;

x.majorIntervalLength = CPTDecimalFromInt(1);
x.labelingOrigin = @(0).decimalValue;
int i = 0;
NSMutableArray *customLabels = [NSMutableArray new];
NSMutableArray *titleLocations = [NSMutableArray new];
for (NSMutableDictionary *v in self.values)
{
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[[v valueForKey:@"x"] toDDMMYYYYString] textStyle:x.labelTextStyle];
    newLabel.tickLocation = [[NSNumber numberWithInt:i] decimalValue];
    newLabel.offset          = x.labelOffset + x.majorTickLength;
    newLabel.rotation = M_PI/2.0f;
    [customLabels addObject:newLabel];
    [titleLocations addObject:[NSNumber numberWithInt:i ]];
    i++;
}

x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorTickLocations = [NSSet setWithArray:titleLocations];
x.axisLabels = [NSSet setWithArray:customLabels];
x.majorGridLineStyle = majorGridLineStyle;
x.axisConstraints             = [CPTConstraints constraintWithLowerOffset:0.0];

x.labelingOrigin = @(0).decimalValue;
x.orthogonalCoordinateDecimal = [NSDecimalNumber zero].decimalValue;
lineCap.lineStyle = x.axisLineStyle;
lineCap.fill      = [CPTFill fillWithColor:lineCap.lineStyle.lineColor];

// Label y with an automatic label policy.
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy              = CPTAxisLabelingPolicyAutomatic;
y.majorGridLineStyle          = majorGridLineStyle;
y.axisConstraints             = [CPTConstraints constraintWithLowerOffset:0.0];
y.labelOffset                 = 10.0;
y.minorTicksPerInterval = 0;`

我已经给出了以下 xrange 和 yrange

if(self.values.count>5){
        plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(self.pt) length:CPTDecimalFromFloat( MAX(self.values.count, 6))];
        plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(min) length:CPTDecimalFromFloat(max)];
}else{
        plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(self.pt) length:CPTDecimalFromFloat( MIN(self.values.count, 3))];
        plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(min) length:CPTDecimalFromFloat(max)];
}

【问题讨论】:

  • 显示你的代码,你在哪里计算 xAxis
  • 嗨,我可以在数据点之间以相等的距离进行绘图,coreplot 中的任何方法都可以在两个数据点之间有自定义间隙/空间?

标签: objective-c core-plot


【解决方案1】:

数据源为每个数据点提供 x 值和 y 值。这些值不必均匀分布。轴标签由labelingPolicy 控制。使用提供的位置或“无”策略来提供您自己的标签和/或勾选位置。

【讨论】:

  • 您好 Eric,我添加了代码参考,请您指导我,我无法理解。
  • newLabel.tickLocation设置为数据点的x值对应的值,而不是索引。使用您在数据源中使用的相同转换来计算 values 字典中的 x 值。
  • 嗨,Eric,我试过了,但是两个数据点之间的距离保持不变/等距,您能否提供一个简单的代码参考会有所帮助。 TIA
  • 查看 Core Plot 附带的 Plot Gallery 示例应用程序中的“Axis Labeling Policy”演示。
  • 您好 Eric,我可以在标签之间创建间隙或空间,但不能在图或数据点上创建。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多