我在我的项目中使用了https://github.com/danielgindi/Charts库。它工作得很好,但我想只在用户点击栏内时选择栏数据。目前它也会返回点击外的栏数据。它提供了用户点击栏外时最接近点击的栏数据。
我把条形图做成这样:
_chartView.delegate = self;
_chartView.drawBarShadowEnabled = NO;
_chartView.drawValueAboveBarEnabled = YES;
_chartView.maxVisibleValueCount = 60;
ChartXAxis *xAxis = _chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelTextColor = [UIColor whiteColor];
xAxis.labelFont = [UIFont systemFontOfSize:20.f];
xAxis.drawGridLinesEnabled = NO;
xAxis.spaceBetweenLabels = 2.0;
xAxis.labelWidth = 100;
ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:20.f];
leftAxis.labelCount = 5;
leftAxis.labelTextColor = [UIColor whiteColor];
leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
leftAxis.valueFormatter.maximumFractionDigits = 1;
leftAxis.valueFormatter.negativeSuffix = @"k";
leftAxis.valueFormatter.positiveSuffix = @"k";
leftAxis.valueFormatter.positivePrefix = @"$";
leftAxis.valueFormatter.negativePrefix = @"$";
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
leftAxis.spaceTop = 0.15;
leftAxis.customAxisMin = 0.0; // this replaces startAtZero = YES
_chartView.legend.position = ChartLegendPositionBelowChartLeft;
_chartView.legend.form = ChartLegendFormSquare;
_chartView.legend.formSize = 20.0;
_chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20.f];
_chartView.legend.xEntrySpace = 4.0;
_chartView.legend.textColor = [UIColor whiteColor];
NSMutableArray *xVals = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
[xVals addObject:months[i % 12]];
}
NSMutableArray *yVals = [[NSMutableArray alloc] init];
NSMutableArray *yValsColor = [[NSMutableArray alloc] init];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:10 xIndex:1]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:15 xIndex:2]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:20 xIndex:3]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:45 xIndex:4]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:30 xIndex:5]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:5 xIndex:6]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:15 xIndex:7]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:22 xIndex:8]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:32 xIndex:9]];
[yValsColor addObject:[UIColor yellowColor]];
BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"Charges"];
set1.barSpace = 0.35;
set1.colors = yValsColor;
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
[data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:20.f]];
[data setValueTextColor:[UIColor whiteColor]];
_chartView.data = data;
NSArray *arrLabelData = [NSArray arrayWithObjects:@"All", @"1/1/2016 to 9/29/2016", @"All", @"All", @"Date of Billed",nil];
NSArray *arrLabel = [NSArray arrayWithObjects:@"Ins:",@" DOS:",@" Location:",@" Provider:",@" Aging Calculation Based On:",nil];
NSMutableAttributedString *strFS = [[NSMutableAttributedString alloc] init];
UIColor *color = [UIColor yellowColor]; // select needed color
NSDictionary *attrs = @{ NSForegroundColorAttributeName : color};
NSDictionary *attrsLab = @{ NSForegroundColorAttributeName : [UIColor whiteColor]};
for(int i = 0; i <arrLabelData.count; i++)
{
NSString *strLabData = [arrLabelData objectAtIndex:i];
NSString *strLab = [arrLabel objectAtIndex:i];
NSAttributedString *attStringLab = [[NSAttributedString alloc] initWithString:strLab attributes:attrsLab];
NSAttributedString *attStringLabData = [[NSAttributedString alloc] initWithString:strLabData attributes:attrs];
[strFS appendAttributedString:attStringLab];
[strFS appendAttributedString:attStringLabData];
}
lblAttributes.attributedText = strFS;
字符串
我想回应的数据只有当用户点击内栏。
有谁知道吗?
5条答案
按热度按时间njthzxwz1#
我的应用程序中有用户版本2.2.3的图表。请尝试此实现来解决您的问题
1)在ChartHighlighter.swift文件中添加函数。
字符串
2)更新ChartHighlighter.swift文件中的getHighlight函数。
型
希望这对你有帮助。快乐编码.
zqdjd7g92#
我有一个使用相同库的项目,我能够通过将ChartHighlighter.swift文件中的getHighlight函数更改为以下内容来获得您所要求的功能:
字符串
mspsb9vt3#
我将@rohit Sidpara的答案转换为3.2.1版本的图表
这两个函数都需要写入BarHighlighter.swift文件
字符串
aurhwmvo4#
1.点击此链接:https://github.com/danielgindi/Charts/blob/master/ChartsDemo-iOS/Objective-C/Components/BalloonMarker.swift
1.全部复制并粘贴到项目中
1.添加
1.使用
xt0899hw5#
最大高亮距离属性可用于图表视图默认值为500 dp。将其更改为10并检查您的视图