Revised: Graph world total COVID-19 cases, deaths, and recoveries in C#


[COVID-19]

This post revises the example Graph world total COVID-19 cases, deaths, and recoveries in C#.

Ulrich Rosin left a comment on that post. He added a seven-day average. He also displayed tick marks for the beginning of each month instead of every day. The pandemic has been going on for quite a while since I posted the original example, so now the daily tick marks are too close together to be useful.

This revision makes two changes: it displays tick marks at the start of each month and it displays the names of the months.

Drawing Changes

The new version of the DrawAxes method uses the following code snippet to draw the tick marks and the month labels.

// Draw tick marks on the 1st of each month.
using (Font font = new Font("Arial", 10))
{
    using (StringFormat sf = new StringFormat())
    {
        sf.Alignment = StringAlignment.Near;
        sf.LineAlignment = StringAlignment.Far;

        for (int day_num = 0; day_num < num_cases; day_num++)
        {
            if (CountryData.Dates[day_num].Day == 1)
            {
                gr.DrawLine(pen, day_num, -tick_y, day_num, tick_y);

                state = gr.Save();
                gr.ResetTransform();

                string month =
                    CountryData.Dates[day_num].ToString("MMMyy");
                PointF[] p = { new PointF(day_num, 0) };
                Transform.TransformPoints(p);
                gr.DrawString(month, font, Brushes.Blue,
                    p[0].X, p[0].Y, sf);

                gr.Restore(state);
            }
        }
    }
}

This code creates a Font to draw the month names and a StringFormat to align them. It sets the StringFormat properties to draw text above and to the right of the drawing position.

Next the code loops through the days of data. It checks each day’s date, stored in the CountryData class’s Dates array, to see if the day is the first of a month. If this is the first of a month, the code draws its tick mark as it did in the previous version of the example.

The code then saves the current graphics state and resets the Graphics object’s transformations. Now any drawing will be done in pixels and not in the transformed coordinate system that the program uses to draw the tick marks and the data curves.

Next the program converts the date into a string with a format similar to SEP20.

The code then creates a point with X coordinate equal to the day number and Y coordinate equal to 0. That point represents the day’s location on the X axis. The code transforms the point using the drawing transformation to see where that point lies on the graph. It then calls the Graphics object’s DrawString method to draw the month string at the transformed location. The StringFormat object makes the text appear above and to the right of the point as shown in the picture at the top of the post.

Conclusion

The rest of the example is similar to the original version. See the original post to read about additional details. Click the Download button below to see how the rest of this program works.

Data

The following picture shows the data for the US, Spain, Italy, and the whole world when I posted the original version of this example.


[COVID-19]

If you compare this picture to the one at the top of this post, you’ll see that all of the numbers have continued to increase. That makes sense. The longer the pandemic continues, more people will be infected and therefore the more people per million will be infected.

The US now leads the pack in infections per million, but Spain is currently having a large increase and may eventually catch up to the US soon if nothing changes.

The US data is downward curving, which is good, but the numbers are still increasing quickly. Spain’s curve has taken an alarming upward turn.

It’s hard to tell what the world data is doing. It may also curve upward as Fall in the northern hemisphere begins.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in drawing, graphics and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to Revised: Graph world total COVID-19 cases, deaths, and recoveries in C#

  1. Ziz says:

    No need to publish but it caught my eye that you had updated your Covid-19 app. I went back to look at the issue I have with compiling it around line 142 of Form.

    my changes are below not sure if it is still working correctly in all areas but it compiles and shows me graphs now.

    first time through the data below mydata = 1/22/20
    I then mangle it and dateValue = 22/01/2020 00:00:00

    I guess it still needs work but it takes a US date and makes it UK date. When I get more time I will look again but if it triggers the answer please let me know.
    ———————————————————————-
    string mydate = fields[1, col + first_date_col – 1].ToString();

    var formatStrings = new string[] { “M/d/yy”, “M/dd/yy”, “MM/d/yy”, “MM/dd/yy” };
    if (DateTime.TryParseExact(mydate, formatStrings, enUS, DateTimeStyles.None, out dateValue))
    {
    CountryData.Dates[col – 1] = DateTime.FromOADate(dateValue.ToOADate());
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.