[visual-studio]C# 和 XAML 线系列

标签: Visual-Studio
发布时间: 2014/3/1 3:51:10
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

现在我严重被困。

我有代码,从数据库读取的每个小时,呼叫量,然而当它来到的时间将它们映射到关系图,什么都不显示了......

如何映射它使用下面的命令:ls.ItemsSource = Enumerable.Range(0, counter).Select(k => new KeyValuePair<int, int>(callHour[k], callCounter[k])).ToArray();

这里是我所有的代码:

XAML:

    <DVC:Chart Name="Chart"
               Background="#463F3F">                
        <DVC:Chart.PlotAreaStyle>
            <Style TargetType="Grid">
                <Setter Property="Background" Value="Transparent" />
            </Style>
        </DVC:Chart.PlotAreaStyle>
    </DVC:Chart>

C# 中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Data;
using System.Windows.Controls.DataVisualization.Charting;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        AllAgentHourData();
    }

    private void AllAgentHourData()
    {
        string[] qId = new string[100];
        int[] callHour = new int[100];
        int[] callCounter = new int[100];
        int qCounter = 0;
        //int counter = 0;

        SqlConnection sqlConnection1 = new SqlConnection("Server=nl-reportserver;Database=RC_Dailer_WH;User Id=sa;Password=<password>");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader reader;
        sqlConnection1.Open();
        //cmd.CommandText = "Select distinct queueid from RC_call_logs order by queueid";
        cmd.CommandText = "Select CONVERT(date, starttime, 120) AS callDate, queueid, datepart(hour,convert(datetime,starttime,111)) AS cHour, count(id) as numberOfCalls from RC_call_logs where convert(date,starttime,120) = convert(date,getdate(),120) and queueid = 17000 group by CONVERT(date, starttime, 120), datepart(hour,convert(datetime,starttime,111)), queueid order by callDate, queueid, cHour";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = sqlConnection1;



        reader = cmd.ExecuteReader();
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                callHour[qCounter] = reader.GetInt32(2);
                callCounter[qCounter] = reader.GetInt32(3);
                qCounter++;
            }
        }
        else
        {
            MessageBox.Show("No Error message");
        }
        reader.Close();
        sqlConnection1.Close();

        LineSeries ls = new LineSeries();

        ls.Title = "17000";
        ls.IndependentValueBinding = new Binding("Key");
        ls.DependentValueBinding = new Binding("Value");

        ls.ItemsSource = Enumerable.Range(0, qCounter).Select(i=> new KeyValuePair<int, int>(callHour[i], callCounter[i])).ToArray();

    }

}
}

解决方法 1:

您需要将线系列添加到图表

Chart.Series.Add(ls);
官方微信
官方QQ群
31647020