【问题标题】:Issue in drawing graph using CorePlot使用 CorePlot 绘制图形的问题
【发布时间】:2016-05-24 09:56:04
【问题描述】:

我想创建一个基于日志的应用程序,我正在尝试制作一个图表来显示基于 24 小时时间的活动。一小时必须分成四个(15分钟)。我也尝试过使用核心情节。我已经附上了我到目前为止所做的代码和我到目前为止得到的图表。

 const CGFloat majorTickLength = 20; // height of the major tick
const CGFloat minorTickLength = 8.0;  // height of the minor tick
//    const CGFloat titleOffset     = self.titleSize;

#if TARGET_OS_IPHONE
CGRect bounds = hostingView.bounds;
#else
CGRect bounds = NSRectToCGRect(hostingView.bounds);
#endif

// Create graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:bounds];
[self addGraph:graph toHostingView:hostingView];
[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTSlateTheme]];

graph.fill = [CPTFill fillWithColor:[CPTColor blackColor]];

// Plot area
graph.plotAreaFrame.paddingTop    = self.titleSize;
graph.plotAreaFrame.paddingBottom = self.titleSize;
graph.plotAreaFrame.paddingLeft   = self.titleSize;
graph.plotAreaFrame.paddingRight  = self.titleSize;
graph.plotAreaFrame.masksToBorder = NO;

// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:@0.0 length:@1440.0];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:@0.0 length:@20.0];

// Line styles
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0;

CPTMutableLineStyle *majorTickLineStyle = [axisLineStyle mutableCopy];
majorTickLineStyle.lineWidth = 1.0;
majorTickLineStyle.lineCap   = kCGLineCapRound;

CPTMutableLineStyle *minorTickLineStyle = [axisLineStyle mutableCopy];
minorTickLineStyle.lineWidth = 1.0;
minorTickLineStyle.lineCap   = kCGLineCapButt;

// Text styles
CPTMutableTextStyle *axisTitleTextStyle = [CPTMutableTextStyle textStyle];
axisTitleTextStyle.fontName = @"Helvetica-Bold";


CPTMutableNumberSet *majorTickLocations = [NSMutableSet set];

for (int i = 60; i <= 1440; i += 60)
{
    [majorTickLocations addObject:@(i)];
}

CPTMutableNumberSet *minorTickLocations = [NSMutableSet set];
for ( NSUInteger loc = 0; loc <= 1440; loc += 15 )
{
    [minorTickLocations addObject:@(loc)];
}


// Axis1
CPTXYAxis *axis1 = [[CPTXYAxis alloc] init];
axis1.plotSpace          = graph.defaultPlotSpace;
axis1.labelingPolicy     = CPTAxisLabelingPolicyNone;
axis1.orthogonalPosition = @1.0;
axis1.tickDirection      = CPTSignPositive;
axis1.axisLineStyle      = axisLineStyle;
axis1.majorTickLength    = majorTickLength;
axis1.majorTickLineStyle = majorTickLineStyle;
axis1.minorTickLength    = minorTickLength;
axis1.minorTickLineStyle = minorTickLineStyle;
axis1.majorTickLocations = majorTickLocations;
axis1.minorTickLocations = minorTickLocations;
// Axis2
CPTXYAxis *axis2 = [[CPTXYAxis alloc] init];
axis2.plotSpace          = graph.defaultPlotSpace;
axis2.labelingPolicy     = CPTAxisLabelingPolicyNone;
axis2.orthogonalPosition = @2.0;
axis2.tickDirection      = CPTSignPositive;
axis2.axisLineStyle      = axisLineStyle;
axis2.majorTickLength    = majorTickLength;
axis2.majorTickLineStyle = majorTickLineStyle;
axis2.minorTickLength    = minorTickLength;
axis2.minorTickLineStyle = minorTickLineStyle;
axis2.majorTickLocations = majorTickLocations;
axis2.minorTickLocations = minorTickLocations;
// Axis3
CPTXYAxis *axis3 = [[CPTXYAxis alloc] init];
axis3.plotSpace          = graph.defaultPlotSpace;
axis3.labelingPolicy     = CPTAxisLabelingPolicyNone;
axis3.orthogonalPosition = @3.0;
axis3.tickDirection      = CPTSignPositive;
axis3.axisLineStyle      = axisLineStyle;
axis3.majorTickLength    = majorTickLength;
axis3.majorTickLineStyle = majorTickLineStyle;
axis3.minorTickLength    = minorTickLength;
axis3.minorTickLineStyle = minorTickLineStyle;
axis3.majorTickLocations = majorTickLocations;
axis3.minorTickLocations = minorTickLocations;
// Axis4
CPTXYAxis *axis4 = [[CPTXYAxis alloc] init];
axis4.plotSpace          = graph.defaultPlotSpace;
axis4.labelingPolicy     = CPTAxisLabelingPolicyNone;
axis4.orthogonalPosition = @4.0;
axis4.tickDirection      = CPTSignPositive;
axis4.axisLineStyle      = axisLineStyle;
axis4.majorTickLength    = majorTickLength;
axis4.majorTickLineStyle = majorTickLineStyle;
axis4.minorTickLength    = minorTickLength;
axis4.minorTickLineStyle = minorTickLineStyle;
axis4.majorTickLocations = majorTickLocations;
axis4.minorTickLocations = minorTickLocations;

CPTMutableAxisLabelSet *axis4LabelSet = [NSMutableSet set];

for ( NSUInteger i = 1; i < 24; i++ )
{
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%lu", (unsigned long)i]
                                                      textStyle:axis4.labelTextStyle];
    newLabel.tickLocation = @(i*60);
    newLabel.offset       = axis4.labelOffset + axis4.majorTickLength;

    [axis4LabelSet addObject:newLabel];
}
axis4.axisLabels = axis4LabelSet;



// Add axes to the graph
graph.axisSet.axes = @[axis1, axis2, axis3, axis4];

在我上面的代码中,我能够使用 majorTickLocations 来表示每个 HOUR 和 minorTickLocations 来表示 15 分钟的间隔。但我想要的输出如下图所示。每个 30 分钟标记也应显示不同。我怎样才能做到这一点。

【问题讨论】:

    标签: ios objective-c graph core-plot


    【解决方案1】:

    再添加一个水平轴。在现有的四个上,使用次要刻度线表示 15 分钟间隔,使用主要刻度线表示 30 分钟间隔。在新轴上,按小时间隔设置主要网格线(在majorTickLocations 处绘制)。新轴上不需要任何刻度线。

    【讨论】:

    • 你能用一些示例代码 pl 来说明如何设置majorGridLines吗?
    • 我的意思是,如何指定majorGridLines的位置
    • 即如何指定属性axis5.majorGridLines的值?
    • 在刻度线位置绘制网格线。设置相应的线型,使网格线出现。 Plot Gallery 应用中有很多示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    相关资源
    最近更新 更多