0

I Have a datatable with 3 columns Year (string) Line name (string)

        table.Columns.Add("YEAR", typeof(string));
        table.Columns.Add("LINE_NAME", typeof(string));
        table.Columns.Add("COUNT", typeof(double));

        table.Rows.Add(new object[] {"2018",    "Line1"              ,      2   });
        table.Rows.Add(new object[] {"2017",    "Line2"              ,      6   });
        table.Rows.Add(new object[] {"2018",    "Line2"              ,      1   });
        table.Rows.Add(new object[] {"2011",    "Line3"              ,      35  });
        table.Rows.Add(new object[] {"2013",    "Line3"              ,      143 });
        table.Rows.Add(new object[] {"2014",    "Line3"              ,      108 });
        table.Rows.Add(new object[] {"2015",    "Line3"              ,      50  });
        table.Rows.Add(new object[] {"2016",    "Line3"              ,      44  });
        table.Rows.Add(new object[] {"2017",    "Line3"              ,      34  });
        table.Rows.Add(new object[] {"2013",    "Line4"              ,      3   });
        table.Rows.Add(new object[] {"2014",    "Line4"              ,      6   });
        table.Rows.Add(new object[] {"2015",    "Line4"              ,      4   });
        table.Rows.Add(new object[] {"2016",    "Line4"              ,      1   });
        table.Rows.Add(new object[] {"2017",    "Line4"              ,      2   });

What i am trying to achieve is:

  1. Have the X axis as the YEAR

  2. Have the Y axis as the COUNT

  3. Have a separate line on the line chart for each line in LINE_NAME column.

I can't for the life of me figure out how to do it using UltraDataChart...

Can someone please be kind enough to implement even a two line chart based on this given data as an example?

The amount of "lines" that will exist is technically unknown, but surely any concrete example can be converted into a more generic loop

SwiftHands
  • 255
  • 3
  • 12

1 Answers1

0

UltraDataChart accepts as DataSource list of objects. What you need to do is first convert your data to a lists of objects like this:

var data = table.AsEnumerable() // convert the data to Enumerable so you can use linq over it
    .Select(r => new { year = r["YEAR"], lineName = r["LINE_NAME"], count = r["COUNT"] }) // convert data to list of anonimous objects
    .GroupBy(d => d.lineName, (key, value) => new { key = key, value = value.ToList() }) // group the data by line names
    .ToArray();

Then you need to add a new series for each line name. First add the necessary axes you need:

var xAxis = new NumericXAxis();
var yAxis = new NumericYAxis();
myChart.Axes.Add(xAxis);
myChart.Axes.Add(yAxis);

Then go through the data you have created and add all the necessary series:

foreach (var line in data)
{
    var lineData = line.value;
    var series = new ScatterLineSeries();
    series.XAxis = xAxis;
    series.YAxis = yAxis;
    series.XMemberPath = "year";
    series.YMemberPath = "count";
    series.DataSource = lineData;
    myChart.Series.Add(series);
}
wnvko
  • 1,985
  • 1
  • 14
  • 18