Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 94857

textForAxis delegate method called multiple times

$
0
0

Hi Jack,

Your suggestion about limiting variables initialization during the textForAxis:value:atIndex: proved to be effective. However, my graph does not seem to limit the initial number of ticks regarding the available space for the graph. This means all dataPoints on graph have a tick, and that each of them is named right away. 

If the user zoom in on the graph though, the number of ticks is limited and looks OK. The behavior described above only happens when the xAxis.zoom value is 1, hence when there's no zoom at all. I do set the major tick interval to 1 when creating axes, so maybe the problem comes from there?

I've joined code snippets and screenshots to illustrate my saying. We use a numeric xAxis to represent temporal values, because they and not continuous and we wanted to prevent wholes of data in the graph for weekends and such off business days.

01.- (void)initChart
02.{
03.// Graphique
04.self.chart = [[TKChart alloc] initWithFrame:CGRectZero];
05.self.chart.delegate = self;
06.self.chart.dataSource = self;
07.[self.chart setInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)];
08.self.chart.translatesAutoresizingMaskIntoConstraints = NO;
09.[self addSubview:_chart];
10.[self bringSubviewToFront:self.chartPeriodSelector];
11.     
12.//Configuration axe X
13.TKChartNumericAxis *xAxis = [[TKChartNumericAxis alloc] initWithMinimum:((DCOStockDataPoint *)[self.chartData firstObject]).dataXValue andMaximum:((DCOStockDataPoint *)[self.chartData lastObject]).dataXValue];
14.xAxis.majorTickInterval = @1;
15.xAxis.style.majorTickStyle.ticksHidden = NO;
16.xAxis.style.labelStyle.fitMode = TKChartAxisLabelAlignmentBottom | TKChartAxisLabelAlignmentVerticalCenter;
17.xAxis.allowZoom = YES;
18.xAxis.allowPan = YES;
19.self.chart.xAxis = xAxis;
20. 
21.// Configuration axe Y
22.NSNumber *yMin = ((DCOStockDataPoint *)self.chartData[0]).low;
23.NSNumber *yMax = ((DCOStockDataPoint *)self.chartData[0]).high;
24. 
25.for (DCOStockDataPoint *dataPoint in self.chartData)
26.    if ([dataPoint.high compare:yMax] == NSOrderedDescending) {
27.        yMax = dataPoint.high;
28.    }
29.         
30.    if ([dataPoint.low compare:yMin] == NSOrderedAscending) {
31.       yMin = dataPoint.low;
32.    }
33.}
34.double pct = (0.05 * ([yMax doubleValue] - [yMin doubleValue]));
35.double adjustedYMin = [yMin doubleValue] - pct < 0 ? 0 : [yMin doubleValue] - pct;
36. 
37.TKChartNumericAxis *yAxis = [[TKChartNumericAxis alloc] initWithMinimum:[NSNumber numberWithDouble:adjustedYMin] andMaximum:[NSNumber numberWithDouble:[yMax doubleValue]+pct]];
38.yAxis.style.labelStyle.textAlignment = TKChartAxisLabelAlignmentRight | TKChartAxisLabelAlignmentBottom;
39.yAxis.style.labelStyle.firstLabelTextAlignment = TKChartAxisLabelAlignmentRight | TKChartAxisLabelAlignmentTop;
40.yAxis.style.lineHidden = YES;
41.yAxis.position = TKChartAxisPositionLeft;
42.[yAxis setMajorTickInterval:[NSNumber numberWithDouble:([yMax doubleValue] - [yMin doubleValue]) / 4]];
43.self.chart.yAxis = yAxis;
44.     
45.// On initie le style de la grille
46.self.chart.gridStyle.horizontalFill = [TKSolidFill solidFillWithColor:[UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0]];
47.self.chart.gridStyle.horizontalAlternateFill = [TKSolidFill solidFillWithColor:[UIColor whiteColor]];
48.     
49.// TrackBall
50.self.chart.allowTrackball = YES;
51.self.chart.trackball.orientation = TKChartTrackballOrientationVertical;
52.self.chart.trackball.tooltip.pinPosition = TKChartTrackballPinPositionNone;
53.self.chart.trackball.line.hidden = NO;
54.self.chart.trackball.snapMode = TKChartTrackballSnapModeAllClosestPoints;
55.self.chart.trackball.tooltip.style.textAlignment = NSTextAlignmentLeft;
56.}
57. 
58.- (NSString *)chart:(TKChart *)chart textForAxis:(TKChartAxis *)axis value:(id)value atIndex:(NSUInteger)index
59.{
60.    //Index du tick par rapport au nombre complet de ticks du graphique
61.    NSInteger tickIndexInGraph = (NSInteger) roundl([value doubleValue]);
62.    NSString *textToReturn = @"";
63.     
64.    // Si on a l'axe des X, que l'index est un entier et est plus petit que le nombre de donnees dans l'array
65.    if(!axis.isVertical) {
66.        if(tickIndexInGraph < self.chartData.count && tickIndexInGraph == [value doubleValue]) {
67.            DCOStockDataPoint *dataPoint = self.chartData[tickIndexInGraph];
68.            if(chart.xAxis.zoom != 1) {
69.                textToReturn = [dataPoint dateStringForPeriod:self.chartPeriod];
70.            } else if (tickIndexInGraph == 0 || tickIndexInGraph == self.chartData.count-1) {
71.                textToReturn = [dataPoint dateStringForPeriod:self.chartPeriod];
72.            }
73.        }
74.    }
75.    // Si on a l'axe des Y
76.    else {
77.        textToReturn = [[[DCOFormatterManager manager] numberFormatterWithTwoDecimals] stringFromNumber:value];
78.    }
79.     
80.    return textToReturn;
81.}

 


Viewing all articles
Browse latest Browse all 94857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>