You can do this via a custom point style. The PointStyle type looks and feels like an enum, but if you look at its definition you'll notice that it's actually an abstract base class and that its values are actually static properties that return implementations of that base class. This provides the ease of use of an enum, but actually lets you define custom values with extended behavior, which cannot be done with a normal enum. Several types in the Measurement Studio UI assembly follow this pattern, including LineStyle, Border, LedStyle, SwitchStyle, etc. Some of these types, including PointStyle, also have the concept of value dependency, which allows the type to have different behavior depending on the context that it's used. For the PointStyle, this context could be the value.
For example, in your code example, you wanted to have a red point style when the value is greater than .1, a yellow point style when the value was > .05 and < .1, and a green point style for any other value. The point style below demonstrates how to do this and also takes it a step further by also displaying different styles for these different ranges:
class DynamicPointStyle : PointStyle
{
public override bool IsValueDependent
{
get
{
// Specify value dependency since we want different colors
// depending on the value of the point.
return true;
}
}
public override void Draw(object context, PointStyleDrawArgs args)
{
double yValue = args.Y;
if (yValue > .1)
PointStyle.Plus.Draw(context, CreateArgs(args, Color.Red));
else if (yValue > .05)
PointStyle.EmptyCircle.Draw(context, CreateArgs(args, Color.Yellow));
else
PointStyle.SolidCircle.Draw(context, CreateArgs(args, Color.Green));
}
private static PointStyleDrawArgs CreateArgs(PointStyleDrawArgs args, Color color)
{
return new PointStyleDrawArgs(args.Graphics, color, args.Size);
}
}
To test this class, create a new Windows Forms project, add a ScatterGraph to the form, and add the following code to the form:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
const int dataLength = 50;
double[] xData = new double[dataLength], yData = new double[dataLength];
Random rnd = new Random();
for (int i = 0; i < dataLength; ++i)
{
xData[i] = i;
yData[i] = rnd.NextDouble() * 0.25;
}
scatterGraph1.PlotXY(xData, yData);
scatterPlot1.LineStyle = LineStyle.None;
scatterPlot1.PointStyle = new DynamicPointStyle();
}
Run the application and you'll see a result similar to the screenshot attached below. For another custom point style example or to see examples of creating a custom line style or border, see the CustomStyles example that ships with Measurement Studio 7.0.
- Elton