Ejemplo n.º 1
0
            private string ParseData(CovidParser.Country.Data data, CovidParser.Country.Data previousData, CovidParser.Country.Data worldData, string country)
            {
                string countryName = (char)(country[0] - 32) + country.Remove(0, 1);

                string confirmed = ToStringWithDots(data.confirmed);
                string deaths    = ToStringWithDots(data.deaths);
                string recovered = ToStringWithDots(data.recovered);

                string confirmedPW = MathF.Round(data.confirmed * 100f / worldData.confirmed, 2).ToString();
                string deathsPW    = MathF.Round(data.deaths * 100f / worldData.deaths, 2).ToString();
                string recoveredPW = MathF.Round(data.recovered * 100f / worldData.recovered, 2).ToString();

                string confirmedA = ToStringWithDots(data.confirmed - previousData.confirmed);
                string deathsA    = ToStringWithDots(data.deaths - previousData.deaths);
                string recoveredA = ToStringWithDots(data.recovered - previousData.recovered);

                string deathsP    = MathF.Round(data.deaths * 100f / data.confirmed, 2).ToString();
                string recoveredP = MathF.Round(data.recovered * 100f / data.confirmed, 2).ToString();

                return("\n" + countryName + " on " + data.date +
                       ":\n\t         \t\t\t\t[Cases]\t\t\t[World %]\t\t\t[Increase]\t\t\t[Cases %]" +
                       "\n\tConfirmed:\t" + confirmed + "\t\t\t\t" + GenerateSpaces(confirmed, new string[] { deaths, recovered }) + confirmedPW + "%" + GenerateSpaces(confirmedPW, new string[] { deathsPW, recoveredPW }) + "\t\t\t\t+" + confirmedA +
                       "\n\tDeaths:  \t\t" + deaths + "\t\t\t\t" + GenerateSpaces(deaths, new string[] { confirmed, recovered }) + deathsPW + "%" + GenerateSpaces(deathsPW, new string[] { confirmedPW, recoveredPW }) + "\t\t\t\t+" + deathsA + GenerateSpaces(deathsA, new string[] { confirmedA, recoveredA }) + "\t\t\t\t" + deathsP + "%" +
                       "\n\tRecovered:\t" + recovered + "\t\t\t\t" + GenerateSpaces(recovered, new string[] { deaths, confirmed }) + recoveredPW + "%" + GenerateSpaces(recoveredPW, new string[] { confirmedPW, deathsPW }) + "\t\t\t\t+" + recoveredA + GenerateSpaces(recoveredA, new string[] { confirmedA, deathsA }) + "\t\t\t\t" + recoveredP + "%");


                /*return "\n" + countryName + " on " + data.date +
                 *  ":\n\t         \t\t\t\t[Cases] [World %] [Increase] [cases %]" +
                 *  "\n\tConfirmed:\t" + confirmed + "\t\t\t\t" + confirmedPW + "%\t\t\t\t+" + confirmedA +
                 *  "\n\tDeaths:  \t\t" + deaths + "\t\t\t\t" + deathsPW + "%\t\t\t\t+" + deathsA + "\t\t\t\t" + deathsP + "%" +
                 *  "\n\tRecovered:\t" + recovered + "\t\t\t\t" + recoveredPW + "%\t\t\t\t+" + recoveredA + "\t\t\t\t" + recoveredP + "%";*/
            }
