I'm using the .NET System.Windows.Forms.DataVisualization.Charting
types and want to assign a MarkerStyle
to each Series I create preferably skipping MarkerStyle.None
, but getting the next one each time and cycling around to the beginning again.
private MarkerStyle markers = new MarkerStyle();
public Series CreateSeries(string name)
{
var s = chart1.Series.Add(name);
s.ChartType = SeriesChartType.Point;
s.MarkerSize = 7;
s.MarkerStyle = Cycle(markers ,MarkerStyle.None); // get next marker style
s.XValueType = ChartValueType.DateTime;
return s;
}
I tried variations of the following code I found, all of which don't compile:
public static IEnumerable<T>Cycle<T>(IEnumerable<T> iterable,<T> skip)
{
while (true)
{
foreach (T t in iterable)
{
if(t == skip)
continue;
yield return t;
}
}
}
Gives error
CS0411. The type arguments for method 'MyClass.Cycle(System.Collections.Generic.IEnumerable)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Although I have a specific use it would be nice for a generic answer.
Edit: Having looked at the code above more closely and I see it was never going to work outside a foreach loop. Ho hum. Time to write a hack:
private int _markerIter = 0;
private readonly int _markerCount = Enum.GetValues(typeof(MarkerStyle)).Length;
public MarkerStyle Cycle()
{
if (_markerIter + 1 == _markerCount)
{
_markerIter = 1; //skips MarkerStyle.None which is == 0
}
else
{
_markerIter++;
}
return (MarkerStyle) _markerIter;
}