Search code examples
ios-charts

Combined Charts in iOS-Chart - Negative values for Line Chart are not rendered


I am using io-Charts (https://github.com/danielgindi/Charts/). I will have to show a bar chart and a line chart in the combined chart. The xAxis will reflect values from -12 to 12 and y value ranges between -3000 to 3000.While the bar diagram is rendered properly for negative y and x axis , the line chart is not showing for values less than 0.As suggested in the following link ,

Negative values not displayed in line charts using ios-charts using Swift

I have set the _chartView.xAxis.axisMinimum = -12; // to start from -12 and not 0.

Please find below the current graph , enter image description here

The Setting for the chart view and data set for the line chart ,

    - (LineChartData *)generateLineData

{ LineChartData *d = [[LineChartData alloc] init];

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

[entries addObject:[[ChartDataEntry alloc] initWithX:0 y:-3000]];
[entries addObject:[[ChartDataEntry alloc] initWithX:0.5 y:-999]];
[entries addObject:[[ChartDataEntry alloc] initWithX:1 y:-1000]];
[entries addObject:[[ChartDataEntry alloc] initWithX:2 y:-1000]];
[entries addObject:[[ChartDataEntry alloc] initWithX:3.5 y:-1300]];
[entries addObject:[[ChartDataEntry alloc] initWithX:4 y:-900]];
[entries addObject:[[ChartDataEntry alloc] initWithX:5 y:-1200]];
[entries addObject:[[ChartDataEntry alloc] initWithX:6 y:-2500]];
[entries addObject:[[ChartDataEntry alloc] initWithX:8 y:-1300]];
[entries addObject:[[ChartDataEntry alloc] initWithX:9 y:1400]];
[entries addObject:[[ChartDataEntry alloc] initWithX:11 y:-900]];
[entries addObject:[[ChartDataEntry alloc] initWithX:12 y:-3000]];
[entries addObject:[[ChartDataEntry alloc] initWithX:-1 y:-1000]];
[entries addObject:[[ChartDataEntry alloc] initWithX:-2 y:-1300]];
[entries addObject:[[ChartDataEntry alloc] initWithX:-4 y:-1100]];
[entries addObject:[[ChartDataEntry alloc] initWithX:-5 y:-1500]];
[entries addObject:[[ChartDataEntry alloc] initWithX:-6 y:-1500]];
[entries addObject:[[ChartDataEntry alloc] initWithX:-8 y:-1100]];
[entries addObject:[[ChartDataEntry alloc] initWithX:-9 y:-1000]];
[entries addObject:[[ChartDataEntry alloc] initWithX:-11 y:-1500]];

LineChartDataSet *set = [[LineChartDataSet alloc] initWithValues:entries label:@"Line DataSet"];
[set setColor:[UIColor colorWithRed:240/255.f green:238/255.f blue:70/255.f alpha:1.f]];
set.lineWidth = 2.5;
set.fillColor = [UIColor colorWithRed:240/255.f green:238/255.f blue:70/255.f alpha:1.f];
set.mode = LineChartModeHorizontalBezier;
set.drawValuesEnabled = NO;
set.drawCirclesEnabled = NO;
set.axisDependency = AxisDependencyLeft;

[d addDataSet:set];

return d;

}

I am not sure if I am missing some setting for the chartview.


Solution

  • Fixed this by reordering the line chart data set with x values from -12 to 12 , in my original question , the data set was mixed and not in a proper order for the line chart.