I am using IGChart in one of my IOS projects and couldn't find how to display legend view on it. I have checked the infragistics SDK but the given 2 lines of code snippet regarding how to use legend with IGChart is not clear. So if you could add an example about legend usage with IGChart I will be pleased. Also here is what I am doing with Objective C:
@property (strong, nonatomic) IGLegend *chartLegend;
Firstly, I am initialising the legend within a function.
self.chartLegend = [[IGLegend alloc] initWithLegendType:IGChartLegendTypeSeries];[self.chartLegend setFrame:CGRectMake(10, 10, 200, 100)];self.chartLegend.translatesAutoresizingMaskIntoConstraints = NO;self.chartLegend.backgroundColor = [MyColor gray25];self.chartView.legend = self.chartLegend;[self.view addSubview:self.chartLegend];
In another function I am adding my IGLineSeries to my chart:
IGCategorySeriesDataSourceHelper *categorySource = [[IGCategorySeriesDataSourceHelper alloc] init];categorySource.data = lineChartArr; categorySource.valuePath = @"close";categorySource.labelPath = @"date";
lineSeries = [[IGLineSeries alloc] initWithKey:@"chartSeries"];
lineSeries.xAxis = xAxis;
lineSeries.yAxis = yAxis;
lineSeries.yAxis.minimum = -20;
lineSeries.yAxis.maximum = 20;
lineSeries.dataSource = categorySource;
lineSeries.xAxis.majorStrokeThickness = 0.6f;
lineSeries.yAxis.majorStrokeThickness = 0.6f;
lineSeries.xAxis.majorStroke = [[IGBrush alloc]initWithColor:[MyColor gray25]];
lineSeries.yAxis.majorStroke = [[IGBrush alloc]initWithColor:[MyColor gray25]];
lineSeries.title = title;
lineSeries.legendItemIsVisible = YES;
lineSeries.legend = self.chartLegend;
[self.chartView addSeries:lineSeries];
So what am I doing wrong? These codes only display a gray view without the titles. Thanks for your answers.
I looked over your code but didn't see anything obvious that would prevent the legends from showing. Attached is a sample showing a mock up of how they work.
Please modify it to show the behavior you are seeing.
Hi Darrell, thank you for the code example. After I checked it I realised that IGLegend displays the titles only if it is created in - (void)viewDidLoad in my project. So, I need to find a way to update it's titles. In my project, if I create IGLegend in - (void)viewDidLoad { } there is no problem. I am using the following code snippet to create IGLegend.
-(void)makeLegend
{
chartLegend = [[IGLegend alloc] initWithFrame:CGRectMake(30, 30, 200, 200) andLegendType:IGChartLegendTypeSeries];
[self.view addSubview:chartLegend];
self.chartView.legend = chartLegend;
chartLegend.backgroundColor = [MyColor gray25];
}
....
lineSeries.title = title;lineSeries.legendItemIsVisible = YES;
But as a user when I tap on "update chart" button on the ViewController, the chart graph changes( pulling data from service, populating the chart etc. ) perfectly but the legend stays on the screen without displaying anything. I tried to remove and add the legend after the chart updating processes but it didn't work.
So how can I update the legend? Any suggestions?