You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用LINQPad将含日期与累计收益的JSON数据绘制成折线图

用LINQPad绘制累计收益折线图的解决方案

核心修改步骤

  • 直接用反序列化得到的jsonData集合调用Chart方法,指定X轴为时间字段entryTime,Y轴为累计收益字段cumulativegains,明确指定折线图类型。
  • 先对数据按entryTime排序,保证折线按时间顺序绘制,避免数据无序导致折线错乱。
  • 可选:通过WithTitle、WithXAxisTitle、WithYAxisTitle添加图表和轴标签,提升可读性。

修改后的完整代码

void Main()
{
    string json = "[\r\n  {\r\n    \"entryTime\": \"2019-01-08T00:00:01\",\r\n    \"gains\": -0.00071,\r\n    \"cumulativegains\": -0.00071\r\n  },\r\n  {\r\n    \"entryTime\": \"2019-01-08T05:00:01\",\r\n    \"gains\": -0.00102,\r\n    \"cumulativegains\": -0.00102\r\n  },\r\n  {\r\n    \"entryTime\": \"2019-01-08T09:00:01\",\r\n    \"gains\": -0.00113,\r\n    \"cumulativegains\": -0.00113\r\n  },\r\n  {\r\n    \"entryTime\": \"2019-01-08T10:31:19\",\r\n    \"gains\": -0.00236,\r\n    \"cumulativegains\": -0.00236\r\n  },\r\n  {\r\n    \"entryTime\": \"2019-01-09T20:21:34\",\r\n    \"gains\": -0.00262,\r\n    \"cumulativegains\": -0.00262\r\n  },\r\n  {\r\n    \"entryTime\": \"2019-01-11T16:00:01\",\r\n    \"gains\": -0.00075,\r\n    \"cumulativegains\": -0.00075\r\n  },\r\n ]";
    
    // 反序列化JSON数据
    List<trades2plot> jsonData = JsonConvert.DeserializeObject<List<trades2plot>>(json);

    // 按时间排序后绘制折线图,添加标题和轴标签
    jsonData
        .OrderBy(t => t.entryTime)
        .Chart(t => t.entryTime, t => t.cumulativegains, Util.SeriesType.Line)
        .WithTitle("累计收益趋势图")
        .WithXAxisTitle("时间")
        .WithYAxisTitle("累计收益")
        .Dump();
}

public class trades2plot
{
    public DateTime entryTime;
    public double gains;
    public double cumulativegains;
    public trades2plot() { }
}

说明

  • OrderBy(t => t.entryTime):确保数据按时间顺序排列,折线图的点会按时间先后连接,符合趋势展示逻辑。
  • Chart方法的三个参数依次为:X轴字段选择器、Y轴字段选择器、图表类型。
  • 链式调用WithTitle等方法是LINQPad图表扩展的特性,用于增强图表的可读性。

内容的提问来源于stack exchange,提问作者junkone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 14:25:23