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


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

                string month =
                PointF[] p = { new PointF(day_num, 0) };
                gr.DrawString(month, font, Brushes.Blue,
                    p[0].X, p[0].Y, sf);


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.


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.


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


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.

5 Responses 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());

  2. Stefan says:

    Hi Rod,

    tried your program… running into this failure (line 328, at Excel.workbook):

    Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))

    Internet oracle gives hint for some wrong registry key… tried but didn’t work.
    Seems I use the “wrong” Excel version (2010) – any advice from your experience?

    • RodStephens says:

      Hmm… You probably need to install Office interrop. I try not to install extras, but I’ve had it installed for so long that I forget about it.

      Currently you can get it here.

      Posta follow-up comment and let me know if that works.

      • Stefan says:

        Hint doesn’t work.

        Internet oracle tells me I have to check interrop keys… by deleting “wrong” key from registry (Start–> Run –> regedit –> HKEY_CLASSES_ROOT–>TypeLib–>{00020813-0000-0000-C000-000000000046} –> …) I steped over to next failure 🙁

        Now debuger terminated at line #135 (CountryData.Dates = …) with an System.OverflowException (arithmetic operator overflow).

        I’m not sure if this is now due to the changes above or “just” simple programming failure.

        • RodStephens says:

          So it’s running but crashing now? That’s probably an improvement.

          I don’t know why that particular line would give an overflow exception. You might use the debugger to look at num_dates when the code crashes. If it’s somehow reading the data wrong, that value could be getting messed up and causing a problem.

          At this point, if you have the program running, it could be that the data has changed and the program doesn’t understand the new format. For example, the latest downloaded data files seem to have a new row at the end that has no country name and is all zeros. The program crashes when it tries to get the country name. I’ll update the example to ignore rows with no country names.

          Unfortunately I don’t know if there’s an easy way to know what’s going on. You may have to just use the debugger to track down where it’s getting confused.

          Be sure to delete any old CSV files just in case.

Comments are closed.