1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// local imports

use charts::SourceSeries;

/// Utility functions.

/// Get lowest value among `length` data points in given `source`.
pub fn lowest(source: SourceSeries, length: usize) -> Option<f64> {
    if length <= 0 {
        return None;
    }

    let lowest = source.get(0);

    if lowest.is_none() {
        return None;
    }

    let mut lowest = source.get(0).unwrap();

    for index in 1..(length) {
        match source.get(index) {
            None => {}
            Some(maybe_lowest) => if maybe_lowest < lowest {
                lowest = maybe_lowest;
            },
        }
    }

    Some(lowest)
}

/// Get highest value among `length` data points in given `source`.
pub fn highest(source: SourceSeries, length: usize) -> Option<f64> {
    if length <= 0 {
        return None;
    }

    let highest = source.get(0);

    if highest.is_none() {
        return None;
    }

    let mut highest = highest.unwrap();

    for index in 1..(length) {
        match source.get(index) {
            None => {}
            Some(maybe_highest) => if maybe_highest > highest {
                highest = maybe_highest;
            },
        }
    }

    Some(highest)
}