I am plotting a Microsoft System.Web.UI.DataVisualization.Charting.Chart with three series (two columns and an overlaying line). It appears that the y axis upper bound is auto calculated based on the data range in the first series added to the chart. Subsequently any series does not get rendered when its y value is greater than the first series y value. In this instance I would like it to use the maximum value from any series.

The series are added to the chart via a generic method:

foreach (var s in model.Series)
            {
                var chartSeries = new Series(s.Title);

                chartSeries.ChartType = s.Type;
                chartSeries.Color = s.DrawColour;

                // add the data to the series
                foreach (DataRow row in data.Tables[0].Rows)
                {
                    chartSeries.Points.AddXY(row[model.XAxis.DataColumnName], row[s.ColumnName]);
                }
                // does the series name come from a column? if not use the plain text. if the column has a caption use it, otherwise the column name.
                chartSeries.Name = data.Tables[0].Columns.Contains(s.Title) ? String.IsNullOrEmpty(data.Tables[0].Columns[s.Title].Caption) ? data.Tables[0].Columns[s.Title].Caption : data.Tables[0].Columns[s.Title].ColumnName : s.Title;

                chart.Series.Add(chartSeries);
            }

If I reverse the order the series are added to the chart then the plot area is correct, however the line series sits behind the columns.

Any ideas? Thanks.

有帮助吗?

解决方案

I'm assuming you have a single chart area that you are adding the series' to? If so, you can modify the maximum value of the Y axis that belongs to that chart area. It would be something like this:

// put this block above your foreach loop
double maxval = 100; // <-- put your maximum value here.
chart.ChartAreas[0].AxisY.Maximum = maxval;

Using LINQ, you can easily find the maximum value inside all of your series then set that discovered max value as the AxisY.Maximum.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top