Wednesday, 15 May 2013

ios - Bar chart/UIView animation conundrum -



ios - Bar chart/UIView animation conundrum -

i'm trying create simple bar chart showing relative durations of sequentially recorded activities, , can't life of me display properly. it's based on simple uiview animations, , parts of seem work, namely drop shadow under bar. however, bars never appear.

i'm doubly flummoxed, because modeled code closely on bar chart within app work. real difference driving info , orientation of bars.

here's code, followed console readout:

-(void) displaydaychart { nstimeinterval timeframe = -86400; // 24 hours test, selectable in real life nsdate *daystart = [nsdate datewithtimeintervalsincenow:timeframe]; nsdate *ratnow = [nsdate date]; int y = (self.chartview.frame.size.height) - 5; nslog(@"daystart %@",daystart); // info source // predicate retrieve activities stopped after origin of timeframe, plus running 1 actpredicate = [nspredicate predicatewithformat:@"(starttime >= %@ , stoptime <= %@) or ((starttime <= %@ , stoptime >= %@) or (starttime <= %@ , stoptime == null))",daystart,ratnow,daystart,daystart,ratnow]; // fetch activities nsfetchedresultscontroller *dayactivityfrc = [timedactivity mr_fetchallsortedby:@"starttime" ascending:no withpredicate:actpredicate groupby:nil delegate:nil]; // int tacount = [timedactivity mr_countofentities]; // nslog(@"total of timedactivities %d",tacount); int grabbedactivities = dayactivityfrc.fetchedobjects.count; nslog(@"total of grabbedactivities %d",grabbedactivities); nslog(@"number of timedactivities in dayactivityfrc %d",dayactivityfrc.fetchedobjects.count); daychartbar *thisbar; timedactivity *thisitem; for(int i=0; i<(dayactivityfrc.fetchedobjects.count); i++) { thisbar = [[daychartbar alloc] initwithframe:cgrectzero]; thisbar.tag = i+1; [self.chartview addsubview:thisbar]; thisitem = [dayactivityfrc.fetchedobjects objectatindex:i]; // increment vertical location of bar y = y - 20; // calculate top (currently timing) activity's duration if (thisitem.stoptime == nil) { nsnumber *n = ([nsnumber numberwithdouble: abs([thisitem.starttime timeintervalsincedate:[nsdate date]])]); thisitem.duration = n; } // calculate bottom activity's duration if (thisitem.starttime < daystart && thisitem.stoptime > daystart) { nsnumber *n = ([nsnumber numberwithdouble: abs([thisitem.stoptime timeintervalsincedate:daystart])]); thisitem.duration = n; // nslog(@"%@ bottom item",thisitem.name); } // calculate middle activities' duration if (thisitem.starttime > daystart && thisitem.stoptime < ratnow) { nsnumber *n = ([nsnumber numberwithdouble: abs([thisitem.stoptime timeintervalsincedate:thisitem.starttime])]); thisitem.duration = n; } nslog(@"check loop %@",thisitem.name); nslog(@"starttime %@",thisitem.starttime); nslog(@"stoptime %@",thisitem.stoptime); nslog(@"duration %@\n",thisitem.duration); // width of view = 280 // calculate width of bar (proportionate length of activity vs timeframe, relative available space in chart view) int w = ((280) * ([thisitem.duration doublevalue])) / (abs(timeframe)); nslog(@"w = %d",w); // create animated bar of appropriate color , size [uiview animatewithduration:.3 delay:.2 options: uiviewanimationcurveeaseout // deprecated, still works animations:^ { // starting state thisbar.frame = cgrectmake(20, y, 0, 15); thisbar.backgroundcolor = [uicolor blackcolor]; // end state thisbar.frame = cgrectmake(20, y, w, 15); nslog(@"thisbar.frame %@",nsstringfromcgrect (thisbar.frame)); thisbar.backgroundcolor = thisitem.color; thisbar.layer.shadowcolor = [[uicolor blackcolor] cgcolor]; thisbar.layer.shadowopacity = 0.7; thisbar.layer.shadowradius = 4.0; thisbar.layer.shadowoffset = cgsizemake(5.0f, 5.0f); thisbar.layer.shadowpath = [uibezierpath bezierpathwithrect:thisbar.bounds].cgpath; nslog(@"bar created!"); } completion:^(bool finished) { // reserved creating name label @ end of each bar nslog(@"bar completed!"); }]; } }

and console readout:

2014-06-20 11:05:17.910 wmdgx[46607:a0b] number of activities 2 2014-06-20 11:05:22.793 wmdgx[46607:a0b] daystart 2014-06-19 18:05:22 +0000 2014-06-20 11:05:22.794 wmdgx[46607:a0b] total of grabbedactivities 8 2014-06-20 11:05:22.795 wmdgx[46607:a0b] number of timedactivities in dayactivityfrc 8 2014-06-20 11:05:22.796 wmdgx[46607:a0b] check loop dusting 2014-06-20 11:05:22.796 wmdgx[46607:a0b] starttime 2014-06-20 17:29:04 +0000 2014-06-20 11:05:22.797 wmdgx[46607:a0b] stoptime (null) 2014-06-20 11:05:22.797 wmdgx[46607:a0b] duration 0 2014-06-20 11:05:22.798 wmdgx[46607:a0b] w = 0 2014-06-20 11:05:22.798 wmdgx[46607:a0b] thisbar.frame {{20, 261}, {0, 15}} 2014-06-20 11:05:22.799 wmdgx[46607:a0b] bar created! 2014-06-20 11:05:22.800 wmdgx[46607:a0b] check loop test 1 2014-06-20 11:05:22.800 wmdgx[46607:a0b] starttime 2014-06-20 17:19:22 +0000 2014-06-20 11:05:22.801 wmdgx[46607:a0b] stoptime 2014-06-20 17:29:04 +0000 2014-06-20 11:05:22.801 wmdgx[46607:a0b] duration 581 2014-06-20 11:05:22.802 wmdgx[46607:a0b] w = 1 2014-06-20 11:05:22.803 wmdgx[46607:a0b] thisbar.frame {{20, 241}, {1, 15}} 2014-06-20 11:05:22.803 wmdgx[46607:a0b] bar created! 2014-06-20 11:05:22.804 wmdgx[46607:a0b] check loop dusting 2014-06-20 11:05:22.804 wmdgx[46607:a0b] starttime 2014-06-20 16:52:10 +0000 2014-06-20 11:05:22.805 wmdgx[46607:a0b] stoptime 2014-06-20 17:19:22 +0000 2014-06-20 11:05:22.805 wmdgx[46607:a0b] duration 1632 2014-06-20 11:05:22.806 wmdgx[46607:a0b] w = 5 2014-06-20 11:05:22.808 wmdgx[46607:a0b] thisbar.frame {{20, 221}, {5, 15}} 2014-06-20 11:05:22.809 wmdgx[46607:a0b] bar created! 2014-06-20 11:05:22.810 wmdgx[46607:a0b] check loop test 1 2014-06-20 11:05:22.811 wmdgx[46607:a0b] starttime 2014-06-20 16:52:04 +0000 2014-06-20 11:05:22.811 wmdgx[46607:a0b] stoptime 2014-06-20 16:52:10 +0000 2014-06-20 11:05:22.812 wmdgx[46607:a0b] duration 6 2014-06-20 11:05:22.812 wmdgx[46607:a0b] w = 0 2014-06-20 11:05:22.813 wmdgx[46607:a0b] thisbar.frame {{20, 201}, {0, 15}} 2014-06-20 11:05:22.813 wmdgx[46607:a0b] bar created! 2014-06-20 11:05:22.814 wmdgx[46607:a0b] check loop timer sleeping 2014-06-20 11:05:22.814 wmdgx[46607:a0b] starttime 2014-06-20 16:29:06 +0000 2014-06-20 11:05:22.816 wmdgx[46607:a0b] stoptime 2014-06-20 16:52:04 +0000 2014-06-20 11:05:22.816 wmdgx[46607:a0b] duration 1378 2014-06-20 11:05:22.817 wmdgx[46607:a0b] w = 4 2014-06-20 11:05:22.818 wmdgx[46607:a0b] thisbar.frame {{20, 181}, {4, 15}} 2014-06-20 11:05:22.818 wmdgx[46607:a0b] bar created! 2014-06-20 11:05:22.820 wmdgx[46607:a0b] check loop dusting 2014-06-20 11:05:22.821 wmdgx[46607:a0b] starttime 2014-06-20 16:28:53 +0000 2014-06-20 11:05:22.821 wmdgx[46607:a0b] stoptime 2014-06-20 16:29:06 +0000 2014-06-20 11:05:22.822 wmdgx[46607:a0b] duration 12 2014-06-20 11:05:22.823 wmdgx[46607:a0b] w = 0 2014-06-20 11:05:22.824 wmdgx[46607:a0b] thisbar.frame {{20, 161}, {0, 15}} 2014-06-20 11:05:22.825 wmdgx[46607:a0b] bar created! 2014-06-20 11:05:22.826 wmdgx[46607:a0b] check loop test 1 2014-06-20 11:05:22.827 wmdgx[46607:a0b] starttime 2014-06-20 16:28:38 +0000 2014-06-20 11:05:22.827 wmdgx[46607:a0b] stoptime 2014-06-20 16:28:53 +0000 2014-06-20 11:05:22.827 wmdgx[46607:a0b] duration 14 2014-06-20 11:05:22.828 wmdgx[46607:a0b] w = 0 2014-06-20 11:05:22.828 wmdgx[46607:a0b] thisbar.frame {{20, 141}, {0, 15}} 2014-06-20 11:05:22.829 wmdgx[46607:a0b] bar created! 2014-06-20 11:05:22.829 wmdgx[46607:a0b] check loop timer sleeping 2014-06-20 11:05:22.829 wmdgx[46607:a0b] starttime 2014-06-20 16:25:35 +0000 2014-06-20 11:05:22.830 wmdgx[46607:a0b] stoptime 2014-06-20 16:28:38 +0000 2014-06-20 11:05:22.831 wmdgx[46607:a0b] duration 183 2014-06-20 11:05:22.832 wmdgx[46607:a0b] w = 0 2014-06-20 11:05:22.834 wmdgx[46607:a0b] thisbar.frame {{20, 121}, {0, 15}} 2014-06-20 11:05:22.834 wmdgx[46607:a0b] bar created! 2014-06-20 11:05:23.348 wmdgx[46607:a0b] bar completed! 2014-06-20 11:05:23.350 wmdgx[46607:a0b] bar completed! 2014-06-20 11:05:23.351 wmdgx[46607:a0b] bar completed! 2014-06-20 11:05:23.352 wmdgx[46607:a0b] bar completed! 2014-06-20 11:05:23.352 wmdgx[46607:a0b] bar completed! 2014-06-20 11:05:23.353 wmdgx[46607:a0b] bar completed! 2014-06-20 11:05:23.354 wmdgx[46607:a0b] bar completed! 2014-06-20 11:05:23.355 wmdgx[46607:a0b] bar completed!

i hope dumbass case of not seeing forest trees, i've been beating brains out 3 days before bringing so. know it's not proofreading site.

here's screenshot showing drop shadows (and absence of bars themselves). drop shadows appear in right location , right size based on data:

edit***********

following comments rdelmar , @gro below, changed line:

thisbar.backgroundcolor = thisitem.color;

to:

thisbar.backgroundcolor = [uicolor redcolor];

which produced screenshot:

so, believing problem have been identified, went , changed this:

thisbar = [[daychartbar alloc] initwithframe:cgrectzero]; thisbar.tag = i+1; [self.chartview addsubview:thisbar]; thisitem = [dayactivityfrc.fetchedobjects objectatindex:i];

to this

thisbar = [[daychartbar alloc] initwithframe:cgrectzero]; thisbar.tag = i+1; [self.chartview addsubview:thisbar]; thisitem = [dayactivityfrc.fetchedobjects objectatindex:i]; thisbar.endcolor = thisitem.color;

and (in animation block):

thisbar.backgroundcolor = thisitem.color;

to this:

thisbar.backgroundcolor = thisbar.endcolor;

and bars have disappeared 1 time again.

second edit***************

i've had quite bit of backtracking, bars displaying, albeit bit strangely. problem (the primary 1 @ least) colors assigned each item through it's associated category, passed on timedactivity, represented bars. yeah, know, kinda convoluted. need cleaning up.

in case, looks now. said, behavior little weird--for example, top bar has drop shadow , no bar, @ to the lowest degree i'm seeing some colored bars. however, drop shadows, inexplicably, beingness created before bars themselves. or appear.

i thought i'd wrap up, since no 1 has contributed 24 hours, , problem stated has been resolved.

the color of each bar intended set sort of round-about core info scheme of relationships. 1 time straightened out, colored bars appeared expected.

the other (unstated) problem there apparent randomness lengths of bars relative available space. @ point view arithmetic issue.

thanks @rdelmar , @gro, both of comments pointed me core info problem.

ios bar-chart uiviewanimation

No comments:

Post a Comment