从数据库获取实时图表数据

时间:2017-09-26 14:05:58

标签: c# wpf charts livecharts

我有一个名为tblWeeklyAudit的表,它有多行数据。我想阅读它并在实时图表上显示它。代码如下所示。

(from v in _DWEntities.DimScheduleJourneys
                join od in _DWEntities.OperatingDateVehicleJourneyMappings on v.VehicleJourneyUID equals od.VehicleJourneyUID
                where od.OperatingDateKey == _operatingDate && (vehicleJourneyUID == null || v.VehicleJourneyUID == vehicleJourneyUID)
                && (v.ScheduleDeparture != null && v.ScheduleDeparture != "" && v.ScheduleArrival != null && v.ScheduleArrival != "")
                select new ScheduleJourney
                {
                    VehicleJourneyUID = v.VehicleJourneyUID,
                    VehicleJourneyID = v.VehicleJourneyID,
                    TransportMode = v.TransportMode,
                    JourneyCode = v.JourneyCode == null ? v.VehicleJourneyCode : v.JourneyCode,
                    DepartureTime = v.ScheduleDeparture,
                    DirectionID = v.DirectionID,
                    BlockID = od.BlockId.Value,
                    BlockNumber = od.BlockNumber,
                    DutyBoard = od.DutyBoard,
                    DutyBoardID = od.DutyBoardID,
                    LineUID = v.LineUId,
                    LineName = v.LineName,
                    LineColor = v.LineColor,
                    //LineColor = v.LineName,
                    ServiceID = v.ServiceID,
                    ServiceCode = v.ServiceCode,
                    OperatorID = v.OperatorId,
                    OperatorCode = v.OperatorCode,
                    OperatorName = v.OperatorName,
                    JourneyPatternUID = v.JourneyPatternUID,
                    FirstStopPoint = v.FirstStopPoint,
                    LastStoppoint = v.LastStopPoint,
                    FirstStopPointName = v.FirstStopPointName,
                    LastStopPointName = v.LastStopPointName,
                    EndTime = v.ScheduleArrival,
                    OperatingDate = od.OperatingDate,
                    JourneyAccessibility = v.WheelchairAccessible.HasValue ? v.WheelchairAccessible.Value : false,
                    TenantId = v.TenantId.HasValue ? v.TenantId.Value : new Guid(),
                    DeadRunStartTime = v.DeadRunStartTime,
                    DeadRunEndTime = v.DeadRunEndTime,
                    MarketingName = v.MarketingName,
                    PrivateCode = v.PrivateCode,
                    //   GarageID = v.GarageID,
                    GarageCode = v.GarageCode,
                    IsFrequentService = v.FrequentService.HasValue ? v.FrequentService.Value : false,
                    FrequentServiceInterval = v.ScheduleFrequency,
                    Origin = v.Origin,
                    Destination = v.Destination,
                    IsMasterJourney = v.IsMasterJourney
                });

图表值由此行给出,我调用了表tblWeeklyAudit并从该缺陷表中调用。它有几个浮点值行

using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
using static MAS.clsPUB;

namespace MAS.Windows
{
    /// <summary>
    /// Interaction logic for Dash.xaml
    /// </summary>
    public partial class Dash : Window
    {
        public SeriesCollection SeriesCollection { get; set; }
        public string[] Labels { get; set; }
        //public Func<double, string> YFormatter { get; set; }


        public Dash()
        {
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            double test =0;
            if (CON.State == ConnectionState.Open)
            {
                CON.Close();
            }

            CON.ConnectionString = ConfigurationManager.ConnectionStrings["conDB"].ConnectionString;
            CON.Open();
            CMD = new SqlCommand("select * from tblWeeklyAudit", CON);
            RDR = CMD.ExecuteReader();
            if (RDR.Read())
            {
                 test = Convert.ToDouble(RDR["Defects"]);
            }


            SeriesCollection = new SeriesCollection
            {
                new LineSeries
                {
                    Values = new ChartValues<double> { test }
                },  

            };

            Labels = new[] { "Jan", "Feb", "Mar", "Apr", "May" };


            DataContext = this;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

将double值添加到List<double>,然后传递给ChartValues<double>类的构造函数:

private void LoadData()
{
    List<double> allValues = new List<double>();
    if (CON.State == ConnectionState.Open)
    {
        CON.Close();
    }

    CON.ConnectionString = ConfigurationManager.ConnectionStrings["conDB"].ConnectionString;
    CON.Open();
    CMD = new SqlCommand("select * from tblWeeklyAudit", CON);
    RDR = CMD.ExecuteReader();
    while (RDR.Read())
    {
        allValues.Add(Convert.ToDouble(RDR["Defects"]));
    }

    SeriesCollection = new SeriesCollection
            {
                new LineSeries
                {
                    Values = new ChartValues<double>(allValues)
                }
            };

    Labels = new[] { "Jan", "Feb", "Mar", "Apr", "May" };


    DataContext = this;
}

答案 1 :(得分:0)

要阅读多行,请执行以下操作:

var List<double> allValues = new List<double>();

if (RDR.HasRows)
        {
            while (RDR.Read())
            {
               allValues.Add(Convert.ToDouble(RDR["Defects"]));
            }
        }

然后在SeriesCollection中使用allValues。