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.
Hi Darrell, unfortunately I couldn't fixed my problem. Your example is working properly. I am doing the same steps but IGChart doesn't display the legend values.
At first the chart is displayed with one IGFinancialPriceSeries series. When the users taps on the update button, I remove all the axes and series from the chart:
-(void)removeSeriesAndAxes { if (self.chartMode == Line) {
for (IGSeries *series in seriesArrayValues) { // seriesArrayValues is a MutableArray which holds the current series [self.chartView removeSeries:series]; } seriesArrayValues = [[NSMutableArray alloc] init]; for (IGAxis *axis in self.chartView.axes) { [self.chartView removeAxis:axis]; } }}
then add two IGLineSeries series:
if (self.chartView.axes.count <= 0) { xAxis = [[IGCategoryXAxis alloc] initWithKey:@"xAxis"]; yAxis = [[IGNumericYAxis alloc] initWithKey:@"yAxis"]; xAxis.labelOrientationAngle = 45; if (ix == 0) { xAxis.interval = openArr.count / 6; }
[self.chartView addAxis:xAxis]; [self.chartView addAxis:yAxis]; }
categorySource = [[IGCategorySeriesDataSourceHelper alloc] init]; categorySource.data = lineChartArr; categorySource.valuePath = @"close"; categorySource.labelPath = @"date";
lineSeries = [[IGLineSeries alloc] initWithKey:@"lineSeries"];
[seriesArrayValues addObject:lineSeries];
self.chartView.theme = nil;
self.chartView.animateSeriesWhenAxisRangeChanges = YES;
Only gray legend without displaying the values. It is kind a bug?
I updated the sample to show the legend on a button click rather then viewDidLoad. I am not seeing the behavior you are describing. I do see an issue in which the background color of the legend appears to get lost when resetting data after the legend is displayed, so that is a bug. But the legend is still showing data and that data is correct.
Can you modify the attached sample to show what 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?
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.