Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. #r "System.Windows.Forms"
  2.  
  3. using System.Windows.Forms;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Drawing.Drawing2D;
  7. using System.Net;
  8. using System.Web;
  9.  
  10. HttpWebRequest hwr = HttpWebRequest.CreateHttp("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv");
  11. class Stat
  12. {
  13.     public DateTime Date { get; set; }
  14.     public String State { get; set; }
  15.     public int Fips { get; set; }
  16.     public int Cases { get; set; }
  17.     public int Deaths { get; set; }
  18. }
  19.  
  20. List<Stat> stats = new List<Stat>();
  21.  
  22. using (var r = hwr.GetResponse())
  23. using (var srs = r.GetResponseStream())
  24. using (var sr = new StreamReader(srs))
  25. {
  26.     sr.ReadLine();
  27.     String line = "";
  28.     while ((line = sr.ReadLine()) != null)
  29.     {
  30.         var parts = line.Split(',');
  31.         stats.Add(new Stat
  32.         {
  33.             Date = DateTime.Parse(parts[0]),
  34.             State = parts[1],
  35.             Fips = int.Parse(parts[2]),
  36.             Cases = int.Parse(parts[3]),
  37.             Deaths = int.Parse(parts[4])
  38.         });
  39.     }
  40. }
  41.  
  42. int lastCases = 0;
  43. var info = stats.Where(x => x.State == "Washington")
  44.      .Select(x => {
  45.  
  46.          var ret = new
  47.          {
  48.              Date = x.Date,
  49.              Cases = x.Cases,
  50.              Delta = x.Cases - lastCases,
  51.              Deaths = x.Deaths
  52.          };
  53.          lastCases = x.Cases;
  54.  
  55.          return ret;
  56.      })
  57.     .ToList();
  58.  
  59.  
  60. Form x = (new Form()
  61. {
  62.     Width = 1000,
  63.     Height = 800,
  64. BackColor = Color.White
  65. });
  66.  
  67. x.Paint += (s, e) => {
  68.     e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  69.     int offX = 10;
  70.  
  71.     float increment = (800 - 20) / (float)info.Count;
  72.     float scaleCases = (600 - 40) / (float)info.Max(x => x.Cases);
  73.     float scaleDeltas = (600 - 40) / (float)info.Max(x => x.Delta);
  74.  
  75.     int item = 0;
  76.     e.Graphics.DrawLines(Pens.Blue, info.Select(x => new PointF(
  77.           offX + (item++) * increment,
  78.           600 - 20 - scaleCases * x.Cases
  79.         )).ToArray());
  80.     item = 0;
  81.     e.Graphics.DrawLines(Pens.Orange, info.Select(x => new PointF(
  82.           offX + (item++) * increment,
  83.           600 - 20 - scaleCases * x.Delta
  84.         )).ToArray());
  85.     item = 0;
  86.     e.Graphics.DrawLines(Pens.Red, info.Select(x => new PointF(
  87.           offX + (item++) * increment,
  88.           600 - 20 - scaleCases * x.Deaths
  89.         )).ToArray());
  90.  
  91.     for (int i = 0; i < info.Count; ++i)
  92.     {
  93.         e.Graphics.FillEllipse(Brushes.LightBlue, offX + i * increment - 2, 600 - 20 - scaleCases * info[i].Cases - 2, 4, 4);
  94.         e.Graphics.FillEllipse(Brushes.Yellow, offX + i * increment - 2, 600 - 20 - scaleCases * info[i].Delta - 2, 4, 4);
  95.         e.Graphics.FillEllipse(Brushes.Pink, offX + i * increment - 2, 600 - 20 - scaleCases * info[i].Deaths - 2, 4, 4);
  96.  
  97.         e.Graphics.DrawString(info[i].Cases.ToString() + "(" + (info[i].Deaths / (float)info[i].Cases * 100).ToString("0.00") + "%)", new Font("Segoe UI", 8), Brushes.Blue, offX + (i) * increment + 10, 600 - 20 - scaleCases * info[i].Cases);
  98.         e.Graphics.DrawString(info[i].Delta.ToString(), new Font("Segoe UI", 8), Brushes.Orange, offX + (i) * increment + 10, 600 - 20 - scaleCases * info[i].Delta);
  99.         e.Graphics.DrawString(info[i].Deaths.ToString(), new Font("Segoe UI", 8), Brushes.Red, offX + (i) * increment + 10, 600 - 20 - scaleCases * info[i].Deaths);
  100.     }
  101.  
  102.  
  103. };
  104.  
  105. x.Show();
  106. x.Invalidate();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement