Вопрос

How to show a Label on Bar at Bottom?, I am using below method and setting labeloffset, but it shows label on top (In the image label text "Test" is displaying in the middle of bar, but i need to display bottom).

enter image description here

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
 static CPTMutableTextStyle *labelText = nil;

if ( !labelText ) {
    labelText       = [[CPTMutableTextStyle alloc] init];
    labelText.color = [CPTColor redColor];
}

NSString *labelValue = @"Test";

NSDictionary *testsDict = [assmentsTestsForGraphArray objectAtIndex:index];
NSInteger score = [[testsDict valueForKey:@"Score"] integerValue];

CPTTextLayer *newLayer = [[CPTTextLayer alloc] initWithText:labelValue style:labelText];

plot.labelOffset = -score;//0;//-10;

 return newLayer;
}
Это было полезно?

Решение

There are at least three different ways to do this:

  1. Create a second plot (bar plot or scatter plot; it doesn't really matter) and give it the same x-values as the original plot but set all of the y-values equal to the vertical position of the x-axis. Label the second plot and not the first. Set up the second plot so it doesn't draw anything except the label, i.e., set all line styles and fills to nil.

  2. Use custom axis labels on the x-axis rather than plot data labels.

  3. Create plot space annotations for each label instead of plot data labels.

Другие советы

Sorry, I wasn't thinking right yesterday. The beginning of my first answer remains the same.

Bar plot's labels are (to the best of my very limited knowledge) set to the max value of a bar. So in order to get the label offset to the bottom of the graph, you need to to come up with an algorithm that works well for the graph you have. The best place to set the offset, in my opinion, is in the numberForPlot delegate function when the fieldEnum is CPTBarPlotFieldBarTip. I couldn't spend very much time figuring out the proper algorithm, but if you use the Plot_Gallery_iOS example project and edit VerticalBarChart.m, you'll find a wide range of offsets you have to use. Here's what little I did find though:

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    NSNumber *num = nil;

    if ( fieldEnum == CPTBarPlotFieldBarLocation ) {
        // location
        if ( [plot.identifier isEqual:@"Bar Plot 2"] ) {
            num = [NSDecimalNumber numberWithInt:index];
        }
        else {
            num = [NSDecimalNumber numberWithInt:index];
        }
    }
    else if ( fieldEnum == CPTBarPlotFieldBarTip ) {
        // length
        if ( [plot.identifier isEqual:@"Bar Plot 2"] ) {
            num = [NSDecimalNumber numberWithInt:index];
        }
        else {
            num = [NSDecimalNumber numberWithInt:(index + 1) * (index + 1)];
            plot.labelOffset = -1.0;
            //-1.0 works well for the first bar, -345.0 works well for the last bar
        }
    }
    else {
        // base
        if ( [plot.identifier isEqual:@"Bar Plot 2"] ) {
            num = [NSDecimalNumber numberWithInt:0];
        }
        else {
            num = [NSDecimalNumber numberWithInt:index];
        }
    }

    return num;
}

-1.0 works well for the first bar (height of 1 unit), -345.0 works well for the last bar (height of 100 units). Sorry I led you in the wrong direction yesterday and that I don't have enough time to help you come up with a good algorithm, but hopefully that helps point you in the right direction.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top