Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display label MMyyyy for barChart #2142

Closed
perwyl opened this issue Feb 6, 2017 · 4 comments
Closed

Display label MMyyyy for barChart #2142

perwyl opened this issue Feb 6, 2017 · 4 comments

Comments

@perwyl
Copy link

perwyl commented Feb 6, 2017

Hi

I need to display the bar chart data according to MMyyyy, may i know how should i approach this?

I tried converting the Date to double using expirationDate.timeIntervalSinceDate and convert it back to date at func stringForValue(_ value: Double, axis: AxisBase?) -> String {}

The barData's color is gone. I can see it's top label value though

Some Observations as well:
let yData = BarChartDataEntry(x: month, y: value) //month = 1475 251 200

func stringForValue(_ value: Double, axis: AxisBase?) -> String { } //value = 1464 000 000 and increment by 2 000 000

screen shot 2017-02-06 at 7 18 44 pm

@DoctorG
Copy link

DoctorG commented Feb 7, 2017

Same issue here. Hope this objective-c code from my app helps you out.

I think what you want to do is implement IAxisValueFormatter delegate.

@interface UndeliveredGraphDueByMonthViewController: UIViewController <ChartViewDelegate, IChartAxisValueFormatter>

I stored my xLabels as an array of dates but discovered the delegate was calling for items out of the array bounds and crashing the program, so I ended up using a starting date and implanting a function I otherwise use to add months to the start month/year for the graph.

Here is my chart setup for xAxis:
ChartXAxis *xAxis = self.chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.axisMinimum = -0.5; //subtract 0.5 to show all of first bar
//set axismax after counting xLabels
xAxis.granularity = 1.0;
xAxis.centerAxisLabelsEnabled = YES;
xAxis.labelRotationAngle = -45;
xAxis.valueFormatter = self; //IAxisformatter

Here's the delegate callback:

#pragma mark - IAxisValueFormatter

  • (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"MMMyyyy" options:0 locale:[NSLocale currentLocale]]];
    NSDate *startMY = xLabels[0]; //first date for xValues
    NSDate *currentMY = [AppDelegate addMonthsToDate:startMY monthsToAdd:value];
    NSString *retDate = [dateFormatter stringFromDate:currentMY];
    NSLog(@"value: %f date:%@", value,retDate);
    return retDate;
    }

Heres my add month function incase you need it:
+(NSDate *)addMonthsToDate:(NSDate *)referenceDate monthsToAdd:(long)months {
NSDate *nextMonth = nil;

if (referenceDate) {
	NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
	NSDateComponents *offsetComponents = [[NSDateComponents alloc]init];

	[offsetComponents setMonth:months];
	nextMonth = [gregorian dateByAddingComponents:offsetComponents toDate:referenceDate options:0];
}
return (nextMonth);

}

@st-small
Copy link

@DoctorG Thank you for example! I'm working with it but can't do that I want, can you help me?! I have array with NSDate objects and I want to show them in xAxis. When I use [yVals addObject:[[BarChartDataEntry alloc] initWithX:x y:val]]; I use double parameter something like (NSTimeInterval tInt = [[NSDate date] timeIntervalSinceDate:self.dates[i]];), but when I want to get string from my x value, using delegate IAxisValueFormatter, it shows another date that I need...

Please, need in your help! TNX!

@liuxuan30
Copy link
Member

@st-small it's about data formatter issues, so maybe ask on SO and be more specific.
As I see you get the interval, as long as you pass the write value, it can't be too diferent dates..

@st-small
Copy link

st-small commented Apr 13, 2017

@liuxuan30 Thank you for reply! I did it! It was very simple! In method setChart... I use int i with steps 0,1,2,...n and in formatter method I use my dates and make strings from them to show as labels in xAxis. But have question, can I show yAxis values in format integer value, without .00? I want to see value, for example, 7, but now showing 7.00. Tnx!

Update: just now see your reply in other thread about valueformatter, ok, will try to have that I want, thank you very much!!!

@perwyl perwyl closed this as completed Jul 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants