objective c - Add missing years and corresponding values into arrays -
i've been messing around jbchartview library , seems charting. it's easy enough use i'm having problems getting data in format need particular chart.
the user can enter value , corresponding year. saved using core data. data follows:
year: 0 value: 100 year:2 value 200 year 3 value 150
i create 2 arrays, 1 year number , value. in case though, 3 bars. i'd bar value 0 year 1.
i think best way approach through year array, check see if first value 0, check if every consecutive year value +1. if not, add 1 previous year , insert value of 0 values array @ same index position.
i know if best approach , if doing comparison.
thanks
ok got answer own question , thought i'd post may in future, when creating charts using this, or other libraries.
i first populate 2 mutable arrays
chartlegend = [nsmutablearray arraywithobjects:@1,@3, nil]; chartdata = [nsmutablearray arraywithobjects:@"100",@"300", nil];
so i've got years 1 , 3, each associated value in chartdata array.
i need create year 0 , year 2 bar chart has bar every year 0 maximum year, 3.
- (void)additemstoarray { (int i=0; i<[chartlegend count]; i++) { //get values our array required calculations int intpreviousvalue = 0; int intcurrentvalue = [[chartlegend objectatindex:i]integervalue]; if (i>0) { intpreviousvalue = [[chartlegend objectatindex:(i-1)]integervalue]; } //deal first item in array should 0 if (i == 0) { if (intcurrentvalue != 0) { [chartlegend insertobject:[nsnumber numberwithint:0] atindex:i]; [chartdata insertobject:[nsnumber numberwithint:0] atindex:i]; } } //now deal other array items else if (intcurrentvalue - intpreviousvalue !=1) { int intnewvalue = intpreviousvalue +1; [chartlegend insertobject:[nsnumber numberwithint:intnewvalue] atindex:i]; [chartdata insertobject:[nsnumber numberwithint:0] atindex:i]; } } //create string of values in array nsstring *dates = [chartlegend componentsjoinedbystring:@","]; nsstring *values = [chartdata componentsjoinedbystring:@","]; //display text in couple of labels check intended result self.yearslabel.text = dates; self.valueslabel.text = values;
}
that seems working me. should easy enough populate arrays using coredata information, make sure it's sorted first.
Comments
Post a Comment