ios - 核心图:2Y 图缩放问题

标签 ios core-plot

我们使用 Core-Plot 实现了 2Y 散点图 - 输出是用 X 和 Y 轴绘制的一张图表,然后在顶部绘制一些空白区域,然后绘制 X 和 Y2 轴图表。

我们希望图表具有 X 和 y 轴,并且 X 和 Y2 轴图表重叠,以便用户可以正确查看。

示例代码在这里:

 -(void)configureGraph {
    graph = [[CPTXYGraph alloc] initWithFrame:CGRectMake(0, 0, self.hostView.bounds.size.width, self.hostView.bounds.size.height)];
    [graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
    self.hostView.hostedGraph = graph;

    [graph setPaddingTop:5.0f];
    [graph setPaddingLeft:5.0f];
    [graph setPaddingRight:5.0f];
    [graph setPaddingBottom:5.0f];
    [graph.plotAreaFrame setPaddingTop:5.0f];
    [graph.plotAreaFrame setPaddingRight:20.0f];
    [graph.plotAreaFrame setPaddingLeft:20.0f];
    [graph.plotAreaFrame setPaddingBottom:25.0f];
    graph.plotAreaFrame.borderLineStyle = nil;  
}

-(void)configurePlots {
    CPTColor *your_color = [CPTColor colorWithComponentRed:126 green:230 blue:255 alpha:0];
    int cntSelectedVar = 0;
    graph = self.hostView.hostedGraph;
    CPTXYPlotSpace *plotSpace  = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    CPTXYPlotSpace *plotSpace2 = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = NO;
    plotSpace2.allowsUserInteraction = NO;
//    

        // 1 - Get graph and plot space

        graph.plotAreaFrame.fill = [CPTFill fillWithColor:your_color];
        // 2 - Create the three plots
        CPTScatterPlot *aaplPlot = [[CPTScatterPlot alloc] init];
        aaplPlot.dataSource = self;
        aaplPlot.identifier = variableID ;
        CPTColor *aaplColor = [self  GetRGBForPlotSymbol:cntSelectedVar];
        [graph addPlot:aaplPlot toPlotSpace:plotSpace];
        CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];

        plotSpace.xRange = xRange;
        CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
        plotSpace.yRange = yRange;


        // 4 - Create styles and symbols
        CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy];
        aaplLineStyle.lineWidth = 0;
        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(3.0f, 3.0f);
        aaplPlot.plotSymbol = aaplSymbol;
        cntSelectedVar++;



        CPTScatterPlot *googPlot= [[CPTScatterPlot alloc] init];
        googPlot.dataSource = self;
        googPlot.identifier = variableID;
        [self.hostView.hostedGraph addPlot:googPlot];
        CPTColor *googColor = [self  GetRGBForPlotSymbol:cntSelectedVar];

plotSpace2.xRange = plotSpace.xRange;
    CPTMutablePlotRange *y2Range = [[CPTMutablePlotRange alloc] initWithLocation:CPTDecimalFromDouble(FinalMinValueY2) length:CPTDecimalFromDouble(FinalMaxValueY2)];
