Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying limits for rolling graph builder #927

Merged
merged 17 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Bonsai.Design.Visualizers/BarGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Collections.Generic;
using System.Drawing;
using ZedGraph;

namespace Bonsai.Design.Visualizers
{
class BarGraph : RollingGraph
{
public BarBase BaseAxis
{
get { return GraphPane.BarSettings.Base; }
set { GraphPane.BarSettings.Base = value; }
}

public BarType BarType
{
get { return GraphPane.BarSettings.Type; }
set { GraphPane.BarSettings.Type = value; }
}

internal override CurveItem CreateSeries(string label, IPointListEdit points, Color color)
{
var curve = new BarItem(label, points, color);
curve.Label.IsVisible = !string.IsNullOrEmpty(label);
curve.Bar.Fill.Type = FillType.Solid;
curve.Bar.Border.IsVisible = false;
return curve;
}

static int FindIndex(IPointListEdit series, string index)
{
for (int i = 0; i < series.Count; i++)
{
if (EqualityComparer<string>.Default.Equals(index, (string)series[i].Tag))
{
return i;
}
}

return -1;
}

public void AddValues(string index, double[] values)
{
if (values.Length > 0)
{
var updateIndex = FindIndex(Series[0], index);
if (updateIndex >= 0 && BaseAxis <= BarBase.X2) UpdateLastBaseX();
else if (updateIndex >= 0) UpdateLastBaseY();
else if (BaseAxis <= BarBase.X2) AddBaseX();
else AddBaseY();

void UpdateLastBaseX()
{
for (int i = 0; i < Series.Length; i++)
Series[i][updateIndex].Y = values[i];
}

void UpdateLastBaseY()
{
for (int i = 0; i < Series.Length; i++)
Series[i][updateIndex].X = values[i];
}

void AddBaseX()
{
for (int i = 0; i < Series.Length; i++)
Series[i].Add(new PointPair(0, values[i], index));
}

void AddBaseY()
{
for (int i = 0; i < Series.Length; i++)
Series[i].Add(new PointPair(values[i], 0, index));
}
}
}
}
}
35 changes: 31 additions & 4 deletions Bonsai.Design.Visualizers/BarGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,54 @@ public class BarGraphBuilder : SingleArgumentExpressionBuilder
/// Gets or sets a value specifying the axis on which the bars in the graph will be displayed.
/// </summary>
[TypeConverter(typeof(BaseAxisConverter))]
[Category(nameof(CategoryAttribute.Appearance))]
[Description("Specifies the axis on which the bars in the graph will be displayed.")]
public BarBase BaseAxis { get; set; }

/// <summary>
/// Gets or sets a value specifying how the different bars in the graph will be visually arranged.
/// </summary>
[Category(nameof(CategoryAttribute.Appearance))]
[Description("Specifies how the different bars in the graph will be visually arranged.")]
public BarType BarType { get; set; }

/// <summary>
/// Gets or sets the optional capacity used for rolling bar graphs. If no capacity is specified,
/// all data points will be displayed.
/// </summary>
[Category("Range")]
[Description("The optional capacity used for rolling bar graphs. If no capacity is specified, all data points will be displayed.")]
public int? Capacity { get; set; }

/// <summary>
/// Gets or sets a value specifying a fixed lower limit for the y-axis range.
/// If no fixed range is specified, the graph limits can be edited online.
/// </summary>
[Category("Range")]
[Description("Specifies the optional fixed lower limit of the y-axis range.")]
public double? Min { get; set; }

/// <summary>
/// Gets or sets a value specifying a fixed upper limit for the y-axis range.
/// If no fixed range is specified, the graph limits can be edited online.
/// </summary>
[Category("Range")]
[Description("Specifies the optional fixed upper limit of the y-axis range.")]
public double? Max { get; set; }

internal VisualizerController Controller { get; set; }

internal class VisualizerController
{
internal int Capacity;
internal int? Capacity;
internal double? Min;
internal double? Max;
internal Type IndexType;
internal string IndexLabel;
internal string[] ValueLabels;
internal Action<object, BarGraphVisualizer> AddValues;
internal BarBase BaseAxis;
internal BarType BarType;
}

/// <summary>
Expand All @@ -75,9 +97,14 @@ public override Expression Build(IEnumerable<Expression> arguments)
var valueParameter = Expression.Parameter(typeof(object));
var viewParameter = Expression.Parameter(typeof(BarGraphVisualizer));
var elementVariable = Expression.Variable(parameterType);
Controller = new VisualizerController();
Controller.Capacity = Capacity.GetValueOrDefault();
Controller.BaseAxis = BaseAxis;
Controller = new VisualizerController
{
Capacity = Capacity,
Min = Min,
Max = Max,
BaseAxis = BaseAxis,
BarType = BarType
};

var selectedIndex = GraphHelper.SelectIndexMember(elementVariable, IndexSelector, out Controller.IndexLabel);
Controller.IndexType = selectedIndex.Type;
Expand Down
155 changes: 155 additions & 0 deletions Bonsai.Design.Visualizers/BarGraphView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading