使用CorePlot绘制图表时出现问题

时间:2016-05-24 09:56:05

标签: ios objective-c graph core-plot

我想创建一个基于日志的应用程序,我试图制作一个基于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];

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

1 个答案:

答案 0 :(得分:0)

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