【问题标题】:Convert List<double> to LiveCharts.IChartValues将 List<double> 转换为 LiveCharts.IChartValues
【发布时间】:2017-03-22 14:45:02
【问题描述】:

我想用 LiveCharts 在图表中绘制我的双精度值。 但我无法转换我的价值观。 我得到了错误:

Cannot convert source type 'System.Collections.Generic.List<double>
to target type 'LiveCharts.IChartValues'

这是我的代码(可能不需要):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;
using LiveCharts;
using LiveCharts.Wpf;
using Brushes = System.Windows.Media.Brushes;

namespace LiveAnalysis
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            var xvals = new List<DateTime>();
            var yvals = new List<double>();

            using (var db = new SystemtestFunctions.TestingContext())
            {
                var lttResults = db.LttResults;
                var par1 = 0.0;
                foreach (var data in lttResults)
                {
                    if (data.GatewayEventType == 41)
                        par1 = data.FloatValue;
                    if (data.GatewayEventType != 42) continue;

                    var par2 = data.FloatValue;
                    var diff = Math.Round(par1 - par2, 3);
                    yvals.Add(diff);
                    xvals.Add(data.DateTime);
                }
            }

            cartesianChart1.Series = new SeriesCollection
            {
                new LineSeries
                {
                    Title = "Series 1",
                    Values = yvals,
                },
            };
        }
    }
}

【问题讨论】:

  • 这个错误看起来很明显
  • 也许只需将 yvals 定义从 List&lt;double&gt; 更改为 ChartValues&lt;double&gt;
  • @Pikoh 你想添加这个作为答案还是我应该删除这个问题?
  • @kame 随便你。我没有将其添加为答案,因为我从未使用过 livecharts,但也许我可以添加它以防其他人发现它有用

标签: c# charts livecharts


【解决方案1】:

除了@Pikoh 的回答,您还可以使用AsChartValues() 扩展将任何IEnumerable 转换为ChartValues 实例:

cartesianChart1.Series = new SeriesCollection
        {
            new LineSeries
            {
                Title = "Series 1",
                Values = yvals.AsChartValues(),
            },
        };

【讨论】:

    【解决方案2】:

    LiveCharts LineSeries 在其Values 属性中期望ChartValues 类型的变量。所以在这种情况下你应该改变:

    var yvals = new List<double>();
    

    进入:

    var yvals = new ChartValues<double>();
    

    【讨论】:

      【解决方案3】:
      cartesianChart1.Series = new SeriesCollection
          {
              new LineSeries
              {
                  Title = "Series 1",
                  Values = new ChartValues<double>(yvals)
              },
          };
      

      【讨论】:

        【解决方案4】:

        其他答案

                var temp1 = new List<double> { 10, 12, 8, 9, 11, 5, 14, 8, 3, 18, 15 };
                var temp2 = new List<double> { 12, 10, 18, 19, 21, 15, 4, 10, 13, 11, 22 };
        
                var tem1 = new ChartValues<double>();
                var tem2 = new ChartValues<double>();
        
                for (int i = 0; i < temp1.Count; i++)
                {
                    tem1.Add(temp1[i]);
                    tem2.Add(temp2[i]);
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-01
          • 2021-12-26
          相关资源
          最近更新 更多