Ejemplo n.º 2
0
        public static void CreateCovidChart(CovidParser.Country[] countries, CovidChartTypes type, int startDay = 0, int endDay = 2147483647)
        {
            if (startDay < 0)
            {
                Debug.LogWarning("Start day is lower than 0, fixing");
                startDay = 0;
            }

            int xMin = startDay;
            int xMax = countries[0].data.Count;
            int yMin = 0;

            CovidParser.Country.Data c = FindMax(countries, type, startDay, endDay);
            long yMax = 0;

            string title = "COVID-19 in ";

            for (int i = 0; i < countries.Length; i++)
            {
                title += countries[i].name + " ";
            }
            switch (type)
            {
            case CovidChartTypes.deaths:
                yMax   = c.deaths;
                title += " deaths";
                break;

            case CovidChartTypes.confirmed:
                yMax   = c.confirmed;
                title += " confirmed";
                break;

            case CovidChartTypes.recovered:
                yMax   = c.recovered;
                title += " recovered";
                break;

            case CovidChartTypes.all:
                yMax = c.confirmed;
                break;

            default:
                break;
            }



            List <double>[] values = null; //new List<double>();
            List <double>   keys   = new List <double>();

            for (int j = 0; j < countries.Length; j++)
            {
                for (int i = startDay; i < countries[j].data.Count && i < endDay; i++)
                {
                    if (j == 0)
                    {
                        keys.Add(i);
                    }
                    switch (type)
                    {
                    case CovidChartTypes.deaths:
                        if (values == null || values[j] == null)
                        {
                            values    = new List <double> [countries.Length];
                            values[j] = new List <double>();
                        }
                        values[j].Add(countries[j].data[i].deaths);

                        break;

                    case CovidChartTypes.confirmed:
                        if (values == null || values[j] == null)
                        {
                            values    = new List <double> [countries.Length];
                            values[j] = new List <double>();
                        }
                        values[j].Add(countries[j].data[i].confirmed);

                        break;

                    case CovidChartTypes.recovered:
                        if (values == null || values[j] == null)
                        {
                            values    = new List <double> [countries.Length];
                            values[j] = new List <double>();
                        }
                        values[j].Add(countries[j].data[i].recovered);

                        break;

                    case CovidChartTypes.all:
                        //Debug.LogError("All is not supported yet");
                        if (values == null)
                        {
                            values = new List <double> [countries.Length * 3];
                        }
                        if (values[j * 3] == null)
                        {
                            values[j * 3]     = new List <double>();
                            values[j * 3 + 1] = new List <double>();
                            values[j * 3 + 2] = new List <double>();
                        }
                        values[j * 3].Add(countries[j].data[i].confirmed);
                        values[j * 3 + 1].Add(countries[j].data[i].deaths);
                        values[j * 3 + 2].Add(countries[j].data[i].recovered);
                        break;

                    default:
                        break;
                    }
                }
            }
            var pl = new PLStream();

            pl.sdev("pngcairo");
            pl.sfnam("covid.png");

            pl.init();
            pl.col0(15);

            // Set to use 10000 instead of 1 * 10^5
            pl.syax(10, 10);

            if (endDay < xMax)
            {
                xMax = endDay - 1;
            }


            pl.env(xMin, xMax, yMin, yMax, AxesScale.Independent, AxisBox.BoxTicksLabels);

            //pl.setcontlabelformat(10, 10);
            pl.col0(15);
            pl.lab("Days", "Cases", title);



            Pattern[]     lg_patterns = new Pattern[values.Length];
            double[]      lg_scales   = new double[values.Length];
            string[]      lg_texts    = new string[values.Length];
            int[]         lg_lcolors  = new int[values.Length];
            double[]      lg_lwidths  = new double[values.Length];
            int[]         lg_scolors  = new int[values.Length];
            int[]         lg_snumbers = new int[values.Length];
            string[]      lg_symbols  = new string[values.Length];
            LineStyle[]   lg_lstyles  = new LineStyle[values.Length];
            LegendEntry[] lg_entries  = new LegendEntry[values.Length];

            double lg_spacing = 2.8 / (countries.Length + 0.5);
            double lg_tscale  = 1.4 / (countries.Length + 0.5);
            double lg_toffset = 0.5;



            switch (type)
            {
            case CovidChartTypes.deaths:
                pl.scmap0(new int[] { 255, 255 }, new int[] { 255, 0 }, new int[] { 0, 0 });
                break;

            case CovidChartTypes.confirmed:
                break;

            case CovidChartTypes.recovered:
                break;

            case CovidChartTypes.all:
                pl.scmap0(new int[] { 255, 255, 0, 204, 128, 0 }, new int[] { 255, 0, 255, 153, 0, 128 }, new int[] { 0, 0, 0, 0, 0, 0 });
                break;

            default:
                break;
            }

            for (int i = 0; i < values.Length; i++)
            {
                pl.col0(i);

                pl.line(keys.ToArray(), values[i].ToArray());


                lg_entries[i]  = LegendEntry.Line;
                lg_lstyles[i]  = LineStyle.Continuous;
                lg_lcolors[i]  = i;
                lg_scolors[i]  = i;
                lg_snumbers[i] = i;
                lg_lwidths[i]  = 2;
                lg_texts[i]    = countries[i / 3].name;
                if (i % 3 == 0)
                {
                    lg_texts[i] += " confirmed";
                }
                else if (i % 3 == 1)
                {
                    lg_texts[i] += " deaths";
                }
                else
                {
                    lg_texts[i] += " recovered";
                }

                lg_scales[i] = 1;
            }



            pl.legend(out double width, out double height, Legend.BoundingBox, Position.Left | Position.Top, 0, 0, 0.1, 0, 1, LineStyle.Continuous, 0, 0, lg_entries, lg_toffset, lg_tscale, lg_spacing, 1.0,
                      lg_lcolors, lg_texts, lg_lcolors, lg_patterns, lg_scales, lg_lwidths, lg_lcolors, lg_lstyles, lg_lwidths, lg_scolors, lg_scales, lg_snumbers, lg_symbols);

            pl.eop();

            pl.ResetOpts();

            IDisposable disp = (IDisposable)pl;

            disp.Dispose();

            //pl.gver(out var varText);
            //Debug.Log("Plplot version: " + varText);
        }
