在 HorizontalBarChart 中切割最后一个条形图
Cutting Last Bar chart in HorizontalBarChart
我在 Charts
图形库中遇到问题。
我正在使用HorizontalBarChartView
遇到以下问题:
下面是我的图表设置代码:
- (void)setupBarChartForPinData {
switch (_selectedLegend) {
case kAppGraphAllLegend:
_barPinChartHeightConstarins.constant = (self.mutArrPinList.count*2)*15;
break;
case kAppGraphLegend1:
_barPinChartHeightConstarins.constant = (self.mutArrPinList.count*15);
break;
case kAppGraphLegend2:
_barPinChartHeightConstarins.constant = self.mutArrPinList.count*15;
break;
default:
break;
}
[UIView animateWithDuration:0.2
animations:^{
// Called on parent view
[self.view layoutIfNeeded];
_scrView.contentSize = CGSizeMake(_scrView.contentSize.width,_containerView.frame.size.height + 8 );
}];
_barChartPins.delegate = self;
_barChartPins.drawBarShadowEnabled = NO;
//_barChartPins.minOffset = 0;
_barChartPins.dragEnabled = YES;
_barChartPins.dragDecelerationEnabled = YES;
_barChartPins.chartDescription.enabled = NO;
_barChartPins.drawValueAboveBarEnabled = YES;
_barChartPins.leftAxis.axisMinimum = 0;
ChartXAxis *xAxis = _barChartPins.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.drawGridLinesEnabled = YES;
xAxis.labelCount = self.mutArrPinList.count;
xAxis.valueFormatter = self;
xAxis.drawAxisLineEnabled = YES;
xAxis.labelFont = kAPPCiscoSansTTNormal(10.0);
xAxis.labelTextColor = kAppPrimaryHeading;
ChartYAxis *leftAxis = _barChartPins.leftAxis;
leftAxis.drawGridLinesEnabled = NO;
_barChartPins.leftAxis.enabled = NO;
_barChartPins.leftAxis.drawAxisLineEnabled = NO;
_barChartPins.rightAxis.enabled = NO;
_barChartPins.rightAxis.drawAxisLineEnabled = NO;
_barChartPins.legend.enabled = NO;
[self setPinGraphData];
}
设置数据:
- (void)setPinGraphData
{
NSMutableArray *arrVals1 = [NSMutableArray new];
NSMutableArray *arrVals2 = [NSMutableArray new];
[self.mutArrPinList enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[arrVals1 addObject:[[BarChartDataEntry alloc]
initWithX:idx
yValues:@[[NSNumber numberWithDouble:[obj[@"outstanding"] doubleValue]]]]];
[arrVals2 addObject:[[BarChartDataEntry alloc]
initWithX:idx
yValues:@[[NSNumber numberWithDouble:[obj[@"incoming"] doubleValue]]]]];
}];
BarChartDataSet *set1 = nil;
BarChartDataSet *set2 = nil;
if (_barChartPins.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_barChartPins.data.dataSets[0];
set1.values = arrVals1;
if (_barChartPins.data.dataSetCount > 1) {
set2 = (BarChartDataSet *)_barChartPins.data.dataSets[1];
set2.values = arrVals2;
}
[_barChartPins.data notifyDataChanged];
[_barChartPins notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithValues:arrVals1];
set2 = [[BarChartDataSet alloc] initWithValues:arrVals2];
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
switch (_selectedLegend) {
case kAppGraphAllLegend:{
[set1 setColor:kAppLegend1GraphColor];
[set2 setColor:kAppLegend2GraphColor];
[dataSets addObject:set1];
[dataSets addObject:set2];
break;
}
case kAppGraphLegend1:{
[set1 setColor:kAppLegend1GraphColor];
[dataSets addObject:set1];
break;
}
case kAppGraphLegend2:{
[set2 setColor:kAppLegend2GraphColor];
[dataSets addObject:set2];
break;
}
default:
break;
}
BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
if (_selectedLegend == kAppGraphAllLegend) {
//Setup fro GroupBars
double groupSpace = 0.3;
double barSpace = 0.05;
double barWidth = 0.3;
// (0.3 + 0.05) * 2 + 0.3 = 1.00 -> interval per "group"
// (baSpace + barWidth) * dataSet count + groupSpace = 1.0
int groupCount = arrVals1.count;
int startYear = 0;
data.barWidth = barWidth;
_barChartPins.xAxis.axisMinimum = startYear;
[data groupBarsFromX:0 groupSpace:groupSpace barSpace:barSpace];
double groupWidth = [data groupWidthWithGroupSpace:groupSpace barSpace:barSpace];
_barChartPins.xAxis.axisMaximum = startYear + groupWidth * groupCount;
_barChartPins.xAxis.centerAxisLabelsEnabled = YES;
}else{
_barChartPins.xAxis.centerAxisLabelsEnabled = NO;
}
[data setDrawValues:YES];
[data setValueTextColor:kAppPrimaryHeading];
[data setValueFont:kAPPCiscoSansTTNormal(8.0f)];
_barChartPins.data = data;
}
}
我同时使用组柱和单个柱,在组柱中它工作正常但在单个柱中它切割最后一个柱。
任何帮助将不胜感激。
提前致谢。
我找到了上述问题的解决方案。
正如我所说,我正在为启用组图执行条件代码,这会产生问题,因为我正在根据组 space 和条形宽度手动更改轴的最小值和最大值。
data.barWidth = barWidth;
_barChartPins.xAxis.axisMinimum = startYear;
_barChartPins.xAxis.axisMaximum = startYear + groupWidth * groupCount;
由于上面的代码,当我删除组并选择 Normal one 时,它会采用我的自定义最大值和最小值 xAxis
值,因此我们必须像下面那样重置 xAxis
最小值和最大值。
_barChartPins.xAxis.centerAxisLabelsEnabled = NO;
if ([_barChartPins.xAxis isAxisMinCustom]) {
[_barChartPins.xAxis resetCustomAxisMin];
}
if ([_barChartPins.xAxis isAxisMaxCustom]) {
[_barChartPins.xAxis resetCustomAxisMax];
}
data.barWidth = 0.5f;
这将解决我的问题,即在顶部额外 space 并在底部切条。
希望这能帮助其他人解决同样的问题。
谢谢。
我在 Charts
图形库中遇到问题。
我正在使用HorizontalBarChartView
遇到以下问题:
下面是我的图表设置代码:
- (void)setupBarChartForPinData {
switch (_selectedLegend) {
case kAppGraphAllLegend:
_barPinChartHeightConstarins.constant = (self.mutArrPinList.count*2)*15;
break;
case kAppGraphLegend1:
_barPinChartHeightConstarins.constant = (self.mutArrPinList.count*15);
break;
case kAppGraphLegend2:
_barPinChartHeightConstarins.constant = self.mutArrPinList.count*15;
break;
default:
break;
}
[UIView animateWithDuration:0.2
animations:^{
// Called on parent view
[self.view layoutIfNeeded];
_scrView.contentSize = CGSizeMake(_scrView.contentSize.width,_containerView.frame.size.height + 8 );
}];
_barChartPins.delegate = self;
_barChartPins.drawBarShadowEnabled = NO;
//_barChartPins.minOffset = 0;
_barChartPins.dragEnabled = YES;
_barChartPins.dragDecelerationEnabled = YES;
_barChartPins.chartDescription.enabled = NO;
_barChartPins.drawValueAboveBarEnabled = YES;
_barChartPins.leftAxis.axisMinimum = 0;
ChartXAxis *xAxis = _barChartPins.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.drawGridLinesEnabled = YES;
xAxis.labelCount = self.mutArrPinList.count;
xAxis.valueFormatter = self;
xAxis.drawAxisLineEnabled = YES;
xAxis.labelFont = kAPPCiscoSansTTNormal(10.0);
xAxis.labelTextColor = kAppPrimaryHeading;
ChartYAxis *leftAxis = _barChartPins.leftAxis;
leftAxis.drawGridLinesEnabled = NO;
_barChartPins.leftAxis.enabled = NO;
_barChartPins.leftAxis.drawAxisLineEnabled = NO;
_barChartPins.rightAxis.enabled = NO;
_barChartPins.rightAxis.drawAxisLineEnabled = NO;
_barChartPins.legend.enabled = NO;
[self setPinGraphData];
}
设置数据:
- (void)setPinGraphData
{
NSMutableArray *arrVals1 = [NSMutableArray new];
NSMutableArray *arrVals2 = [NSMutableArray new];
[self.mutArrPinList enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[arrVals1 addObject:[[BarChartDataEntry alloc]
initWithX:idx
yValues:@[[NSNumber numberWithDouble:[obj[@"outstanding"] doubleValue]]]]];
[arrVals2 addObject:[[BarChartDataEntry alloc]
initWithX:idx
yValues:@[[NSNumber numberWithDouble:[obj[@"incoming"] doubleValue]]]]];
}];
BarChartDataSet *set1 = nil;
BarChartDataSet *set2 = nil;
if (_barChartPins.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_barChartPins.data.dataSets[0];
set1.values = arrVals1;
if (_barChartPins.data.dataSetCount > 1) {
set2 = (BarChartDataSet *)_barChartPins.data.dataSets[1];
set2.values = arrVals2;
}
[_barChartPins.data notifyDataChanged];
[_barChartPins notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithValues:arrVals1];
set2 = [[BarChartDataSet alloc] initWithValues:arrVals2];
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
switch (_selectedLegend) {
case kAppGraphAllLegend:{
[set1 setColor:kAppLegend1GraphColor];
[set2 setColor:kAppLegend2GraphColor];
[dataSets addObject:set1];
[dataSets addObject:set2];
break;
}
case kAppGraphLegend1:{
[set1 setColor:kAppLegend1GraphColor];
[dataSets addObject:set1];
break;
}
case kAppGraphLegend2:{
[set2 setColor:kAppLegend2GraphColor];
[dataSets addObject:set2];
break;
}
default:
break;
}
BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
if (_selectedLegend == kAppGraphAllLegend) {
//Setup fro GroupBars
double groupSpace = 0.3;
double barSpace = 0.05;
double barWidth = 0.3;
// (0.3 + 0.05) * 2 + 0.3 = 1.00 -> interval per "group"
// (baSpace + barWidth) * dataSet count + groupSpace = 1.0
int groupCount = arrVals1.count;
int startYear = 0;
data.barWidth = barWidth;
_barChartPins.xAxis.axisMinimum = startYear;
[data groupBarsFromX:0 groupSpace:groupSpace barSpace:barSpace];
double groupWidth = [data groupWidthWithGroupSpace:groupSpace barSpace:barSpace];
_barChartPins.xAxis.axisMaximum = startYear + groupWidth * groupCount;
_barChartPins.xAxis.centerAxisLabelsEnabled = YES;
}else{
_barChartPins.xAxis.centerAxisLabelsEnabled = NO;
}
[data setDrawValues:YES];
[data setValueTextColor:kAppPrimaryHeading];
[data setValueFont:kAPPCiscoSansTTNormal(8.0f)];
_barChartPins.data = data;
}
}
我同时使用组柱和单个柱,在组柱中它工作正常但在单个柱中它切割最后一个柱。
任何帮助将不胜感激。
提前致谢。
我找到了上述问题的解决方案。
正如我所说,我正在为启用组图执行条件代码,这会产生问题,因为我正在根据组 space 和条形宽度手动更改轴的最小值和最大值。
data.barWidth = barWidth;
_barChartPins.xAxis.axisMinimum = startYear;
_barChartPins.xAxis.axisMaximum = startYear + groupWidth * groupCount;
由于上面的代码,当我删除组并选择 Normal one 时,它会采用我的自定义最大值和最小值 xAxis
值,因此我们必须像下面那样重置 xAxis
最小值和最大值。
_barChartPins.xAxis.centerAxisLabelsEnabled = NO;
if ([_barChartPins.xAxis isAxisMinCustom]) {
[_barChartPins.xAxis resetCustomAxisMin];
}
if ([_barChartPins.xAxis isAxisMaxCustom]) {
[_barChartPins.xAxis resetCustomAxisMax];
}
data.barWidth = 0.5f;
这将解决我的问题,即在顶部额外 space 并在底部切条。
希望这能帮助其他人解决同样的问题。
谢谢。