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:
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;
}