2016-05-24 14 views
3

Ich möchte eine protokollbasierte App erstellen, für die ich versuche, ein Diagramm zu erstellen, das Aktivitäten basierend auf 24 Stunden anzeigt. Eine Stunde muss in vier geteilt werden (15 Minuten). Ich habe versucht, Core-Plot für das gleiche zu verwenden. Ich habe den Code, den ich bis jetzt gemacht habe und den Graph, den ich bis jetzt bekommen habe, angehängt.Problem beim Zeichnen von Graphen mit CorePlot

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 In meinem obigen Code war ich in der Lage majorTickLocations zu verwenden, um jede Stunde und minorTickLocations repräsentieren die 15 Minuten-Intervall darzustellen. Aber meine gewünschte Ausgabe ist wie das folgende Bild. Jede 30-Minuten-Marke sollte auch anders angezeigt werden. Wie kann ich das erreichen? Desired Output

Antwort

0

Fügen Sie eine weitere horizontale Achse hinzu. Verwenden Sie für die vier vorhandenen Intervalle die kleinen Häkchen für die 15-Minuten-Intervalle und die großen Häkchen für die 30-Minuten-Intervalle. Legen Sie auf der neuen Achse die Hauptrasterlinien (gezeichnet unter majorTickLocations) in den Stundenintervallen fest. Sie benötigen keine Teilstriche auf der neuen Achse.

+0

Können Sie etwas über das Festlegen von majorGridLines mit einem Beispielcode pl zeigen? – Mano

+0

Ich meine, wie die Standorte von majorGridLines – Mano

+0

angeben, wie der Wert für die Eigenschaft axis5.majorGridLines angegeben werden? – Mano

Verwandte Themen