Skip to content

Commit

Permalink
fixes #1555
Browse files Browse the repository at this point in the history
Updated ViewModel to modify ScatterSeries configurations, including changing GeometrySize from 50 to 100 and adding a new ScatterSeries with StackGroup. In CoreScatterSeries.cs, removed _weightBounds and added _stackGroup, _minGeometrySize, and _geometrySize fields. Updated MinGeometrySize and GeometrySize properties, added StackGroup property, and modified Invalidate and GetBounds methods to use StackGroup. In SeriesContext.cs, added _weightBounds dictionary and methods to manage weight bounds. Added StackGroup property to IScatterSeries.cs.
  • Loading branch information
beto-rodriguez committed Oct 13, 2024
1 parent b372836 commit db5884d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
19 changes: 15 additions & 4 deletions samples/ViewModelsSamples/Scatter/Bubbles/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,25 @@ public ViewModel()
new ScatterSeries<WeightedPoint>
{
Values = values1,
GeometrySize = 50,
MinGeometrySize = 5
GeometrySize = 100,
MinGeometrySize = 5,
},
new ScatterSeries<WeightedPoint, RoundedRectangleGeometry>
{
Values = values2,
GeometrySize = 50,
MinGeometrySize = 5
GeometrySize = 100,
MinGeometrySize = 5,
StackGroup = 1
},
new ScatterSeries<WeightedPoint>
{
Values = [ new() { X = 10, Y = 10, Weight = 500 } ],
GeometrySize = 100,
MinGeometrySize = 5,
// use the stack group to shhare the Weight between series. // mark
// in this case, the previous series shares the same
// StackGroup, thus series share the Weigth bounds.
StackGroup = 1
}
];
}
Expand Down
27 changes: 18 additions & 9 deletions src/LiveChartsCore/CoreScatterSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// SOFTWARE.

using System;
using System.Collections;
using System.Collections.Generic;
using LiveChartsCore.Drawing;
using LiveChartsCore.Kernel;
Expand All @@ -48,8 +47,10 @@ public class CoreScatterSeries<TModel, TVisual, TLabel, TDrawingContext, TErrorG
where TDrawingContext : DrawingContext
where TErrorGeometry : class, ILineGeometry<TDrawingContext>, new()
{
private Bounds _weightBounds = new();
private IPaint<TDrawingContext>? _errorPaint;
private int? _stackGroup;
private double _minGeometrySize = 6d;
private double _geometrySize = 24d;

/// <summary>
/// Initializes a new instance of the <see cref="CoreScatterSeries{TModel, TVisual, TLabel, TDrawingContext, TErrorGeometry}"/> class.
Expand Down Expand Up @@ -80,15 +81,14 @@ public CoreScatterSeries(ICollection<TModel>? values)
/// <value>
/// The minimum size of the geometry.
/// </value>
public double MinGeometrySize { get; set; } = 6d;

public double MinGeometrySize { get => _minGeometrySize; set => SetProperty(ref _minGeometrySize, value); }
/// <summary>
/// Gets or sets the size of the geometry.
/// </summary>
/// <value>
/// The size of the geometry.
/// </value>
public double GeometrySize { get; set; } = 24d;
public double GeometrySize { get => _geometrySize; set =>SetProperty(ref _geometrySize, value); }

/// <summary>
/// Gets a value indicating whether the points in this series use weight.
Expand All @@ -102,6 +102,9 @@ public IPaint<TDrawingContext>? ErrorPaint
set => SetPaintProperty(ref _errorPaint, value, true);
}

/// <inheritdoc cref="IScatterSeries{TDrawingContext}.StackGroup"/>
public int? StackGroup { get => _stackGroup; set => SetProperty(ref _stackGroup, value); }

