ArthurGuibert/FSLineChart

Minimum Y-value greater than zero?

canyondellomo opened this issue Β· 20 comments

Sorry, another question. Is there a way to have the graph's y-axis not start at zero? I have a data set that ranges from 20000-25000, and don't want all of the excess displayed.

You can do it, but you need a bit of pre-processing before. If you know the value you want your chart to start, you can offset all the values by 20000 (in your case), and then in the labelForValue block, you offset the value that is going to be displayed by 20000.

Example:
lineChart.labelForValue = ^(CGFloat value) { return [NSString stringWithFormat:@"%f", value + 20000]; };

Oh, fantastic. Thanks a bunch.

On Aug 15, 2016, at 5:11 PM, Arthur notifications@github.com wrote:

You can do it, but you need a bit of pre-processing before. If you know the value you want your chart to start, you can offset all the values by 20000 (in your case), and then in the labelForValue block, you offset the value that is going to be displayed by 20000.

Example:
lineChart.labelForValue = ^(CGFloat value) {
return [NSString stringWithFormat:@"%f", value + 20000];
};

β€”
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub #35 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/AL77bWyTzLpRpwVkQcuNTfSXhbWxfmcBks5qgNYBgaJpZM4JkxH5.

@canyondellomo can you tell me how you did this exactly in the code? Thanks!

Hey Arthur-

So I'm not around the code I used for that project, but conceptually you do the following:

  1. Wherever in your project you create your data matrix, add in the Y-axis adjustment- this is as simple as subtracting a hard coded value from the entire matrix.
  2. In your plotting method, when you label your Y-axis, simply add the adjustment back in to the value when printing your string.

That seemed to work for me. If you're still having trouble ping me again and I'll go find the actual code.

Best of luck!

C

On Nov 1, 2016, at 4:30 AM, Alifar notifications@github.com wrote:

@canyondellomo can you tell me how you did this exactly in the code? Thanks!

β€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@canyondellomo yes please if you could be so kind πŸ‘

This is the code I have now, but same as you I know the minimum value, where do I need to put the minimum value and take it with the grid?

NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:self.lineData.count];
    NSMutableArray* months = [NSMutableArray arrayWithCapacity:self.lineData.count];
    for(int i=0;i<self.lineData.count;i++) {

        Kobject *z = [self.lineData objectAtIndex:i];
        chartData[i] = [NSNumber numberWithFloat:[z.oPrice floatValue]];
        months[i] = z.oDate;
    }

    self.lineChart = [[FSLineChart alloc] initWithFrame:CGRectMake(15, 188, [UIScreen mainScreen].bounds.size.width - 30, 120)];
    self.lineChart.verticalGridStep = 6;
    self.lineChart.horizontalGridStep = 7;
    self.lineChart.fillColor = [UIColor colorWithRed:0.18f green:0.67f blue:0.84f alpha:0.20f];
    self.lineChart.color = [self.lineChart.dataPointColor colorWithAlphaComponent:0.3];
    self.lineChart.valueLabelPosition = ValueLabelLeftMirrored;
    self.lineChart.indexLabelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10];
    self.lineChart.valueLabelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:9];
    self.lineChart.labelForIndex = ^(NSUInteger item) {
        return months[item];
    };
    self.lineChart.labelForValue = ^(CGFloat value) {

        return [NSString stringWithFormat:@"€ %.2f", value];
    };
    [self.lineChart setChartData:chartData];

Ah okay.

So self.lineData contains the numerical information. Wherever that gets originally populated, subtract your adjustment from each value.

So for instance, in my code, in viewDidLoad, self.visVals, an NSMutableArray, gets filled by a series of JSON objects. Each value it loads in, I subtracted 120 from it because I wanted my Y-axis to start at 120.

int vis=[[metrics objectAtIndex:2] intValue];
vis=vis-120;
[self.visVals addObject:[NSNumber numberWithInt:vis]];

Super simple there. Then, in the plotting area (which you included above), under the labelForValue component, simply call out the value and add your adjustment back to it.

So in mine:

self.visChart.labelForValue = ^(CGFloat value) {
        value=value+120;
        return [NSString stringWithFormat:@"%.f",value];

    };

See if that works!

@canyondellomo it works, thanks a million! πŸ‘

No problem! Glad it worked out.

On Nov 1, 2016, at 3:54 PM, Alifar notifications@github.com wrote:

@canyondellomo https://github.com/canyondellomo it works, thanks a million! πŸ‘

β€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #35 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/AL77bZLC4ZlYcw5l8grehG2qnlIsjBQ7ks5q55kJgaJpZM4JkxH5.

@canyondellomo @ArthurGuibert would you guys mind take a look at the following issue I have?

see: #41

Thanks in advance.

Thank you for your time C,

The whole code looks as followed:

float n = [[sort objectAtIndex:0] floatValue];
        
        NSString* forFloat = [NSString stringWithFormat:@"%.3f", n];
        lowFloat = [forFloat floatValue]-0.001;

NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:self.lineData.count];
            NSMutableArray* months = [NSMutableArray arrayWithCapacity:self.lineData.count];
            for(int i=0;i<self.lineData.count;i++) {
                
                Kobject *z = [self.lineData objectAtIndex:i];
                chartData[i] = [NSNumber numberWithFloat:[z.oPrice floatValue]-lowFloat];
                months[i] = z.oDate;
            }
            
            self.lineChart = [[FSLineChart alloc] initWithFrame:CGRectMake(15, 44, [UIScreen mainScreen].bounds.size.width - 30, 120)];
            self.lineChart.verticalGridStep = 6;
            self.lineChart.horizontalGridStep = 7;
            self.lineChart.fillColor = [UIColor colorWithRed:0.24 green:0.72 blue:0.68 alpha:0.20];
            self.lineChart.color = [self.lineChart.dataPointColor colorWithAlphaComponent:0.3];
            self.lineChart.valueLabelPosition = ValueLabelLeftMirrored;
            self.lineChart.indexLabelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10];
            self.lineChart.valueLabelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:9];
            self.lineChart.labelForIndex = ^(NSUInteger item) {
                return months[item];
            };
            self.lineChart.labelForValue = ^(CGFloat value) {
                
                return [NSString stringWithFormat:@"$ %.2f", value+lowFloat];
            };
            [self.lineChart setChartData:chartData];
            [view addSubview:self.lineChart];

Would you mind pointing me what I need to add more and where? Also, it only shows the strange behaviour when the values are above 10.

Thanks in advance.

That corrected the linechart, but gives the wrong values, see screenshot:

schermafbeelding 2017-06-23 om 16 33 20

Hmmm now it shows this, getting a bit confused here lol

schermafbeelding 2017-06-23 om 16 41 08

If I remove the lowfloat completely it shows the right line.

Well, when I remove it on the values that are set lower then 10 it will have pretty much no change in the linechart, see:

schermafbeelding 2017-06-23 om 16 46 11

So, I will make an if statement if the value goes above then do it your way πŸ‘

Thanks again man!