绘制主要网格线散点图 - CorrPlot

时间:2012-11-05 02:14:54

标签: ios5 core-plot

我正在尝试绘制我的折线图。但我不能画出主线。 当我自定义x轴标签时,所有majorgridline都不显示。以及如何隐藏y轴标签。 这是我的折线图: enter link description here

帮助我绘制它: enter link description here

  • MajorGridLine
  • custome xaxis lable,并隐藏yaxis lable。 感谢。

这是我的代码:

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

CGRect bounds = layerHostingView.bounds;

// Create the graph and assign the hosting view.
graph = [[CPTXYGraph alloc] initWithFrame:bounds];
layerHostingView.hostedGraph = graph;
[graph applyTheme:theme];

graph.plotAreaFrame.masksToBorder = NO;

// chang the chart layer orders so the axis line is on top of the bar in the chart.
NSArray *chartLayers = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:CPTGraphLayerTypePlots],
                        [NSNumber numberWithInt:CPTGraphLayerTypeMajorGridLines], 
                        [NSNumber numberWithInt:CPTGraphLayerTypeMinorGridLines],  
                        [NSNumber numberWithInt:CPTGraphLayerTypeAxisLines], 
                        [NSNumber numberWithInt:CPTGraphLayerTypeAxisLabels], 
                        [NSNumber numberWithInt:CPTGraphLayerTypeAxisTitles], 
                        nil];
graph.topDownLayerOrder = chartLayers;    
[chartLayers release];


// Add plot space for horizontal bar charts
graph.paddingLeft = 50.0;
graph.paddingTop = 25.0;
graph.paddingRight = 50.0;
graph.paddingBottom = 60.0;


// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(7)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(1000)];

// Setup grid line style
CPTMutableLineStyle *majorXGridLineStyle = [CPTMutableLineStyle lineStyle];
majorXGridLineStyle.lineWidth = 1.0f;
majorXGridLineStyle.dashPattern =  CPTLinearBlendingMode;
majorXGridLineStyle.lineColor = [CPTColor colorWithComponentRed:64/255.0 green:177/255.0 blue:219/255.0 alpha:1.0];


// Setup x-Axis.
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorGridLineStyle = majorXGridLineStyle;
x.preferredNumberOfMajorTicks = 7;
x.majorIntervalLength = CPTDecimalFromString(@"1");
x.minorTicksPerInterval = 1;
x.labelRotation =  M_PI/4;
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(-7.0f)];
x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(-7.0f)];
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSMutableArray *labels = [[NSMutableArray alloc] initWithCapacity:[xAxisWeek count]];
int idx = 0;
for (NSString *week in xAxisWeek)
{
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:week textStyle:x.labelTextStyle];
    label.tickLocation = CPTDecimalFromInt(idx);
    label.offset = 5.0f;
    label.rotation = M_PI/4;
    [labels addObject:label];
    [label release];
    idx--; 
}
x.axisLabels = [NSSet setWithArray:labels];
[labels release];


// Setup y-Axis.
CPTMutableLineStyle *majorYGridLineStyle = [CPTMutableLineStyle lineStyle];
majorYGridLineStyle.lineWidth = 1.0f;
majorYGridLineStyle.dashPattern =  CPTLinearBlendingMode;
majorYGridLineStyle.lineColor = [CPTColor colorWithComponentRed:87/255.0 green:142/255.0 blue:242/255.0 alpha:1.0];

CPTXYAxis *y = axisSet.yAxis;
y.majorGridLineStyle = majorYGridLineStyle;
y.majorIntervalLength = CPTDecimalFromString(@"200");
y.minorTicksPerInterval = 1;
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(1000.0f)];
y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(-1000.0f)];

1 个答案:

答案 0 :(得分:0)

不要为任一轴设置visibleRangegridLinesRange。默认值将使轴和网格线延伸到绘图区域,这是第二个图像中显示的内容。

如果您确实需要使用这些属性(例如,让绘图符号悬挂在网格外),请将两者都设置为相同的范围。