Ejemplo n.º 3
0
            public void Execute(List <CovidParser.Country> data, CovidParser.Country world)
            {
                CovidParser.Country      _country        = null;
                CovidParser.Country.Data countryData     = new CovidParser.Country.Data();
                CovidParser.Country.Data prevCountryData = new CovidParser.Country.Data();

                CovidParser.Country[] cTc = new CovidParser.Country[countriesToCompare.Length];

                if (country == "world")
                {
                    _country    = world;
                    countryData = GetWorldOnDate(world, date);
                    if (date == DateTime.MinValue)
                    {
                        if (world.data.Count > 1)
                        {
                            prevCountryData = world.data[world.data.Count - 2];
                        }
                    }
                    else
                    {
                        DateTime prevDate = date.AddDays(-1);
                        prevCountryData = GetWorldOnDate(world, prevDate);
                    }
                }
                else
                {
                    for (int i = 0; i < data.Count; i++)
                    {
                        if (data[i].name == country)
                        {
                            if (date == DateTime.MinValue)
                            {
                                _country    = data[i];
                                countryData = data[i].data[data[i].data.Count - 1];

                                if (data[i].data.Count > 1)
                                {
                                    prevCountryData = data[i].data[data[i].data.Count - 2];
                                }
                            }
                            else
                            {
                                string toFind = date.Year.ToString() + "-" + date.Month.ToString() + "-" + date.Day.ToString();
                                foreach (CovidParser.Country.Data country in data[i].data)
                                {
                                    if (country.date == toFind)
                                    {
                                        _country    = data[i];
                                        countryData = country;
                                        break;
                                    }

                                    prevCountryData = country;
                                }
                            }

                            //break;
                        }
                        for (int j = 0; j < countriesToCompare.Length; j++)
                        {
                            if (data[i].name == countriesToCompare[j])
                            {
                                if (date == DateTime.MinValue)
                                {
                                    cTc[j] = data[i];
                                }
                                else
                                {
                                    string toFind = DateToStr(date);
                                    foreach (CovidParser.Country.Data country in data[i].data)
                                    {
                                        if (country.date == toFind)
                                        {
                                            cTc[j] = data[i];

                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (_country == null)
                {
                    RespondToCommand("Country ``" + country + "`` not found", channel);
                    return;
                }

                if (param.info)
                {
                    RespondToCommand(ParseData(countryData, prevCountryData, GetWorldOnDate(world, date), country), channel);
                }
                if (param.chart)
                {
                    CovidParser.Country[] countries = new CovidParser.Country[countriesToCompare.Length + 1];
                    countries[0] = _country;

                    for (int i = 0; i < cTc.Length; i++)
                    {
                        if (cTc[i] == null)
                        {
                            RespondToCommand("Cannot find country: " + countriesToCompare[i], channel);
                            Debug.LogError("Cannot find country: " + countriesToCompare[i]);
                            return;
                        }

                        countries[i + 1] = cTc[i];
                    }
                    int start = 0;
                    int end   = 2147483647;

                    if (startDate != DateTime.MinValue)
                    {
                        start = (startDate - StrToDate(world.data[0].date)).Days;
                        Debug.Log("Start: " + start);
                    }
                    if (date != DateTime.MinValue) // end date
                    {
                        end = (date - StrToDate(world.data[0].date)).Days;
                        Debug.Log("End: " + end);
                    }

                    CreateCovidChart(countries, CovidChartTypes.all, start, end);

                    channel.Channel.SendFileAsync("covid.png");
                }
            }