예제 #1
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);
        }