Force JBChartView to use a 0-100 Y-axis

1k Views Asked by At

I'm using the excellent JBChartView in my project and It's working great. Here's my current code:

self.lineChart = [[JBLineChartView alloc] initWithFrame:CGRectMake(320, 400, 680, 300)];
self.lineChart.dataSource = self;
self.lineChart.delegate = self;
self.lineChart.backgroundColor = [UIColor clearColor];
JBLineChartFooterView *footerView = [[JBLineChartFooterView alloc] initWithFrame:CGRectMake(10, ceil(self.view.bounds.size.height * 0.5) - ceil(10 * 0.5), self.view.bounds.size.width - (10 * 2), 10)];
footerView.backgroundColor = [UIColor clearColor];
footerView.leftLabel.text = @"yesterday";
footerView.leftLabel.textColor = [UIColor whiteColor];
footerView.rightLabel.text = @"now";
footerView.rightLabel.textColor = [UIColor whiteColor];
footerView.sectionCount = 10;
self.lineChart.footerView = footerView;
[self.view addSubview:self.lineChart];
[self.lineChart setState:JBChartViewStateCollapsed animated:YES];

Nothing fancy, really.

JBChartView automatically chooses the Y-axis scale based on what data is passed to it in lineChartView: heightForIndex:. This is nice in most situations. My data, however, is on CPU usage, and thus I'd like to force the Y-axis to always be 0 at the bottom and 100 at the top.

Actually, I'd like it to be 0 on the bottom and MAX(100, highestPoint) at the top (because CPU usage can sometimes be over 100%) - is this possible?

2

There are 2 best solutions below

0
On

As of v2.2.1, JBChartView allows you to supply a minimumValue and/or maximumValue for the y-axis.

Notes:

  • If no value(s) are supplied, the min and max values of the chart's data source are used.
  • If value(s) are supplied, they must be >= 0, otherwise an assertion will be thrown.
  • The min/max values are clamped to the ceiling and floor of the actual min/max values of the chart's data source.
  • For example, if a maximumValue of 20 is supplied & the chart's actual max is 100, then 100 will be used.
  • Lastly, for min/max modifications to take effect, reloadData must be called.

Hope this helps!

0
On

It turned out that all I needed to do was modify line 240 of JBLineChartView.m:

self.cachedMaxHeight = maxHeight;

to say:

self.cachedMaxHeight = MAX(100,maxHeight);