private async void DrawKey(CovidWorldwideStats stats)
        {
            const int ICON_STARTING_X  = 3;
            const int ICON_PADDING_Y   = 3;
            const int TEXT_PADDING_Y   = 3;
            const int TEXT_PADDING_X   = 40;
            const int ICON_SIZE_PIXELS = 35;

            if (stats == null)
            {
                return;
            }

            if (!long.TryParse(stats.Recovered, out long recovered) ||
                !long.TryParse(stats.Deaths, out long deaths) ||
                !long.TryParse(stats.AllCases, out long allCases))
            {
                Logger.Instance.LogMessage(TracingLevel.WARN, $"CoronavirusWorldwideStatsAction > Could not convert deaths/all cases to integer Deaths: {stats.Deaths} All Case: {stats.AllCases}");
                return;
            }

            // Get the recovery rate as a percentage
            double recoveryRate = (double)recovered / (double)allCases * 100;

            using (Bitmap img = Tools.GenerateGenericKeyImage(out Graphics graphics))
            {
                int    height         = img.Height;
                int    width          = img.Width;
                float  heightPosition = 10;
                string text;

                var font = new Font("Verdana", 22, FontStyle.Bold, GraphicsUnit.Pixel);
                var fontRecoveryTitle = new Font("Verdana", 20, FontStyle.Bold, GraphicsUnit.Pixel);
                var fontRecovery      = new Font("Verdana", 30, FontStyle.Bold, GraphicsUnit.Pixel);

                Bitmap icon;
                using (icon = IconChar.Ambulance.ToBitmap(ICON_SIZE_PIXELS, Color.Orange))
                {
                    graphics.DrawImage(icon, new Point(ICON_STARTING_X, (int)heightPosition));
                    heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, GraphicUtils.FormatNumber(allCases), font, Brushes.Orange, new PointF(TEXT_PADDING_X, heightPosition + +TEXT_PADDING_Y));
                }
                heightPosition += ICON_PADDING_Y;
                using (icon = IconChar.SkullCrossbones.ToBitmap(ICON_SIZE_PIXELS, Color.Red))
                {
                    graphics.DrawImage(icon, new Point(ICON_STARTING_X, (int)heightPosition));
                    heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, GraphicUtils.FormatNumber(deaths), font, Brushes.Red, new PointF(TEXT_PADDING_X, heightPosition + TEXT_PADDING_Y));
                }
                heightPosition += ICON_PADDING_Y;

                heightPosition = GraphicUtils.DrawStringOnGraphics(graphics, "Recovered:", fontRecoveryTitle, Brushes.Green, new PointF(ICON_STARTING_X, heightPosition));
                // Put percentage exactly in middle
                text = $"{(int)recoveryRate}%";
                float stringWidth = GraphicUtils.CenterText(text, width, fontRecovery, graphics, ICON_STARTING_X);
                GraphicUtils.DrawStringOnGraphics(graphics, text, fontRecovery, Brushes.Green, new PointF(stringWidth, heightPosition));

                await Connection.SetImageAsync(img);

                graphics.Dispose();
            }
        }
Beispiel #2
0
        private async Task LoadCovidData()
        {
            // Check if we should refresh the data
            if ((DateTime.Now - lastRefreshTime).TotalSeconds >= REFRESH_RATE_SECONDS)
            {
                Logger.Instance.LogMessage(TracingLevel.INFO, "Refreshing Covid data");
                lastRefreshTime = DateTime.Now;

                // Get worldwide data
                string url      = $"{COVID_API_SITE}{API_WORLDWIDE}";
                string response = await QueryAPI(url);

                // Parse response
                if (!String.IsNullOrEmpty(response))
                {
                    worldwideStats = JsonConvert.DeserializeObject <CovidWorldwideStats>(response);
                }

                // Get country data
                url      = $"{COVID_API_SITE}{API_COUNTRIES}";
                response = await QueryAPI(url);

                // Parse response
                if (!String.IsNullOrEmpty(response) && TryParse(response, out JArray jArr))
                {
                    countriesStats = jArr.ToObject <List <CovidCountryStats> >();
                }
            }
        }