如何缩放图形以适合可见空间 [英] How to scale graph to fit to visible space

查看:47
本文介绍了如何缩放图形以适合可见空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用核心图(和条形图)来显示财务信息。

I'm using core-plot (and the barcharts) to display financial information.

我的问题是,每当我使用负值时,所显示图形的比例似乎是错误的。我希望整个图形都可见,但是通常只显示图形的一部分,使条形被切掉,有时甚至看不到x轴(显示的部分在y轴上太远了) 。如果我打开了用户互动功能,则可以在视图中滚动以查看图表的不同部分,但是我需要一个概述,而不是详细研究图表。请注意,我并未将整个屏幕用于图表,而仅使用了大约300x240像素。

My problem is that whenever I use negative values, it seems that the scale of the shown graph is wrong. I'd like the entire graph to be visible, but often only a part of the graph is shown, leaving the bars cut off and sometimes not even the x-axis visible (the part shown is so far down on the y-axis). If I turn on the user-interaction, it's possible to scroll within the view to see different parts of the graph, but I want an overview, not study the graph in detail. Note that I do not use the whole screen for the graph, but only roughly 300x240px.

由于某些原因,不允许新用户发布图像,因此我无法附加屏幕截图。如果有人认为它可以帮助您解决问题(并且有时间),我很乐意向您发送两张截屏,分别显示正值和负值。

For some reason new users are not allowed to post images so I can not attach screenshots. If someone thinks it can help with the answer (and has the time) I'd be happy to send you two screenshots of how it looks with only positive values and how it looks with negative values.

我非常感谢我能获得的任何帮助。谢谢。

I'd really appreciate any help I can get. Thank you.

我用来生成图形的代码如下:

- (id)initWithFrame:(CGRect)frame:(FinancialTopicData *) topicDataToSet:(NSArray *) colors {

//Assert correct data
assert([topicDataToSet isKindOfClass:[FinancialTopicData class]]);

if ((self = [super initWithFrame:frame])) {

    self.topicData = topicDataToSet;

    //Create the graph
    graph = [[CPXYGraph alloc] initWithFrame:CGRectZero];

    CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme];
    [graph applyTheme:theme];
    self.hostedLayer = graph;

    // Border
    graph.plotAreaFrame.borderLineStyle = nil;
    graph.plotAreaFrame.cornerRadius = 0.0f;

    // Paddings
    graph.paddingLeft = 0.0f;
    graph.paddingRight = 0.0f;
    graph.paddingTop = 0.0f;
    graph.paddingBottom = 0.0f;
    graph.titlePlotAreaFrameAnchor = CPRectAnchorTop;

    //Plot padding
    graph.plotAreaFrame.paddingLeft = 70.0;
    graph.plotAreaFrame.paddingTop = 10;
    graph.plotAreaFrame.paddingRight = 20.0;
    graph.plotAreaFrame.paddingBottom = 45.0;

    NSDecimalNumber *intervalDivider = [NSDecimalNumber decimalNumberWithString:@kIntervalDivider];

    // Add plot space for horizontal bar charts
    NSDecimalNumber *numberDecimalValueMaxUp = [[topicData maxValueRoundedUp] decimalNumberByAdding:[[[topicData maxValueRoundedUp] decimalNumberBySubtracting:[topicData minValueRoundedDown]] decimalNumberByDividingBy:intervalDivider]];
    NSDecimal decimalValueMaxUp = [numberDecimalValueMaxUp decimalValue];
    NSDecimal decimalValueMinDown = [[topicData minValueRoundedDown] decimalValue];
    NSInteger numberOfTypes = [[topicData years] count];
    NSDecimal numberOfBars = [[NSNumber numberWithInteger:numberOfTypes] decimalValue];
    NSDecimalNumber *yInterval = [[numberDecimalValueMaxUp decimalNumberBySubtracting:[topicData minValueRoundedDown]] decimalNumberByDividingBy:intervalDivider];

    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;

    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:decimalValueMinDown   length:decimalValueMaxUp];

    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:numberOfBars];


    //Add x-axis
    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
    CPXYAxis *x = axisSet.xAxis;
    x.majorTickLineStyle = nil;
    x.minorTickLineStyle = nil;
    x.majorIntervalLength = CPDecimalFromString(@"1");
    x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");

    // Define some custom labels for the data elements
    x.labelRotation = M_PI/4;
    x.labelingPolicy = CPAxisLabelingPolicyNone;


    NSMutableArray *customTickLocations = [NSMutableArray new];
    NSArray *xAxisLabels = [topicData years];

    for(int i = 0; i < [xAxisLabels count];i++){
        [customTickLocations addObject:[NSDecimalNumber numberWithFloat:0.5f+i*1.0f]];
    }

    NSUInteger labelLocation = 0;
    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];

    for (NSNumber *tickLocation in customTickLocations) {

        CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
        newLabel.tickLocation = [tickLocation decimalValue];
        newLabel.offset = x.labelOffset + x.majorTickLength*(numberOfTypes/2);
        newLabel.rotation = M_PI/4;
        [customLabels addObject:newLabel];
        [newLabel release];
    }

    x.axisLabels =  [NSSet setWithArray:customLabels];

    [customTickLocations release];

    CPXYAxis *y = axisSet.yAxis;
    y.minorTickLineStyle = nil;
    y.majorIntervalLength = [yInterval decimalValue];
    y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");

    int i = 0;

    for(NSString *key in [topicData types]){

        CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[colors objectAtIndex:(i%[colors count])] horizontalBars:NO];
        i++;

        barPlot.baseValue = CPDecimalFromString(@"0");
        barPlot.dataSource = topicData;
        barPlot.barOffset = 1.0f+i;
        barPlot.identifier = key;
        barPlot.delegate = topicData;
        barPlot.needsDisplayOnBoundsChange = YES;    

        [graph addPlot:barPlot toPlotSpace:plotSpace];
    }


}
return self;
}

数据源方法:

-(NSNumber *)numberForPlot:(CPPlot *)plot 
                 field:(NSUInteger)fieldEnum 
           recordIndex:(NSUInteger)index  {
NSNumber *num = nil;    
if ( [plot isKindOfClass:[CPBarPlot class]] ) {                        
    switch ( fieldEnum ) {
        case CPBarPlotFieldBarLocation:{
            num = [NSDecimalNumber numberWithUnsignedInteger:index];
            break;
        }    
        case CPBarPlotFieldBarLength:{
            NSString *stringValue = [[[types objectForKey:plot.identifier] objectForKey:[years objectAtIndex:index]] objectForKey:KEY_COMPANY_FINANCIALS_VALUE];                
            num = [formatter numberFromString:stringValue];
           break;
        }
    }
}
return num;
}


-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{
return [years count];
}


推荐答案

<$ c的长度$ c> plotSpace.yRange 应该是 decimalValueMaxUp decimalValueMinDown 之间的差。

这篇关于如何缩放图形以适合可见空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