Chart Multiple series different length generate du

2019-08-31 03:15发布

问题:

Look at this picture from my web application ASP.NET 4.0.

As you can se the lines have a different length. Also there is duplicate x axis entry.

The blue serie has a missing datapoint, the yellow does not. Question 1: How do I align them so the x- axis stays the same. Currently im doing this. And make the lines equally long? Question 2: Is there a way to make the chart interactive so that you can some and hold the cursor on the line to see data from that point, using ASP.NET?

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());
        Chart1.DataManipulator.InsertEmptyPoints(1, IntervalType.Days, serieName);
    }
    catch (Exception ex)
    {
        throw new InvalidOperationException(ex.Message);
    }
}

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

回答1:

This was the answer!

Thanks for pointing that out JBL!

     foreach (System.Web.UI.DataVisualization.Charting.Series serien in Chart1.Series)
     {
                foreach(System.Web.UI.DataVisualization.Charting.DataPoint dataPoint in serien.Points)
                {
                    if (dataPoint.YValues[0] == 0)
                    { 
                       dataPoint.IsEmpty = true;
                    }
                }

                serien.Sort(PointSortOrder.Ascending,sortBy:("X"));
     }