如何在ios 8中使用coreplot编写y1和y2轴的代码?

时间:2014-12-04 06:22:42

标签: objective-c ios8 core-plot

使用绘制线图的核心图。我想要两个不同间隔的y轴。

对于x y轴我使用下面的代码。

  #pragma mark - Chart behavior
    -(void)initPlotForDancis {
        [self configureHostDancis];
        [self configureGraphDancis];
        [self configurePlotsDancis];
        [self configureAxesDancis];
    }

    -(void)configureHostDancis {
        self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:CGRectMake(0 ,0, dancisView.frame.size.width, dancisView.frame.size.height-150)];
        self.hostView.allowPinchScaling = YES;
        [dancisView addSubview:self.hostView];
    }

    -(void)configureGraphDancis {
        // 1 - Create the graph
        CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
        [graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
        self.hostView.hostedGraph = graph;
        // 2 - Set graph title
        NSString *title = @"Dancis Graph";
        graph.title = title;
        // 3 - Create and set text style
        CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
        titleStyle.color = [CPTColor whiteColor];
        titleStyle.fontName = @"Helvetica-Bold";
        titleStyle.fontSize = 16.0f;
        graph.titleTextStyle = titleStyle;
        graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
        graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
        // 4 - Set padding for plot area
        [graph.plotAreaFrame setPaddingLeft:30.0f];
        [graph.plotAreaFrame setPaddingBottom:30.0f];
        // 5 - Enable user interactions for plot space
        CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
        plotSpace.allowsUserInteraction = YES;
    }

    -(void)configurePlotsDancis {
        // 1 - Get graph and plot space
        CPTGraph *graph = self.hostView.hostedGraph;
        CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
        // 2 - Create the three plots
        CPTScatterPlot *aaplPlot = [[CPTScatterPlot alloc] init];
        aaplPlot.dataSource = self;
        aaplPlot.identifier = CPDTickerSymbolAAPL;
        CPTColor *aaplColor = [CPTColor redColor];
        [graph addPlot:aaplPlot toPlotSpace:plotSpace];
        CPTScatterPlot *googPlot = [[CPTScatterPlot alloc] init];
        googPlot.dataSource = self;
        googPlot.identifier = CPDTickerSymbolGOOG;
        CPTColor *googColor = [CPTColor greenColor];
        [graph addPlot:googPlot toPlotSpace:plotSpace];
        CPTScatterPlot *msftPlot = [[CPTScatterPlot alloc] init];
        msftPlot.dataSource = self;
        msftPlot.identifier = CPDTickerSymbolMSFT;
        CPTColor *msftColor = [CPTColor blueColor];
        [graph addPlot:msftPlot toPlotSpace:plotSpace];
        // 3 - Set up plot space
        [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:aaplPlot, googPlot, msftPlot, nil]];
        CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
        [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
        plotSpace.xRange = xRange;
        CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
        [yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
        plotSpace.yRange = yRange;
        // 4 - Create styles and symbols
        CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy];
        aaplLineStyle.lineWidth = 2.5;
        aaplLineStyle.lineColor = aaplColor;
        aaplPlot.dataLineStyle = aaplLineStyle;
        CPTMutableLineStyle *aaplSymbolLineStyle = [CPTMutableLineStyle lineStyle];
        aaplSymbolLineStyle.lineColor = aaplColor;
        CPTPlotSymbol *aaplSymbol = [CPTPlotSymbol ellipsePlotSymbol];
        aaplSymbol.fill = [CPTFill fillWithColor:aaplColor];
        aaplSymbol.lineStyle = aaplSymbolLineStyle;
        aaplSymbol.size = CGSizeMake(6.0f, 6.0f);
        aaplPlot.plotSymbol = aaplSymbol;
        CPTMutableLineStyle *googLineStyle = [googPlot.dataLineStyle mutableCopy];
        googLineStyle.lineWidth = 1.0;
        googLineStyle.lineColor = googColor;
        googPlot.dataLineStyle = googLineStyle;
        CPTMutableLineStyle *googSymbolLineStyle = [CPTMutableLineStyle lineStyle];
        googSymbolLineStyle.lineColor = googColor;
        CPTPlotSymbol *googSymbol = [CPTPlotSymbol starPlotSymbol];
        googSymbol.fill = [CPTFill fillWithColor:googColor];
        googSymbol.lineStyle = googSymbolLineStyle;
        googSymbol.size = CGSizeMake(6.0f, 6.0f);
        googPlot.plotSymbol = googSymbol;
        CPTMutableLineStyle *msftLineStyle = [msftPlot.dataLineStyle mutableCopy];
        msftLineStyle.lineWidth = 2.0;
        msftLineStyle.lineColor = msftColor;
        msftPlot.dataLineStyle = msftLineStyle;
        CPTMutableLineStyle *msftSymbolLineStyle = [CPTMutableLineStyle lineStyle];
        msftSymbolLineStyle.lineColor = msftColor;
        CPTPlotSymbol *msftSymbol = [CPTPlotSymbol diamondPlotSymbol];
        msftSymbol.fill = [CPTFill fillWithColor:msftColor];
        msftSymbol.lineStyle = msftSymbolLineStyle;
        msftSymbol.size = CGSizeMake(6.0f, 6.0f);
        msftPlot.plotSymbol = msftSymbol;

    //   // plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0)
    //                                                length:CPTDecimalFromInteger(10)];
    //    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0)
    //                                                    length:CPTDecimalFromInteger(2000)];
    //   
    }

    -(void)configureAxesDancis {
        // 1 - Create styles
        CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
        axisTitleStyle.color = [CPTColor whiteColor];
        axisTitleStyle.fontName = @"Helvetica-Bold";
        axisTitleStyle.fontSize = 12.0f;
        CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
        axisLineStyle.lineWidth = 2.0f;
        axisLineStyle.lineColor = [CPTColor whiteColor];
        CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
        axisTextStyle.color = [CPTColor whiteColor];
        axisTextStyle.fontName = @"Helvetica-Bold";
        axisTextStyle.fontSize = 11.0f;
        CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
        tickLineStyle.lineColor = [CPTColor whiteColor];
        tickLineStyle.lineWidth = 2.0f;
        CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
        tickLineStyle.lineColor = [CPTColor blackColor];
        tickLineStyle.lineWidth = 1.0f;
        // 2 - Get axis set
        CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
        // 3 - Configure x-axis
        CPTAxis *x = axisSet.xAxis;
        x.title = @"Age In Days";
        x.titleTextStyle = axisTitleStyle;
        x.titleOffset = 15.0f;
        x.axisLineStyle = axisLineStyle;
        x.labelingPolicy = CPTAxisLabelingPolicyNone;
        x.labelTextStyle = axisTextStyle;
        x.majorTickLineStyle = axisLineStyle;
        x.majorTickLength = 4.0f;
        x.tickDirection = CPTSignNegative;







        CGFloat dateCount = [[[CPDStockPriceStore sharedInstance] datesInMonth] count];
        NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
        NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
        NSInteger i = 0;
        for (NSString *date in [[CPDStockPriceStore sharedInstance] datesInMonth]) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date  textStyle:x.labelTextStyle];
            CGFloat location = i++;
            label.tickLocation = CPTDecimalFromCGFloat(location);
            label.offset = x.majorTickLength;
            if (label) {
                [xLabels addObject:label];
                [xLocations addObject:[NSNumber numberWithFloat:location]];
            }
        }
        x.axisLabels = xLabels;
        x.majorTickLocations = xLocations;



        // 4 - Configure y-axis
        CPTAxis *y = axisSet.yAxis;
        y.title = @"Width";
        y.titleTextStyle = axisTitleStyle;
        y.titleOffset = 10.0f;
        y.axisLineStyle = axisLineStyle;
        y.majorGridLineStyle = gridLineStyle;
        y.labelingPolicy = CPTAxisLabelingPolicyNone;
        y.labelTextStyle = axisTextStyle;
        y.labelOffset = -10.0f;
        y.majorTickLineStyle = axisLineStyle;
        y.majorTickLength = 4.0f;
        y.minorTickLength = 2.0f;
        y.tickDirection = CPTSignNegative;
        NSInteger majorIncrement = 100;
        NSInteger minorIncrement = 50;
        CGFloat yMax = 50000.0f;  // should determine dynamically based on max price
        NSMutableSet *yLabels = [NSMutableSet set];
        NSMutableSet *yMajorLocations = [NSMutableSet set];
        NSMutableSet *yMinorLocations = [NSMutableSet set];
        for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
            NSUInteger mod = j % majorIncrement;
            if (mod == 0) {
                CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
                NSDecimal location = CPTDecimalFromInteger(j);
                label.tickLocation = location;
                label.offset = -y.majorTickLength - y.labelOffset;
                if (label) {
                    [yLabels addObject:label];
                }
                [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
            } else {
                [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
            }
        }
        y.axisLabels = yLabels;
        y.majorTickLocations = yMajorLocations;
        y.minorTickLocations = yMinorLocations;

    }

如何绘制y2轴。使用核心绘图库。如何用两个y1和y2轴绘制线图.y1开始500.1000,1500,.....和y2开始10,20,30,40,.....和x轴开始20,25,30, 35,40,......

1 个答案:

答案 0 :(得分:0)

您需要为第二个y轴添加另一个绘图空间。使xRange与第一个绘图空间相同。 Plot Gallery 示例应用程序有几个演示。 &#34;剧情空间演示&#34;创建具有不同设置的多个绘图空间,并为每个绘图空间分配轴。 &#34; Axis Demo&#34;显示如何设置第二个y轴。