dynamic property class for mutilple series

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: dynamic property class for mutilple series

Post by Christopher » Wed Feb 15, 2023 2:56 pm

coldwind wrote:
Wed Feb 15, 2023 12:59 pm
So can i use it to convert the int or double,or long to Decimal on YValue?
Thank you Your Help.
Okay. The JSON Data Types are these, which are the essentially the same as system.text.json.jsonvaluekind. For TeeChart, we need a number and a string, so we can use this enum to extract a number and a string. As you can see, there are only two types of numbers in JSON:
Screenshot from 2023-02-15 15-52-04.png
Screenshot from 2023-02-15 15-52-04.png (14.69 KiB) Viewed 2480 times
This means that numbers can only be Int32 or Double. Internally, TeeChart holds data in the Double type, and as JSON number values can all be converted to double, this means that the following code will cover all number types (you can see it working for both Int32 and Double values):

Code: Select all

        class Request
        {
            public string? Name { get; set; }
            public double Value { get; set; }

            public static Request Empty { get; set; } = new Request { Name = null, Value = -1 };
        }

        private void InitializeChart()
        {

            List<Request> GetData(string request)
            {
                var response = "[{\"name\":\"aa\",\"age\":10,\"height\":170.5},{\"name\":\"bb\",\"age\":11.8,\"height\":175},{\"name\":\"cc\",\"age\":12,\"height\":180.8}]";
                var data = JsonSerializer.Deserialize<JsonElement>(response).EnumerateArray();
                var req = JsonSerializer.Deserialize<JsonElement>(request).EnumerateArray();

                var data_nv = data.SelectMany(x => x.EnumerateObject()).Where(x => x.Name == req.First().GetString() || x.Name == req.Last().GetString()).Select(x => x.Value);

                return Enumerable.Zip(data_nv, data_nv.Skip(1), (x, y) =>
                {
                    if (x.ValueKind == JsonValueKind.String && y.ValueKind == JsonValueKind.Number)
                    {
                        return new Request { Name = x.GetString(), Value = y.GetDouble() };
                    }
                    else
                    {
                        return Request.Empty;
                    }
                }).Where(x => x != Request.Empty).ToList();
            }

            var age = new Line(tChart1.Chart);
            age.YValues.DataMember = "Value";
            age.LabelMember = "Name";
            age.Title = "Age";
            age.LinePen.Width = 3;
            age.Add(GetData("[\"name\",\"age\"]") as IList);

            var height = new Line(tChart1.Chart);
            height.YValues.DataMember = "Value";
            height.LabelMember = "Name";
            height.Title = "Height";
            height.Add(GetData("[\"name\",\"height\"]") as IList);

            height.VertAxis = VerticalAxis.Right;

        }
Screenshot from 2023-02-15 15-55-33.png
Screenshot from 2023-02-15 15-55-33.png (35.19 KiB) Viewed 2480 times
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply