winforms Infragistics Lineseries次要系列和图例未显示

uqcuzwp8  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(129)

我有3列数据表;我要在图表上显示INST_ID和Legends的值。
我的问题是,INST_ID可以变化。它可能有5个不同的INST_ID。
如何显示属于INST_ID的所有系列和图例?
这是当前的结果

下面是我为Ultra Data Chart生成的代码

private void Form1_Load(object sender, EventArgs e)
    { 
        dtLineTest.Columns.Add("SAMPLE_TIME",typeof(DateTime));
        dtLineTest.Columns.Add("INST_ID", typeof(Int32));
        dtLineTest.Columns.Add("VALUE", typeof(Int32));

        dtLineTest.Rows.Add(DateTime.Now,1,14);
        dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-1), 1, 18);
        dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-2), 1, 11);
        dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-3), 1, 7);
        dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-4), 1, 21);

        dtLineTest.Rows.Add(DateTime.Now, 2, 16);
        dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-1), 2, 3);
        dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-2), 2, 5);
        dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-3), 2, 12);
        dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-4), 2, 13);

        var xAxis = new CategoryXAxis();
        var yAxis = new NumericYAxis();

        xAxis.DataSource = dtLineTest;
        xAxis.Label = "SAMPLE_TIME";

        var legend = new UltraLegend();
        var lineSeries = new LineSeries();
        lineSeries.DataSource = dtLineTest;
        lineSeries.ValueMemberPath = "VALUE";
        lineSeries.Title = "INST_ID";
        lineSeries.XAxis = xAxis;
        lineSeries.YAxis = yAxis;
        lineSeries.Legend = legend;

        ultraDataChart1.Axes.Add(xAxis);
        ultraDataChart1.Axes.Add(yAxis);
        ultraDataChart1.Series.Add(lineSeries);
    }
wqlqzqxt

wqlqzqxt1#

要在图表中显示多条线,您需要在图表中添加多个lineSeries。在您的情况下,您可以按INST_ID列对表中的数据进行分组,并为每个组创建一个lineSeries。如下所示:

var groupedData = dtLineTest.AsEnumerable().GroupBy(g => g["INST_ID"]);
foreach (var group in groupedData)
{
    var dt = new DataTable();
    dt.Columns.Add("SAMPLE_TIME", typeof(DateTime));
    dt.Columns.Add("INST_ID", typeof(Int32));
    dt.Columns.Add("VALUE", typeof(Int32));
    foreach (var row in group)
    {
        dt.Rows.Add(row.ItemArray);
    }
    var legend = new UltraLegend();
    var lineSeries = new LineSeries();
    lineSeries.DataSource = dt;
    lineSeries.ValueMemberPath = "VALUE";
    lineSeries.Title = "INST_ID";
    lineSeries.XAxis = xAxis;
    lineSeries.YAxis = yAxis;
    lineSeries.Legend = legend;
    ultraDataChart1.Series.Add(lineSeries);
}
  • 注意:如果您事先为每个系列准备了单独的表,这会容易得多。*

相关问题