如何改变绘图颜色

时间:2013-04-24 04:10:00

标签: ios objective-c core-plot

我正在使用核心绘图库来显示散点图。我在同一图表中显示两个图,我想显示第二个图是不同的颜色。如何显示更改第2个情节颜色请帮帮我。我使用下面的代码

 // Start with some simple sanity checks before we kick off
if ( (self.hostingView == nil) || (self.graphData == nil) ) {
    NSLog(@"TUTSimpleScatterPlot: Cannot initialise plot without hosting view or data.");
    return;
}

if ( self.graph != nil ) {
    NSLog(@"TUTSimpleScatterPlot: Graph object already exists.");
    return;
}

// Create a graph object which we will use to host just one scatter plot.
CGRect frame = [self.hostingView bounds];
self.graph = [[[CPTXYGraph alloc] initWithFrame:frame] autorelease];

// Add some padding to the graph, with more at the bottom for axis labels.
self.graph.plotAreaFrame.paddingTop = 20.0f;
self.graph.plotAreaFrame.paddingRight = 30.0f;
self.graph.plotAreaFrame.paddingBottom = 50.0f;
self.graph.plotAreaFrame.paddingLeft = 50.0f;

// Tie the graph we've created with the hosting view.
self.hostingView.hostedGraph = self.graph;

// If you want to use one of the default themes - apply that here.
//[self.graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];

// Create a line style that we will apply to the axis and data line.

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


// Create a text style that we will use for the axis labels.
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 14;
textStyle.color = [CPTColor whiteColor];

// Create the plot symbol we're going to use.
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol snowPlotSymbol];
plotSymbol.lineStyle = lineStyle;
plotSymbol.size = CGSizeMake(8.0, 8.0);     

// Setup some floats that represent the min/max values on our axis.
float xAxisMin = 0;
float xAxisMax = 120;
float yAxisMin = -200;
float yAxisMax = 400;

// We modify the graph's plot space to setup the axis' min / max values.
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)];

// Modify the graph's axis with a label, line style, etc.
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;




axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;   
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(2.0f);
axisSet.xAxis.minorTicksPerInterval = 1;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
// Define some custom labels for the data elements
axisSet.xAxis.labelRotation  = M_PI / 2;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;


NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:20],[NSDecimalNumber numberWithInt:30],[NSDecimalNumber numberWithInt:40],[NSDecimalNumber numberWithInt:50],[NSDecimalNumber numberWithInt:60],[NSDecimalNumber numberWithInt:70],[NSDecimalNumber numberWithInt:80],[NSDecimalNumber numberWithInt:90],[NSDecimalNumber numberWithInt:100],[NSDecimalNumber numberWithInt:110],[NSDecimalNumber numberWithInt:120],nil];
NSArray *xAxisLabels= [NSArray arrayWithObjects:@"Jan",@"Feb", @"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec", nil];
NSUInteger labelLocation     = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for ( NSNumber *tickLocation in customTickLocations ) {
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[xAxisLabels objectAtIndex:labelLocation++] textStyle:axisSet.xAxis.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset       = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
    NSLog(@"%f",newLabel.offset);
    newLabel.rotation     = M_PI / 4;
    [customLabels addObject:newLabel];
    [newLabel release];
}

axisSet.xAxis.axisLabels = [NSSet setWithArray:customLabels];

//axisSet.yAxis.title = @"Data Y";
//axisSet.yAxis.titleTextStyle = textStyle;
//axisSet.yAxis.titleOffset = 40.0f;
axisSet.yAxis.axisLineStyle = lineStyle;    
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.labelTextStyle = textStyle;   
axisSet.yAxis.labelOffset = 3.0f;   
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(100.0f);
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;   

// Add a plot to our graph and axis. We give it an identifier so that we 
// could add multiple plots (data lines) to the same graph if necessary.
CPTScatterPlot *plot = [[[CPTScatterPlot alloc] init] autorelease];
plot.dataSource = self;
plot.identifier = @"mainplot";
plot.dataLineStyle = lineStyle;
plot.plotSymbol = plotSymbol;
[self.graph addPlot:plot];

CPTScatterPlot *plot2 = [[[CPTScatterPlot alloc] init] autorelease];

plot2.dataSource = self;
plot2.identifier = @"secondplot";
plot2.dataLineStyle = lineStyle;
plot2.plotSymbol = plotSymbol;
[self.graph addPlot:plot2];

0 个答案:

没有答案