简体   繁体   中英

Create Line Chart Using danielgindi/ios-charts library

I am trying to create a line chart using the danielgindi/ios-charts library by passing on the X axis the time values and on the Y axis integer values representing currency amount.

The time value because it is in a finite interval as I am trying to limit it to 7 elements I first convert it to unix and check witch one of them is the oldest, and from the newer ones I substitute the older one so I get and array of NSNumbers.

And here si my code and the array data:

- (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;
}

And here are my helper methods:

+ (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];

}

and xVals data is:

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

and the yVals array is:

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

And the graph is showing as:

在此处输入图片说明

Now, the Y axis look as expected but the Y axis is not correct as spacing between points is equal, any idea on what I am doing wrong?

I already tried to change the next line:

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

by giving a different xIndex but the graph stopped showing altogether on the screen.

Thanks in advance for all the constructive answers :)

I assume you mean "X axis is not correct as spacing between points is equal, any idea on what I am doing wrong?"

It is by design. To workaround this, you need to insert your intermediate date values for x axis, but not insert any data entry for the specifix xIndex. This has been asked on github page already.

You have to write your own logic for [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]];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM