C# Charts add multiple series from datatable

2020-07-18 10:58发布

问题:

I retrieve several datatables from my DB, which vary in size. This one of 2 is just an example.

See the structure here!

I managed to create the 2 different series and have them show up on the legend.

My question is on how to bind that data to the respective series. The series name are created from column doman_namn and the amount of series are created from the "antal" column which holds the number of unique URLS.

QUESTION HOW TO BIND ADDY and ADDX to the chart it fails now.

This is my code so far...

Chart1.DataSource = dt;

int amountofrows = Convert.ToInt32(dt.Rows[0]["antal"].ToString());

for (int i = 0; i < amountofrows; i++)
{
    string serieName = dt.Rows[i]["doman_namn"].ToString();

    Chart1.Series.Add(serieName);
    Chart1.Series[i].ChartType = SeriesChartType.Line;

    foreach(DataRow dr in dt.Rows)
    {
        try
        {
            if (String.Equals(serieName,dr["doman_namn"].ToString(), StringComparison.Ordinal))     
            {
            Chart1.Series[serieName].Points.AddY(Convert.ToDouble(dr["ranking_position"]));
            Chart1.Series[serieName].Points.AddY(Convert.ToDouble(dr["ranking_date"]));
            }
        }
        catch (Exception)
        {
            throw new InvalidOperationException("Failed when adding points");
        }
    }
}


Chart1.DataBind();
Chart1.Visible = true;

CODE AFTER HELP FROM GREGOR

for (int i = 0; i < amountofrows; i++)
{
    string serieName = dt.Rows[i]["doman_namn"].ToString();

    Chart1.Series.Add(serieName);
    Chart1.Series[i].ChartType = SeriesChartType.Line;

    Chart1.Series[serieName].XValueMember = "ranking_date";
    Chart1.Series[serieName].YValueMembers = "ranking_position";

}
Chart1.DataBind();

回答1:

Take a look at one of my samples how to bind DataTable to MS Chart using code:

How to draw Chart based on DataTable from console application?

Hope you will find it usefull.

Here are the key points:

//setting the source from datatable....
chart.DataSource = dt;

//setting XValueMember for first serie (Name is column inside datasource)...
serie1.XValueMember = "ranking_position";

//setting YValueMembers...
serie1.YValueMembers = "ranking_date";

Here is another link for binding multiple series:

http://dotnetslackers.com/articles/net/Binding-a-Microsoft-Chart-with-a-Dataset.aspx



回答2:

I managed to do it myself, but you Gregor Primar pushed me in the right direction!

What was important was that you set the valuetype for the X and Y-axis. As decimal type was not an option I used auto as type.

Chart1.DataSource = dt;

int amountofrows = Convert.ToInt32(dt.Rows[0]["antal"].ToString());

for (int i = 0; i < amountofrows; i++)
{
    List<string> xvals = new List<string>();
    List<decimal> yvals = new List<decimal>();
    string serieName = dt.Rows[i]["doman_namn"].ToString();
    Chart1.Series.Add(serieName);
    Chart1.Series[i].ChartType = SeriesChartType.Line;

    foreach(DataRow dr in dt.Rows)
    {
        try
        {


        if (String.Equals(serieName,dr["doman_namn"].ToString(), StringComparison.Ordinal))     
        {                    
            xvals.Add(dr["ranking_date"].ToString());
            yvals.Add(Convert.ToDecimal(dr["ranking_position"].ToString()));              
        }

        }
        catch (Exception)
        {

            throw new InvalidOperationException("Diagrammet kunde inte ritas upp");
        }
    }
    try
    {
        Chart1.Series[serieName].XValueType = ChartValueType.String;
        Chart1.Series[serieName].YValueType = ChartValueType.Auto;
        Chart1.Series[serieName].Points.DataBindXY(xvals.ToArray(), yvals.ToArray());
    }
    catch (Exception)
    {

        throw new InvalidOperationException("Kunde inte bind punkterna till Diagrammet");
    }    
}

Chart1.DataBind();
Chart1.Visible = true;


标签: c# charts