【问题标题】:Create Line Chart Using danielgindi/ios-charts library使用 danielgindi/ios-charts 库创建折线图
【发布时间】:2016-05-14 19:12:10
【问题描述】:

我正在尝试使用danielgindi/ios-charts library 创建折线图,方法是在 X 轴上传递时间值,在 Y 轴上传递表示货币金额的整数值。

时间值,因为它在有限的区间内,因为我试图将其限制为 7 个元素我得到了 NSNumbers 的数组。

这里是我的代码和数组数据:

- (void)addChartWithArray:(NSArray *)dataArray{

    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    int maxNumberOfValuesInGraph;
    if(dataArray.count >= 7){
        maxNumberOfValuesInGraph = (int)dataArray.count - 7;
    }else{
        maxNumberOfValuesInGraph = 0;
    }

    for (int cout = maxNumberOfValuesInGraph; cout < dataArray.count; cout++){
        TRTargetEstimation *targetEstimation = [dataArray objectAtIndex: cout];
        [xVals addObject:[targetEstimation dateCreated]];
    }

    xVals = [self calculateTimeValues: xVals];
    int secondCounter = 0;
    for (int cout = maxNumberOfValuesInGraph; cout < dataArray.count; cout++) {
        TRTargetEstimation *targetEstimation = [dataArray objectAtIndex: cout];
        [yVals addObject:[[ChartDataEntry alloc] initWithValue:[targetEstimation.estimationValue floatValue] xIndex:secondCounter]];
        secondCounter++;
    }

    LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithYVals:yVals label:@"DataSet 1"];

    set1.lineWidth = 2.f;
    set1.circleRadius = 6.0;
    [set1 setColor: [UIColor orangeColor]];
    [set1 setCircleColor:[UIColor orangeColor]];
    set1.highlightColor = [UIColor orangeColor];
    set1.drawValuesEnabled = YES;

    LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSet:set1];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];


    self.lineChartView.minOffset = 0;
    self.lineChartView.delegate = self;
    self.lineChartView.backgroundColor = [UIColor clearColor];

    self.lineChartView.descriptionText = @"";
    self.lineChartView.noDataTextDescription = @"No history available.";

    self.lineChartView.drawGridBackgroundEnabled = NO;
    self.lineChartView.dragEnabled = NO;
    [self.lineChartView setScaleEnabled:NO];
    self.lineChartView.pinchZoomEnabled = NO;
    [self.lineChartView setViewPortOffsetsWithLeft:10.0 top:0.0 right:10.0 bottom:0.0];

    self.lineChartView.legend.enabled = NO;

    self.lineChartView.leftAxis.enabled = NO;
    self.lineChartView.rightAxis.enabled = NO;
    self.lineChartView.xAxis.enabled = NO;

    self.lineChartView.data = data;

    self.lineChartView.hidden = YES;
    self.leftLineView.hidden = YES;
    self.bottomLineView.hidden = YES;

}

- (NSMutableArray *)calculateTimeValues:(NSMutableArray *)passedArray {
    NSMutableArray *returnedArray = [[NSMutableArray alloc] init];

    for (int count = 0; count < passedArray.count; count++) {

        NSDate *date = [TRUtils getDateOutOfString:[passedArray objectAtIndex:count]
                                     andDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [returnedArray addObject:[NSNumber numberWithInt:[TRUtils getUNIXTimeFromDate:date]]];

    }

    [returnedArray sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES]]];

    int minValue = [[returnedArray firstObject] intValue];
    for (int count = 0; count < passedArray.count; count++) {
        [returnedArray replaceObjectAtIndex:count
                               withObject:[NSString stringWithFormat:@"%d",[[returnedArray objectAtIndex:count] intValue] - minValue]];
    }

    return returnedArray;
}

这是我的辅助方法:

+ (NSDate *)getDateOutOfString:(NSString *)passedString andDateFormat:(NSString *)dateFormat{

    NSString *dateString = passedString;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:dateString];
    return dateFromString;

}

+ (int)getUNIXTimeFromDate:(NSDate *) datePassed{

    return (int)[datePassed timeIntervalSince1970];

}

xVals 数据为:

<__NSArrayM 0x7fca4b775dd0>(
0,
213,
298,
435,
524,
591,
664
)

yVals 数组是:

[234234,
123123,
2223355,
22222222,
2342,
121212,
121212]

图表显示为:

现在,Y 轴看起来像预期的那样,但 Y 轴不正确,因为点之间的间距相等,知道我做错了什么吗?

我已经尝试更改下一行:

[yVals addObject:[[ChartDataEntry alloc] initWithValue:[targetEstimation.estimationValue floatValue] xIndex:secondCounter]];

通过提供不同的 xIndex 但图形完全停止在屏幕上显示。

提前感谢所有建设性的答案:)

【问题讨论】:

    标签: ios objective-c charts ios-charts


    【解决方案1】:

    我假设您的意思是“X 轴不正确,因为点之间的间距相等,知道我做错了什么吗?”

    这是设计使然。要解决此问题,您需要为 x 轴插入中间日期值,但不要为指定 xIndex 插入任何数据条目。这已经在github页面上问过了。

    你必须为[self hasDataAtIndex:cout]编写你自己的逻辑

    for (int cout = maxNumberOfValuesInGraph; cout < dataArray.count; cout++){
        TRTargetEstimation *targetEstimation = [dataArray objectAtIndex: cout];
        [xVals addObject:[targetEstimation dateCreated]];
    
        if ([self hasDataAtIndex:cout]) {
            [yVals addObject:[[ChartDataEntry alloc] initWithValue:[targetEstimation.estimationValue floatValue] xIndex:cout]];
        }
    }
    

    【讨论】:

    • 所以我应该删除这一行 [yVals addObject:[[ChartDataEntry alloc] initWithValue:[targetEstimation.estimationValue floatValue] xIndex:secondCounter]]; 并“插入 x 轴的中间日期值”,您能否在我的代码中举例说明如何实现这些。提前谢谢!
    • 我会试一试,看看我能做到什么,提前谢谢你
    • 您从哪里获得 ChartDataEntry?还有方法 [self hasDataAtIndex:cout]?
    • hasDataAtIndex 逻辑应该由你实现,数据输入在你发布的代码中
    • 你说这个问题是在 GitHub 上发的,能分享一下原帖链接吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多