Refactor CreateChartState
MODIFY
WalletWasabi.Fluent/Controls/LineChart.cs
WalletWasabi.Fluent/Controls/LineChart.cs
@@ -161,26 +161,39 @@ namespace WalletWasabi.Fluent.Controls
private LineChartState CreateChartState(double width, double height)
{
- var state = new LineChartState();
+ var state = new LineChartState
+ {
+ ChartWidth = width,
+ ChartHeight = height,
+ AreaMargin = AreaMargin
+ };
- // Char Size
+ state.AreaWidth = width - state.AreaMargin.Left - state.AreaMargin.Right;
+ state.AreaHeight = height - state.AreaMargin.Top - state.AreaMargin.Bottom;
- state.ChartWidth = width;
- state.ChartHeight = height;
+ SetStateAreaPoints(state);
- // Char Area
+ SetStateXAxisLabels(state);
+ SetStateYAxisLabels(state);
- state.AreaMargin = AreaMargin;
- state.AreaWidth = width - state.AreaMargin.Left - state.AreaMargin.Right;
- state.AreaHeight = height - state.AreaMargin.Top - state.AreaMargin.Bottom;
+ SetStateXAxisCursor(state);
- // X & Y Axis Values
+ return state;
+ }
+ private void SetStateAreaPoints(LineChartState state)
+ {
var xAxisValues = XAxisValues;
var yAxisValues = YAxisValues;
- if (xAxisValues is not null && xAxisValues.Count > 1 && yAxisValues is not null && yAxisValues.Count > 1)
+ if (xAxisValues is null || xAxisValues.Count <= 1 || yAxisValues is null || yAxisValues.Count <= 1)
{
+ state.XAxisStep = double.NaN;
+ state.YAxisStep = double.NaN;
+ state.Points = null;
+ return;
+ }
+
state.XAxisStep = state.AreaWidth / (xAxisValues.Count - 1);
state.YAxisStep = state.AreaHeight / (yAxisValues.Count - 1);
@@ -203,51 +216,45 @@ namespace WalletWasabi.Fluent.Controls
state.Points[i] = new Point(i * state.XAxisStep, yAxisValuesScaled[i]);
}
}
- else
- {
- state.XAxisStep = double.NaN;
- state.YAxisStep = double.NaN;
- state.Points = null;
- }
- // X Axis Labels
-
- var xAxisLabels = XAxisLabels;
- if (xAxisLabels is not null)
+ private void SetStateXAxisLabels(LineChartState state)
{
- state.XAxisLabels = xAxisLabels.ToList();
- }
- else
+ var xAxisLabels = XAxisLabels;
+ if (xAxisLabels is null)
{
if (XAxisStroke is not null && XAxisValues is not null)
{
state.XAxisLabels = XAxisValues.Select(x => x.ToString(CultureInfo.InvariantCulture)).ToList();
}
}
-
- // Y Axis Labels
-
- var yAxisLabels = YAxisLabels;
- if (yAxisLabels is not null)
+ else
{
- state.YAxisLabels = yAxisLabels.ToList();
+ state.XAxisLabels = xAxisLabels.ToList();
}
- else
+ }
+
+ private void SetStateYAxisLabels(LineChartState state)
+ {
+ var yAxisLabels = YAxisLabels;
+ if (yAxisLabels is null)
{
if (YAxisStroke is not null && YAxisValues is not null)
{
state.YAxisLabels = YAxisValues.Select(x => x.ToString(CultureInfo.InvariantCulture)).ToList();
}
}
+ else
+ {
+ state.YAxisLabels = yAxisLabels.ToList();
+ }
+ }
- // X Axis Cursor Position
-
+ private void SetStateXAxisCursor(LineChartState state)
+ {
var xAxisMinValue = XAxisMinValue;
var xAxisMaxValue = XAxisMaxValue;
var xAxisCurrentValue = XAxisCurrentValue;
state.XAxisCursorPosition = ScaleHorizontal(xAxisMaxValue - xAxisCurrentValue, xAxisMaxValue, state.AreaWidth);
-
- return state;
}
private void DrawAreaFill(DrawingContext context, LineChartState state)

Refactor CreateChartState