MiXen MiXen - 1 month ago 21
iOS Question

Implementing chartValueSelected method in Charts library for iOS

I have a application, that can display charts with data using iOS Charts library v.2.3. Now I must enabled option to track touching points of data on chart. I do some research and I know, that I must use chartValueSelected method in class, that implement . I also declare chartView.delegate to self and connect outlet in view on storyboard. But when I tested, always I get signal from chartValueNothingSelected method, none from chartValueSelected. It is something, that I should put in my code, that enable tracking touch on points in my application?

Thanks in advantance.

Edit: code



@interface ChartViewController () <ChartViewDelegate>

@end

@implementation ChartViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.chartView.delegate = self;
_chartView.dragEnabled = YES;
[_chartView setScaleEnabled:YES];
_chartView.pinchZoomEnabled = YES;
_chartView.drawGridBackgroundEnabled = NO;

// some random data generator
// x-axis limit line
ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
llXAxis.lineWidth = 4.0;
llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)];
llXAxis.labelPosition = ChartLimitLabelPositionRightBottom;
llXAxis.valueFont = [UIFont systemFontOfSize:10.f];

//[_chartView.xAxis addLimitLine:llXAxis];

_chartView.xAxis.gridLineDashLengths = @[@10.0, @10.0];
_chartView.xAxis.gridLineDashPhase = 0.f;

ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:150.0 label:@"Upper Limit"];
ll1.lineWidth = 4.0;
ll1.lineDashLengths = @[@5.f, @5.f];
ll1.labelPosition = ChartLimitLabelPositionRightTop;
ll1.valueFont = [UIFont systemFontOfSize:10.0];

ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"];
ll2.lineWidth = 4.0;
ll2.lineDashLengths = @[@5.f, @5.f];
ll2.labelPosition = ChartLimitLabelPositionRightBottom;
ll2.valueFont = [UIFont systemFontOfSize:10.0];

ChartYAxis *leftAxis = _chartView.leftAxis;
[leftAxis removeAllLimitLines];
[leftAxis addLimitLine:ll1];
[leftAxis addLimitLine:ll2];
leftAxis.gridLineDashLengths = @[@5.f, @5.f];
leftAxis.drawZeroLineEnabled = NO;
leftAxis.drawLimitLinesBehindDataEnabled = YES;

_chartView.rightAxis.enabled = NO;

//[_chartView.viewPortHandler setMaximumScaleY: 2.f];
//[_chartView.viewPortHandler setMaximumScaleX: 2.f];


//_chartView.marker = marker;

_chartView.legend.form = ChartLegendFormLine;

[_chartView animateWithXAxisDuration:2.5];

NSMutableArray *xVals = [[NSMutableArray alloc] init];

for (int i = 0; i < 100; i++)
{
[xVals addObject:[@(i) stringValue]];
}

NSMutableArray *yVals = [[NSMutableArray alloc] init];

for (int i = 0; i < 100; i++)
{
double mult = (1000 + 1);
double val = (double) (arc4random_uniform(mult)) + 3;
[yVals addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
}

LineChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
set1.yVals = yVals;
_chartView.data.xValsObjc = xVals;
[_chartView.data notifyDataChanged];
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[LineChartDataSet alloc] initWithYVals:yVals label:@"DataSet 1"];

set1.lineDashLengths = @[@5.f, @2.5f];
set1.highlightLineDashLengths = @[@5.f, @2.5f];
[set1 setColor:UIColor.blackColor];
[set1 setCircleColor:UIColor.blackColor];
set1.lineWidth = 1.0;
set1.circleRadius = 3.0;
set1.drawCircleHoleEnabled = NO;
set1.valueFont = [UIFont systemFontOfSize:9.f];
//set1.fillAlpha = 65/255.0;
//set1.fillColor = UIColor.blackColor;

NSArray *gradientColors = @[
(id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
(id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
];
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);

set1.fillAlpha = 1.f;
set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
set1.drawFilledEnabled = YES;

CGGradientRelease(gradient);

NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];

LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSets:dataSets];

_chartView.data = data;
}
}

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
{
NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
NSLog(@"chartValueNothingSelected");
}

@end

and from .h file

#import <UIKit/UIKit.h>
@import Charts;

@interface ChartViewController : UIViewController
@property(nonatomic, strong) IBOutlet LineChartView* chartView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *chartViewHeightConstraint;

@end




Answer

For everyone, who will occurr similar problem to mine. I resolve my problem, cleaning everything, that can be left of old version 2.3 of Charts using CocoaPods tools and later add reference to new 3.0 version. For me, it enabled option to show actually clicked point.

Comments