防止x轴自定义标签不应从第0个位置开始

时间:2014-11-12 11:55:43

标签: ios core-plot

我已经使用多个图实现了散点图。我在图表中对齐x轴自定义标签时遇到问题。

我想在y轴和第一个x轴标签之间留出相同的空间,如y2轴,以及下图中x轴的最后一个日期标签。如果我在绘图范围内将x的最小值更改为1,则其x轴也会向右移动。

在此处查看我的图表http://1drv.ms/1zLVqZD

代码:

- (void)configureGraph {
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostView.bounds];
[graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
self.graphHostView.hostedGraph = graph;

graph.title = @"Dual Y Axis";

CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = @"Helvetica-Bold";
titleStyle.fontSize = 10.0f;

graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, -10.0f);

[graph.plotAreaFrame setPaddingLeft:50.0f];
[graph.plotAreaFrame setPaddingBottom:40.0f];
[graph.plotAreaFrame setPaddingTop:30.0f];
[graph.plotAreaFrame setPaddingRight:50.0f];

// Plot area delegate
graph.plotAreaFrame.plotArea.delegate = self;
   }

- (void)configurePlots {

CPTGraph *graph = self.graphHostView.hostedGraph;

//setup plot space1

plotSpace1 = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace1.allowsUserInteraction = NO;
plotSpace1.xRange = [CPTMutablePlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(7.0)];
plotSpace1.yRange = [CPTMutablePlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(100.0)];


//setup plot space2
plotSpace2 = [CPTXYPlotSpace new];
plotSpace2.allowsUserInteraction = NO;
plotSpace2.xRange = plotSpace1.xRange;
plotSpace2.yRange = [CPTMutablePlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(24.0)];

[self.graphHostView.hostedGraph addPlotSpace:plotSpace2];

//Safety Score plot
CPTScatterPlot *y1Plot = [CPTScatterPlot new];
y1Plot.dataSource = self;
y1Plot.identifier = @"Y Axis";
y1Plot.interpolation = CPTScatterPlotInterpolationCurved;

[graph addPlot:y1Plot toPlotSpace:plotSpace1];

//Fuel Efficiency plot
CPTScatterPlot *y2Plot = [CPTScatterPlot new];
y2Plot.dataSource = self;
y2Plot.identifier = @"Y2 Axis";
y2Plot.interpolation = CPTScatterPlotInterpolationCurved;
[graph addPlot:y2Plot toPlotSpace:plotSpace2];

//create styles and symbols

CPTColor *yAxisLineColor = [CPTColor colorWithComponentRed:255.0/255.0 green:196.0/255.0 blue:37.0/255.0f alpha:1.0f];
CPTColor *yAxisSymbolColor = [CPTColor colorWithComponentRed:255.0/255.0 green:196.0/255.0 blue:37.0/255.0f alpha:1.0f];
CPTMutableLineStyle *yAxisLineStyle = [y1Plot.dataLineStyle mutableCopy];
yAxisLineStyle.lineWidth = 2.5;
yAxisLineStyle.lineColor = yAxisLineColor;
y1Plot.dataLineStyle = yAxisLineStyle;

CPTMutableLineStyle *yAxisSymbolLineStyle = [CPTMutableLineStyle lineStyle];
yAxisSymbolLineStyle.lineColor = yAxisLineColor;

CPTPlotSymbol *yAxisPlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
yAxisPlotSymbol.fill = [CPTFill fillWithColor: yAxisSymbolColor];
yAxisPlotSymbol.lineStyle = yAxisSymbolLineStyle;
yAxisPlotSymbol.size = CGSizeMake(6.0f, 6.0f);

y1Plot.plotSymbol = yAxisPlotSymbol;
// Set plot delegate, to know when symbols have been touched
// We will display an annotation when a symbol is touched
y1Plot.delegate = self;
y1Plot.plotSymbolMarginForHitDetection = 5.0;

CPTColor *y2AxisLineColor = [CPTColor colorWithComponentRed:5.0/255.0 green:122.0/255.0 blue:232.0/255.0f alpha:1.0f];
CPTColor *y2AxisSymbolColor = [CPTColor colorWithComponentRed:5.0/255.0 green:122.0/255.0 blue:232.0/255.0f alpha:1.0f];
CPTMutableLineStyle *y2AxisLineStyle = [y2Plot.dataLineStyle mutableCopy];
y2AxisLineStyle.lineWidth = 2.5;
y2AxisLineStyle.lineColor = y2AxisLineColor;
y2Plot.dataLineStyle = y2AxisLineStyle;

CPTMutableLineStyle *y2AxisSymbolLineStyle = [CPTMutableLineStyle lineStyle];
y2AxisSymbolLineStyle.lineColor = y2AxisLineColor;

CPTPlotSymbol *y2AxisSymbol = [CPTPlotSymbol ellipsePlotSymbol];
y2AxisSymbol.fill = [CPTFill fillWithColor:y2AxisSymbolColor];
y2AxisSymbol.lineStyle = y2AxisSymbolLineStyle;
y2AxisSymbol.size = CGSizeMake(6.0f, 6.0f);

y2Plot.plotSymbol = y2AxisSymbol;
// Set plot delegate, to know when symbols have been touched
// We will display an annotation when a symbol is touched
y2Plot.delegate = self;
y2Plot.plotSymbolMarginForHitDetection = 5.0;

  }

 - (void)configureAxes {
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 10.0f;

CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];

CPTMutableTextStyle *axisTextStyle = [CPTMutableTextStyle new];;
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 10.0f;