plotSpace2.yRange = y2Range ;
        CPTMutableLineStyle *googLineStyle = [googPlot.dataLineStyle mutableCopy];
        googLineStyle.lineWidth = 0;
        googLineStyle.lineColor = googColor;
        googPlot.dataLineStyle = googLineStyle;
        CPTMutableLineStyle *googSymbolLineStyle = [CPTMutableLineStyle lineStyle];
        googSymbolLineStyle.lineColor = googColor;
        CPTPlotSymbol *googSymbol = [CPTPlotSymbol ellipsePlotSymbol];
        googSymbol.fill = [CPTFill fillWithColor:googColor];
        googSymbol.lineStyle = googSymbolLineStyle;
        googSymbol.size = CGSizeMake(3.0f, 3.0f);
        googPlot.plotSymbol = googSymbol;
        [graph addPlot:googPlot toPlotSpace:plotSpace2];
        cntSelectedVar++;


    [plotSpace scaleToFitPlots:[graph allPlots]];

    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor blackColor];
    axisTitleStyle.fontName = @"Helvetica";
    axisTitleStyle.fontSize = 10.0f;
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle ];
    axisLineStyle.lineWidth = 1.0f;
    axisLineStyle.lineColor = [CPTColor blackColor];
    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
    axisTextStyle.color = [CPTColor blackColor];
    axisTextStyle.fontName = @"Helvetica";
    axisTextStyle.fontSize = 8.0f;
    CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor blackColor];
    tickLineStyle.lineWidth = 1.0f;
    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
    gridLineStyle.lineColor = [CPTColor grayColor];
    gridLineStyle.lineWidth = 1.0f;


    tickLineStyle.lineColor = [CPTColor blackColor];
    tickLineStyle.lineWidth = 1.0f;
    // 2 - Get axis set
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) (id)self.hostView.hostedGraph.axisSet;
    axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(OrignalMinValueY);

    CPTXYAxis *x = axisSet.xAxis;
    CPTXYAxis *y = axisSet.yAxis;






    x.titleTextStyle = axisTitleStyle;

    x.titleOffset = 12.0f;
    x.axisLineStyle = axisLineStyle;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.labelTextStyle = axisTextStyle;
    x.majorTickLineStyle = axisLineStyle;

    x.tickDirection = CPTSignNegative;
    x.minorTickLineStyle = nil;
    x.coordinate = CPTCoordinateX;

    CGFloat dateCount = [Reportdates count];
    NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
    NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];

    //    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:st  textStyle:x.labelTextStyle];


        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        NSMutableSet *xMajorLocations = [NSMutableSet set];
        [formatter setPositiveFormat:@"0.##" ];
        for (double j = FinalMinValueX; j <= FinalMaxValueX; j += FinalIntervalValueX) {
             CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[formatter stringFromNumber:[ NSNumber numberWithDouble:j ] ] textStyle:x.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j);
            label.tickLocation = location;
            label.offset = x.labelOffset;

            if (label) {
                [xLabels addObject:label];
            }
            [xMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];

        }
        x.axisLabels = xLabels;
        x.majorTickLocations = xLocations;


    // 4 - Configure y-axis

    //y.title = @"Price";
    y.titleTextStyle = axisTitleStyle;
    y.titleOffset = -40.0f;
    y.axisLineStyle = axisLineStyle;
    y.majorGridLineStyle = gridLineStyle;
    y.labelingPolicy = CPTAxisLabelingPolicyNone;
    y.labelTextStyle = axisTextStyle;
    y.majorTickLineStyle = axisLineStyle;
    y.tickDirection = CPTSignPositive;
    y.separateLayers= YES;


    double majorIncrement = FinalIntervalValueY;
    double minorIncrement = FinalIntervalValueY;
    CGFloat yMax = FinalMaxValueY;  // should determine dynamically based on max price
    NSMutableSet *yLabels = [NSMutableSet set];
    NSMutableSet *yMajorLocations = [NSMutableSet set];
    NSMutableSet *yMinorLocations = [NSMutableSet set];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setPositiveFormat:@"0.##" ];
    for (double j = FinalMinValueY; j <= yMax; j += minorIncrement) {
       CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[formatter stringFromNumber:[ NSNumber numberWithDouble:j ] ] textStyle:y.labelTextStyle];
        NSDecimal location = CPTDecimalFromInteger(j);
        label.tickLocation = location;
        label.offset = -20;
        if (label) {
            [yLabels addObject:label];
        }
        [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
    }
    y.axisLabels = yLabels;
    y.majorTickLocations = yMajorLocations;
    y.minorTickLocations = yMinorLocations;

    if( [ApplicationSettingOBJ.GB_VariableArrayY2 count ]>0)
    {
    CPTXYAxis *y2 = [[CPTXYAxis alloc] init] ;
    y2.titleTextStyle = axisTitleStyle;
    y2.coordinate = CPTCoordinateY;
    y2.plotSpace = plotSpace2;
    y2.labelingPolicy = CPTAxisLabelingPolicyNone;
    y2.separateLayers= NO;
    y2.majorGridLineStyle = nil;

    y2.labelTextStyle = axisTextStyle;
    y2.majorTickLineStyle = axisLineStyle;
    y2.tickDirection = CPTSignNone;
    y2.orthogonalCoordinateDecimal =CPTDecimalFromFloat(dateCount);
    majorIncrement = FinalIntervalValueY2;
    minorIncrement = FinalIntervalValueY2;
    yMax = FinalMaxValueY2;  // should determine dynamically based on max price
    NSMutableSet *yLabels1 = [NSMutableSet set];
    NSMutableSet *yMajorLocations1 = [NSMutableSet set];
    NSMutableSet *yMinorLocations1 = [NSMutableSet set];

        for (double j = FinalMinValueY2; j <= yMax; j += minorIncrement) {
                    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[formatter stringFromNumber:[ NSNumber numberWithDouble:j ] ] textStyle:y2.labelTextStyle];
                NSDecimal location = CPTDecimalFromInteger(j);
                label.tickLocation = location;
                label.offset = -20;
                if (label) {
                    [yLabels1 addObject:label];
                }
                [yMajorLocations1 addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];

        }

        y2.axisLabels = yLabels1;
        y2.majorTickLocations = yMajorLocations1;
        y2.minorTickLocations = yMinorLocations1;

        axisSet.axes = [NSArray arrayWithObjects:x, y,y2, nil];



}

最佳答案

plotSpaceplotSpace2 是同一个对象 (graph.defaultPlotSpace)。如果您想要两个不同的 y 尺度,请创建一个新的绘图空间并将其添加到图形中。

CPTXYPlotSpace *plotSpace2 = [[CPTXYPlotSpace alloc] init];
[graph addPlotSpace:plotSpace2];

关于ios - 核心图:2Y 图缩放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22963914/

相关文章:

label - core-plot 从轴标签中删除小数点

ios - 如何显示x轴标题?

ios - PHFetchResult 获取所有照片并按日期排序不一致

iOS 照片框架无法获取图像数据/UTI

ios - iOS 中的 InMobi 广告集成

ios - Google Maps API IOS 无法使用自定义 UIView 加载新坐标

ios - Core Plot - 条形图自动初始缩放

ios - 当缩放或移动 iosmap 时,它总是跳回到用户位置 Swift

ios - 不能再在 CorePlot 中子类化 CPTGraphHostingView (v2.3) 吗?

iphone - 核心图 : Updating X Axis range in real time plotting