gpt4 book ai didi

ios - 在 iOS 的 Charts 库中实现 ChartValueSelected 方法

转载 作者:行者123 更新时间:2023-11-29 00:38:47 31 4
gpt4 key购买 nike

我有一个应用程序,可以使用 iOS 图表库 v.2.3 显示带有数据的图表。现在我必须启用选项来跟踪图表上的数据接触点。我做了一些研究,我知道我必须在类中使用 chartValueSelected 方法,它实现了 .我还将 chartView.delegate 声明为 self 并在 Storyboard上的 View 中连接 socket 。但是当我测试它时,我总是从 chartValueNothingSelected 方法中得到一个信号,而没有从 chartValueSelected 中得到信号。这是我应该放入我的代码中的东西,它可以跟踪我应用程序中点的触摸吗?

编辑:代码

@interface ChartViewController () <ChartViewDelegate>

@end

@implementation ChartViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.chartView.delegate = self;
_chartView.dragEnabled = YES;
[_chartView setScaleEnabled:YES];
_chartView.pinchZoomEnabled = YES;
_chartView.drawGridBackgroundEnabled = NO;

// some random data generator
// x-axis limit line
ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
llXAxis.lineWidth = 4.0;
llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)];
llXAxis.labelPosition = ChartLimitLabelPositionRightBottom;
llXAxis.valueFont = [UIFont systemFontOfSize:10.f];

//[_chartView.xAxis addLimitLine:llXAxis];

_chartView.xAxis.gridLineDashLengths = @[@10.0, @10.0];
_chartView.xAxis.gridLineDashPhase = 0.f;

ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:150.0 label:@"Upper Limit"];
ll1.lineWidth = 4.0;
ll1.lineDashLengths = @[@5.f, @5.f];
ll1.labelPosition = ChartLimitLabelPositionRightTop;
ll1.valueFont = [UIFont systemFontOfSize:10.0];

ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"];
ll2.lineWidth = 4.0;
ll2.lineDashLengths = @[@5.f, @5.f];
ll2.labelPosition = ChartLimitLabelPositionRightBottom;
ll2.valueFont = [UIFont systemFontOfSize:10.0];

ChartYAxis *leftAxis = _chartView.leftAxis;
[leftAxis removeAllLimitLines];
[leftAxis addLimitLine:ll1];
[leftAxis addLimitLine:ll2];
leftAxis.gridLineDashLengths = @[@5.f, @5.f];
leftAxis.drawZeroLineEnabled = NO;
leftAxis.drawLimitLinesBehindDataEnabled = YES;

_chartView.rightAxis.enabled = NO;

//[_chartView.viewPortHandler setMaximumScaleY: 2.f];
//[_chartView.viewPortHandler setMaximumScaleX: 2.f];


//_chartView.marker = marker;

_chartView.legend.form = ChartLegendFormLine;

[_chartView animateWithXAxisDuration:2.5];

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

for (int i = 0; i < 100; i++)
{
[xVals addObject:[@(i) stringValue]];
}

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

for (int i = 0; i < 100; i++)
{
double mult = (1000 + 1);
double val = (double) (arc4random_uniform(mult)) + 3;
[yVals addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
}

LineChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
set1.yVals = yVals;
_chartView.data.xValsObjc = xVals;
[_chartView.data notifyDataChanged];
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[LineChartDataSet alloc] initWithYVals:yVals label:@"DataSet 1"];

set1.lineDashLengths = @[@5.f, @2.5f];
set1.highlightLineDashLengths = @[@5.f, @2.5f];
[set1 setColor:UIColor.blackColor];
[set1 setCircleColor:UIColor.blackColor];
set1.lineWidth = 1.0;
set1.circleRadius = 3.0;
set1.drawCircleHoleEnabled = NO;
set1.valueFont = [UIFont systemFontOfSize:9.f];
//set1.fillAlpha = 65/255.0;
//set1.fillColor = UIColor.blackColor;

NSArray *gradientColors = @[
(id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
(id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
];
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);

set1.fillAlpha = 1.f;
set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
set1.drawFilledEnabled = YES;

CGGradientRelease(gradient);

NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];

LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSets:dataSets];

_chartView.data = data;
}
}

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
{
NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
NSLog(@"chartValueNothingSelected");
}

@end

and from .h file

#import <UIKit/UIKit.h>
@import Charts;

@interface ChartViewController : UIViewController
@property(nonatomic, strong) IBOutlet LineChartView* chartView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *chartViewHeightConstraint;

@end

最佳答案

对于每个人,谁会遇到与我类似的问题。我解决了我的问题,使用 CocoaPods 工具清理了旧版本 2.3 图表中可能留下的所有内容,然后添加对新 3.0 版本的引用。对我来说,它启用了显示实际点击点的选项。

关于ios - 在 iOS 的 Charts 库中实现 ChartValueSelected 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39981213/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com