Skip to content

Commit 9995160

Browse files
committed
Update examples/stock.rs from master
It was crashing, perhaps due to a breaking change in chrono.
1 parent 90ebcf3 commit 9995160

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

plotters/examples/stock.rs

+42-40
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
use chrono::{DateTime, Duration, NaiveDate};
1+
use chrono::offset::{Local, TimeZone};
2+
use chrono::{Date, Duration};
23
use plotters::prelude::*;
3-
fn parse_time(t: &str) -> NaiveDate {
4-
DateTime::parse_from_str(&format!("{} 0:0", t), "%Y-%m-%d %H:%M")
4+
fn parse_time(t: &str) -> Date<Local> {
5+
Local
6+
.datetime_from_str(&format!("{} 0:0", t), "%Y-%m-%d %H:%M")
57
.unwrap()
6-
.date_naive()
8+
.date()
79
}
8-
const OUT_FILE_NAME: &'static str = "plotters-doc-data/stock.png";
10+
const OUT_FILE_NAME: &str = "plotters-doc-data/stock.png";
911
fn main() -> Result<(), Box<dyn std::error::Error>> {
1012
let data = get_data();
1113
let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area();
1214
root.fill(&WHITE)?;
1315

1416
let (to_date, from_date) = (
15-
parse_time(&data[0].0) + Duration::days(1),
16-
parse_time(&data[29].0) - Duration::days(1),
17+
parse_time(data[0].0) + Duration::days(1),
18+
parse_time(data[29].0) - Duration::days(1),
1719
);
1820

1921
let mut chart = ChartBuilder::on(&root)
@@ -22,7 +24,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2224
.caption("MSFT Stock Price", ("sans-serif", 50.0).into_font())
2325
.build_cartesian_2d(from_date..to_date, 110f32..135f32)?;
2426

25-
chart.configure_mesh().light_line_style(&WHITE).draw()?;
27+
chart.configure_mesh().light_line_style(WHITE).draw()?;
2628

2729
chart.draw_series(
2830
data.iter().map(|x| {
@@ -38,38 +40,38 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3840
}
3941

4042
fn get_data() -> Vec<(&'static str, f32, f32, f32, f32)> {
41-
return vec![
42-
("2019-04-25", 130.0600, 131.3700, 128.8300, 129.1500),
43-
("2019-04-24", 125.7900, 125.8500, 124.5200, 125.0100),
44-
("2019-04-23", 124.1000, 125.5800, 123.8300, 125.4400),
45-
("2019-04-22", 122.6200, 124.0000, 122.5700, 123.7600),
46-
("2019-04-18", 122.1900, 123.5200, 121.3018, 123.3700),
47-
("2019-04-17", 121.2400, 121.8500, 120.5400, 121.7700),
48-
("2019-04-16", 121.6400, 121.6500, 120.1000, 120.7700),
49-
("2019-04-15", 120.9400, 121.5800, 120.5700, 121.0500),
50-
("2019-04-12", 120.6400, 120.9800, 120.3700, 120.9500),
51-
("2019-04-11", 120.5400, 120.8500, 119.9200, 120.3300),
52-
("2019-04-10", 119.7600, 120.3500, 119.5400, 120.1900),
53-
("2019-04-09", 118.6300, 119.5400, 118.5800, 119.2800),
54-
("2019-04-08", 119.8100, 120.0200, 118.6400, 119.9300),
55-
("2019-04-05", 119.3900, 120.2300, 119.3700, 119.8900),
56-
("2019-04-04", 120.1000, 120.2300, 118.3800, 119.3600),
57-
("2019-04-03", 119.8600, 120.4300, 119.1500, 119.9700),
58-
("2019-04-02", 119.0600, 119.4800, 118.5200, 119.1900),
59-
("2019-04-01", 118.9500, 119.1085, 118.1000, 119.0200),
60-
("2019-03-29", 118.0700, 118.3200, 116.9600, 117.9400),
61-
("2019-03-28", 117.4400, 117.5800, 116.1300, 116.9300),
62-
("2019-03-27", 117.8750, 118.2100, 115.5215, 116.7700),
63-
("2019-03-26", 118.6200, 118.7050, 116.8500, 117.9100),
64-
("2019-03-25", 116.5600, 118.0100, 116.3224, 117.6600),
65-
("2019-03-22", 119.5000, 119.5900, 117.0400, 117.0500),
66-
("2019-03-21", 117.1350, 120.8200, 117.0900, 120.2200),
67-
("2019-03-20", 117.3900, 118.7500, 116.7100, 117.5200),
68-
("2019-03-19", 118.0900, 118.4400, 116.9900, 117.6500),
69-
("2019-03-18", 116.1700, 117.6100, 116.0500, 117.5700),
70-
("2019-03-15", 115.3400, 117.2500, 114.5900, 115.9100),
71-
("2019-03-14", 114.5400, 115.2000, 114.3300, 114.5900),
72-
];
43+
vec![
44+
("2019-04-25", 130.06, 131.37, 128.83, 129.15),
45+
("2019-04-24", 125.79, 125.85, 124.52, 125.01),
46+
("2019-04-23", 124.1, 125.58, 123.83, 125.44),
47+
("2019-04-22", 122.62, 124.0000, 122.57, 123.76),
48+
("2019-04-18", 122.19, 123.52, 121.3018, 123.37),
49+
("2019-04-17", 121.24, 121.85, 120.54, 121.77),
50+
("2019-04-16", 121.64, 121.65, 120.1, 120.77),
51+
("2019-04-15", 120.94, 121.58, 120.57, 121.05),
52+
("2019-04-12", 120.64, 120.98, 120.37, 120.95),
53+
("2019-04-11", 120.54, 120.85, 119.92, 120.33),
54+
("2019-04-10", 119.76, 120.35, 119.54, 120.19),
55+
("2019-04-09", 118.63, 119.54, 118.58, 119.28),
56+
("2019-04-08", 119.81, 120.02, 118.64, 119.93),
57+
("2019-04-05", 119.39, 120.23, 119.37, 119.89),
58+
("2019-04-04", 120.1, 120.23, 118.38, 119.36),
59+
("2019-04-03", 119.86, 120.43, 119.15, 119.97),
60+
("2019-04-02", 119.06, 119.48, 118.52, 119.19),
61+
("2019-04-01", 118.95, 119.1085, 118.1, 119.02),
62+
("2019-03-29", 118.07, 118.32, 116.96, 117.94),
63+
("2019-03-28", 117.44, 117.58, 116.13, 116.93),
64+
("2019-03-27", 117.875, 118.21, 115.5215, 116.77),
65+
("2019-03-26", 118.62, 118.705, 116.85, 117.91),
66+
("2019-03-25", 116.56, 118.01, 116.3224, 117.66),
67+
("2019-03-22", 119.5, 119.59, 117.04, 117.05),
68+
("2019-03-21", 117.135, 120.82, 117.09, 120.22),
69+
("2019-03-20", 117.39, 118.75, 116.71, 117.52),
70+
("2019-03-19", 118.09, 118.44, 116.99, 117.65),
71+
("2019-03-18", 116.17, 117.61, 116.05, 117.57),
72+
("2019-03-15", 115.34, 117.25, 114.59, 115.91),
73+
("2019-03-14", 114.54, 115.2, 114.33, 114.59),
74+
]
7375
}
7476
#[test]
7577
fn entry_point() {

0 commit comments

Comments
 (0)