/// <inheritdoc cref="ChartElement{TDrawingContext}.Invalidate(Chart{TDrawingContext})"/>
public override void Invalidate(Chart<TDrawingContext> chart)
{
Expand All @@ -117,6 +120,9 @@ public override void Invalidate(Chart<TDrawingContext> chart)
var actualZIndex = ZIndex == 0 ? ((ISeries)this).SeriesId : ZIndex;
var clipping = GetClipRectangle(cartesianChart);

var weightStackIndex = StackGroup ?? ((ISeries)this).SeriesId;
var weightBounds = chart.SeriesContext.GetWeightBounds(weightStackIndex);

if (Fill is not null)
{
Fill.ZIndex = actualZIndex + 0.1;
Expand Down Expand Up @@ -148,8 +154,8 @@ public override void Invalidate(Chart<TDrawingContext> chart)
var gs = (float)GeometrySize;
var hgs = gs / 2f;
var sw = Stroke?.StrokeThickness ?? 0;
IsWeighted = _weightBounds.Max - _weightBounds.Min > 0;
var wm = -(GeometrySize - MinGeometrySize) / (_weightBounds.Max - _weightBounds.Min);
IsWeighted = weightBounds.Max - weightBounds.Min > 0;
var wm = -(GeometrySize - MinGeometrySize) / (weightBounds.Max - weightBounds.Min);

var uwx = xScale.MeasureInPixels(secondaryAxis.UnitWidth);
var uwy = yScale.MeasureInPixels(secondaryAxis.UnitWidth);
Expand Down Expand Up @@ -188,7 +194,7 @@ public override void Invalidate(Chart<TDrawingContext> chart)

if (IsWeighted)
{
gs = (float)(wm * (_weightBounds.Max - coordinate.TertiaryValue) + GeometrySize);
gs = (float)(wm * (weightBounds.Max - coordinate.TertiaryValue) + GeometrySize);
hgs = gs / 2f;
}

Expand Down Expand Up @@ -310,7 +316,10 @@ public override void Invalidate(Chart<TDrawingContext> chart)
public override SeriesBounds GetBounds(CartesianChart<TDrawingContext> chart, ICartesianAxis secondaryAxis, ICartesianAxis primaryAxis)
{
var seriesBounds = base.GetBounds(chart, secondaryAxis, primaryAxis);
_weightBounds = seriesBounds.Bounds.TertiaryBounds;

chart.SeriesContext.AppendWeightBounds(
StackGroup ?? ((ISeries)this).SeriesId, seriesBounds.Bounds.TertiaryBounds);

return seriesBounds;
}

Expand Down
33 changes: 33 additions & 0 deletions src/LiveChartsCore/Kernel/SeriesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class SeriesContext<TDrawingContext>(IEnumerable<IChartSeries<TDrawingCon
private readonly Dictionary<IChartSeries<TDrawingContext>, int> _boxPositions = [];
private readonly Dictionary<int, int> _stackColumnPositions = [];
private readonly Dictionary<int, int> _stackRowsPositions = [];
private readonly Dictionary<int, Bounds> _weightBounds = [];

private readonly Dictionary<string, Stacker<TDrawingContext>> _stackers = [];

Expand Down Expand Up @@ -311,4 +312,36 @@ private void CalculatePieLabelsOuterSpace<TLabel>()
}

#endregion

#region scatter

/// <summary>
/// Gets the weight bounds.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public Bounds GetWeightBounds(int key)
{
if (_weightBounds.TryGetValue(key, out var bounds)) return bounds;

bounds = new Bounds();
_weightBounds[key] = bounds;

return bounds;
}

/// <summary>
/// Appends the weight bounds.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AppendWeightBounds(int key, Bounds value)
{
var bounds = GetWeightBounds(key);

if (value.Max > bounds.Max) bounds.Max = value.Max;
if (value.Min < bounds.Min) bounds.Min = value.Min;
}

#endregion
}
6 changes: 6 additions & 0 deletions src/LiveChartsCore/Kernel/Sketches/IScatterSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ public interface IScatterSeries<TDrawingContext> :
/// The minimum size of the geometry.
/// </value>
double MinGeometrySize { get; set; }

/// <summary>
/// Gets or sets and index for the stack group, if multiple weighed series share
/// the same stack group, they will be stacked, defualt is null and means that it is not stacked.
/// </summary>
int? StackGroup { get; set; }
}

0 comments on commit db5884d

Please sign in to comment.