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?
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, 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?
Whether this is a bug or not is something we won't be able to discern without a sample.
I am going to ask again, can you either modify the sample I sent you to reproduce the behavior or create a small sample that shows the behavior.
We cannot determine what is causing the issue without being able to see the issue. If it is a bug we would need a sample so we can track down the behavior. If it's not a bug but a sample issue, we wouldn't be able to tell without looking at code.
My only other guess is that when you are swapping out your items you are disconnecting the legend from the chart. But that is only a guess.
Hi Darrell,
I have created a sample project. I tried to replicate my current project and IGLegend doesn't work in this sample too. If you could check the attached sample project and give me some suggestions to fix this issue I will be pleased. Thank you again.
Short version :
Instead of setting the .theme property to nil , set it to the default theme
chartView.theme = [IGChartDefaultThemes DefaultTheme];
//chartView.theme = nil;
Long version:
In the application code, a blank theme is being created and a few properties are being set. When that is nilled out later, there isn't any style information for the legend, so when rendering happens, the legend is blanked out. So rather than nil, go back to the default theme since that is probably what you are trying to do anyway.
found the answer, chartLegend.orientation = IGOrientationVertical;
Thank you Darrell again.
Thank you so much Darrell; [IGChartDefaultThemes DefaultTheme] worked for me. Also just a quick one, how can I list the items vertically on the IGLegend? Thank you.