CPTMutableTextStyle *axisLabelStyle = [CPTMutableTextStyle new];;
axisLabelStyle.color = [CPTColor whiteColor];
axisLabelStyle.fontName = @"Helvetica-Bold";
axisLabelStyle.fontSize = 10.0f;

CPTMutableLineStyle *majorTickLineStyle = [CPTMutableLineStyle lineStyle];
majorTickLineStyle.lineWidth = 2.5f;
majorTickLineStyle.lineColor = [CPTColor colorWithCGColor:[UIColor whiteColor].CGColor];

CPTMutableLineStyle *minorTickLineStyle = [CPTMutableLineStyle lineStyle];
minorTickLineStyle.lineWidth = 1.5f;
minorTickLineStyle.lineColor = [CPTColor colorWithCGColor:[UIColor colorWithWhite:0.8 alpha:1.0].CGColor];;

CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
gridLineStyle.lineColor = [CPTColor grayColor];
gridLineStyle.lineWidth = 1.0f;

//Get Axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graphHostView.hostedGraph.axisSet;

//Configure x-axis
CPTXYAxis *xAxis = axisSet.xAxis;
xAxis.title = NSLocalizedString(@"This week", nil);
xAxis.titleTextStyle = axisTitleStyle;

xAxis.axisLineStyle = axisLineStyle;
xAxis.labelTextStyle = axisTextStyle;
xAxis.majorTickLineStyle = axisLineStyle;
xAxis.titleDirection = CPTSignNegative;
xAxis.titleOffset = 20.0f;

xAxis.majorIntervalLength = CPTDecimalFromFloat(1.0f);
xAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);

xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
NSArray *xAxisLabels = @[@"01/11/2014", @"02/11/2014", @"03/11/2014", @"04/11/2014", @"05/11/2014", @"06/11/2014", @"07/11/2014"];
NSMutableArray *ticks = [NSMutableArray arrayWithCapacity:1];
for(NSUInteger counter = 0; counter < xAxisLabels.count; counter++) {
    [ticks addObject:@(counter)];
}

NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:xAxisLabels.count];
for(NSNumber *tickLocation in ticks) {
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:xAxisLabels[labelLocation++] textStyle:axisLabelStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = 3.0f; //xAxis.majorTickLength + xAxis.labelOffset;
    //newLabel.rotation = M_PI/3.5f;
    [customLabels addObject:newLabel];
}

xAxis.axisLabels = [NSSet setWithArray:customLabels];
xAxis.majorTickLocations = [NSSet setWithArray:ticks];

//Configure y-axis for safety score
CPTXYAxis *yAxis = axisSet.yAxis;
yAxis.title = NSLocalizedString(@"Y Axis", nil);
yAxis.titleTextStyle = axisTitleStyle;
yAxis.labelTextStyle = axisTextStyle;
//yAxis.majorGridLineStyle = gridLineStyle;
majorTickLineStyle.lineColor = [CPTColor colorWithComponentRed:255.0/255.0 green:196.0/255.0 blue:37.0/255.0f alpha:1.0f];
minorTickLineStyle.lineColor = [CPTColor colorWithComponentRed:255.0/255.0 green:196.0/255.0 blue:37.0/255.0f alpha:1.0f];
axisLineStyle.lineColor = [CPTColor colorWithComponentRed:255.0/255.0 green:196.0/255.0 blue:37.0/255.0f alpha:1.0f];
yAxis.majorTickLineStyle = majorTickLineStyle;
yAxis.minorTickLineStyle = minorTickLineStyle;
yAxis.axisLineStyle = axisLineStyle;

yAxis.titleDirection = CPTSignNegative;
yAxis.majorIntervalLength = CPTDecimalFromFloat(20.0f);
yAxis.minorTicksPerInterval = 1;
yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0);

//Configure y2-axis
CPTXYAxis *y2Axis = [CPTXYAxis new];
y2Axis.title = NSLocalizedString(@"Y2 Axis", nil);
y2Axis.titleRotation = - M_PI_2;
y2Axis.titleTextStyle = axisTitleStyle;
y2Axis.labelTextStyle = axisTextStyle;

majorTickLineStyle.lineColor = [CPTColor colorWithComponentRed:5.0/255.0 green:122.0/255.0 blue:232.0/255.0f alpha:1.0f];
minorTickLineStyle.lineColor = [CPTColor colorWithComponentRed:5.0/255.0 green:122.0/255.0 blue:232.0/255.0f alpha:1.0f];
axisLineStyle.lineColor = [CPTColor colorWithComponentRed:5.0/255.0 green:122.0/255.0 blue:232.0/255.0f alpha:1.0f];

y2Axis.majorTickLineStyle = majorTickLineStyle;
y2Axis.minorTickLineStyle = minorTickLineStyle;
y2Axis.axisLineStyle = axisLineStyle;

y2Axis.plotSpace = plotSpace2;
y2Axis.delegate = self;
y2Axis.coordinate = CPTCoordinateY;
y2Axis.tickDirection = CPTSignPositive;

y2Axis.majorIntervalLength = CPTDecimalFromFloat(8.0f);

y2Axis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(7.0f);

self.graphHostView.hostedGraph.axisSet.axes = @[xAxis, yAxis, y2Axis];
 }

1 个答案:

答案 0 :(得分:0)

您需要移动x范围的位置增加其长度。

plotSpace1.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-1.0)
                                                 length:CPTDecimalFromFloat(8.0)];