Skip to content

Commit

Permalink
Merge pull request #590 from beto-rodriguez/dev
Browse files Browse the repository at this point in the history
2.0.0-beta.360
  • Loading branch information
beto-rodriguez committed Aug 20, 2022
2 parents efbed07 + 1521695 commit 38923ab
Show file tree
Hide file tree
Showing 82 changed files with 956 additions and 581 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;

namespace ViewModelsSamples.VisualTest.TwoChartsOneSeries;
Expand All @@ -10,17 +11,29 @@ public partial class ViewModel
{
public ViewModel()
{
var values = new int[100];
//var values = new int[100];
//var r = new Random();
//var t = 0;

//for (var i = 0; i < 100; i++)
//{
// t += r.Next(-90, 100);
// values[i] = t;
//}

//Series = new ISeries[] { new StepLineSeries<int> { Values = values } };

var values = new ObservableValue[100];
var r = new Random();
var t = 0;

for (var i = 0; i < 100; i++)
{
t += r.Next(-90, 100);
values[i] = t;
values[i] = new(t);
}

Series = new ISeries[] { new StepLineSeries<int> { Values = values } };
Series = new ISeries[] { new StepLineSeries<ObservableValue> { Values = values } };
}

public ISeries[] Series { get; set; }
Expand Down
147 changes: 144 additions & 3 deletions src/LiveChartsCore/CartesianChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class CartesianChart<TDrawingContext> : Chart<TDrawingContext>
internal readonly HashSet<IPlane<TDrawingContext>> _everMeasuredAxes = new();
internal readonly HashSet<Section<TDrawingContext>> _everMeasuredSections = new();
private readonly ICartesianChartView<TDrawingContext> _chartView;
private readonly ISizedGeometry<TDrawingContext> _zoomingSection;
private int _nextSeries = 0;
private double _zoomingSpeed = 0;
private readonly bool _requiresLegendMeasureAlways = false;
Expand All @@ -58,16 +59,23 @@ public class CartesianChart<TDrawingContext> : Chart<TDrawingContext>
/// <param name="view">The view.</param>
/// <param name="defaultPlatformConfig">The default platform configuration.</param>
/// <param name="canvas">The canvas.</param>
/// <param name="zoomingSection">The zooming section.</param>
/// <param name="requiresLegendMeasureAlways">Forces the legends to redraw with every measure request.</param>
public CartesianChart(
ICartesianChartView<TDrawingContext> view,
Action<LiveChartsSettings> defaultPlatformConfig,
MotionCanvas<TDrawingContext> canvas,
ISizedGeometry<TDrawingContext>? zoomingSection,
bool requiresLegendMeasureAlways = false)
: base(canvas, defaultPlatformConfig, view)
{
_chartView = view;
_requiresLegendMeasureAlways = requiresLegendMeasureAlways;
_zoomingSection = zoomingSection ?? throw new Exception($"{nameof(zoomingSection)} is required.");
_zoomingSection.X = -1;
_zoomingSection.Y = -1;
_zoomingSection.Width = 0;
_zoomingSection.Height = 0;
}

/// <summary>
Expand Down Expand Up @@ -227,7 +235,7 @@ public void Zoom(LvcPoint pivot, ZoomDirection direction, double? scaleFactor =
maxt = max + ld * 0.5 * dir;
}

if (maxt - mint < xi.DataBounds.MinDelta * 5) return;
if (direction == ZoomDirection.ZoomIn && maxt - mint < xi.DataBounds.MinDelta * 3) return;

var xm = (max - min) * (isActive ? MaxAxisActiveBound : MaxAxisBound);
if (maxt > xi.DataBounds.Max && direction == ZoomDirection.ZoomOut) maxt = xi.DataBounds.Max + xm;
Expand Down Expand Up @@ -281,7 +289,7 @@ public void Zoom(LvcPoint pivot, ZoomDirection direction, double? scaleFactor =
maxt = max + ld * 0.5 * dir;
}

if (maxt - mint < yi.DataBounds.MinDelta * 5) return;
if (direction == ZoomDirection.ZoomIn && maxt - mint < yi.DataBounds.MinDelta * 3) return;

var ym = (max - min) * (isActive ? MaxAxisActiveBound : MaxAxisBound);
if (maxt > yi.DataBounds.Max && direction == ZoomDirection.ZoomOut) maxt = yi.DataBounds.Max + ym;
Expand Down Expand Up @@ -688,9 +696,9 @@ protected internal override void Measure()
}

var drawablePlane = (IPlane<TDrawingContext>)axis;
_ = _everMeasuredAxes.Add(drawablePlane);
if (drawablePlane.IsVisible)
{
_everMeasuredAxes.Add(drawablePlane);
drawablePlane.Measure(this);
_ = toDeleteAxes.Remove(drawablePlane);
}
Expand Down Expand Up @@ -782,4 +790,137 @@ public override void Unload()

IsFirstDraw = true;
}

private LvcPoint? _sectionZoomingStart = null;

internal override void InvokePointerDown(LvcPoint point, bool isSecondaryAction)
{
if (isSecondaryAction)
{
_sectionZoomingStart = point;

_zoomingSection.X = point.X;
_zoomingSection.Y = point.Y;

var xMode = (_zoomMode & ZoomAndPanMode.X) == ZoomAndPanMode.X;
var yMode = (_zoomMode & ZoomAndPanMode.Y) == ZoomAndPanMode.Y;

if (!xMode)
{
_zoomingSection.X = DrawMarginLocation.X;
_zoomingSection.Width = DrawMarginSize.Width;
}

if (!yMode)
{
_zoomingSection.Y = DrawMarginLocation.Y;
_zoomingSection.Height = DrawMarginSize.Height;
}

Update();
return;
}

base.InvokePointerDown(point, isSecondaryAction);
}

internal override void InvokePointerMove(LvcPoint point)
{
if (_sectionZoomingStart is not null)
{
var xMode = (_zoomMode & ZoomAndPanMode.X) == ZoomAndPanMode.X;
var yMode = (_zoomMode & ZoomAndPanMode.Y) == ZoomAndPanMode.Y;

if (xMode) _zoomingSection.Width = point.X - _sectionZoomingStart.Value.X;
if (yMode) _zoomingSection.Height = point.Y - _sectionZoomingStart.Value.Y;

Update();
return;
}

base.InvokePointerMove(point);
}

internal override void InvokePointerUp(LvcPoint point, bool isSecondaryAction)
{
if (_sectionZoomingStart is not null)
{
if ((_zoomMode & ZoomAndPanMode.X) == ZoomAndPanMode.X)
{
for (var i = 0; i < XAxes.Length; i++)
{
var x = XAxes[i];

var xi = ScaleUIPoint(_sectionZoomingStart.Value, i, 0)[0];
var xj = ScaleUIPoint(point, i, 0)[0];

double xMax, xMin;

if (xi > xj)
{
xMax = xi;
xMin = xj;
}
else
{
xMax = xj;
xMin = xi;
}

if (xMax > (x.MaxLimit ?? double.MaxValue)) xMax = x.MaxLimit ?? double.MaxValue;
if (xMin < (x.MinLimit ?? double.MinValue)) xMin = x.MinLimit ?? double.MinValue;

if (xMax - xMin > x.DataBounds.MinDelta * 3)
{
x.MinLimit = xMin;
x.MaxLimit = xMax;
}
}
}

if ((_zoomMode & ZoomAndPanMode.Y) == ZoomAndPanMode.Y)
{
for (var i = 0; i < YAxes.Length; i++)
{
var y = YAxes[i];

var yi = ScaleUIPoint(_sectionZoomingStart.Value, 0, i)[1];
var yj = ScaleUIPoint(point, 0, i)[1];

double yMax, yMin;

if (yi > yj)
{
yMax = yi;
yMin = yj;
}
else
{
yMax = yj;
yMin = yi;
}

if (yMax > (y.MaxLimit ?? double.MaxValue)) yMax = y.MaxLimit ?? double.MaxValue;
if (yMin < (y.MinLimit ?? double.MinValue)) yMin = y.MinLimit ?? double.MinValue;

if (yMax - yMin > y.DataBounds.MinDelta * 3)
{
y.MinLimit = yMin;
y.MaxLimit = yMax;
}
}
}

_zoomingSection.X = -1;
_zoomingSection.Y = -1;
_zoomingSection.Width = 0;
_zoomingSection.Height = 0;
Update();

_sectionZoomingStart = null;
return;
}

base.InvokePointerUp(point, isSecondaryAction);
}
}
7 changes: 3 additions & 4 deletions src/LiveChartsCore/Chart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,7 @@ internal void ClearTooltipData()

Canvas.Invalidate();
}

internal void InvokePointerDown(LvcPoint point)
internal virtual void InvokePointerDown(LvcPoint point, bool isSecondaryAction)
{
PointerDown?.Invoke(point);

Expand All @@ -366,12 +365,12 @@ internal void InvokePointerDown(LvcPoint point)
View.OnDataPointerDown(iterable, point);
}

internal void InvokePointerMove(LvcPoint point)
internal virtual void InvokePointerMove(LvcPoint point)
{
PointerMove?.Invoke(point);
}

internal void InvokePointerUp(LvcPoint point)
internal virtual void InvokePointerUp(LvcPoint point, bool isSecondaryAction)
{
PointerUp?.Invoke(point);
}
Expand Down
2 changes: 1 addition & 1 deletion src/LiveChartsCore/ColumnSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public override void Measure(Chart<TDrawingContext> chart)
var secondary = secondaryScale.ToPixels(point.SecondaryValue);
var b = Math.Abs(primary - helper.p);

if (point.IsNull)
if (point.IsEmpty)
{
if (visual is not null)
{
Expand Down
20 changes: 12 additions & 8 deletions src/LiveChartsCore/Defaults/DateTimePoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
// SOFTWARE.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using LiveChartsCore.Kernel;
using LiveChartsCore.Kernel.Sketches;

namespace LiveChartsCore.Defaults;

Expand All @@ -48,8 +50,9 @@ public DateTimePoint()
/// <param name="value">The value.</param>
public DateTimePoint(DateTime dateTime, double? value)
{
_dateTime = dateTime;
_value = value;
DateTime = dateTime;
Value = value;
Coordinate = value is null ? Coordinate.Empty : new(dateTime.Ticks, value.Value);
}

/// <summary>
Expand All @@ -68,14 +71,14 @@ public DateTimePoint(DateTime dateTime, double? value)
/// </value>
public double? Value { get => _value; set { _value = value; OnPropertyChanged(); } }

/// <inheritdoc cref="IChartEntity.ChartPoint"/>
public ChartPoint? ChartPoint { get; set; }
/// <inheritdoc cref="IChartEntity.EntityIndex"/>
public int EntityIndex { get; set; }

/// <inheritdoc cref="IChartEntity.EntityId"/>
public int EntityId { get; set; }
/// <inheritdoc cref="IChartEntity.ChartPoints"/>
public Dictionary<IChartView, ChartPoint>? ChartPoints { get; set; }

/// <inheritdoc cref="ICoordinate.Coordinate"/>
public Coordinate Coordinate => new(_dateTime.Ticks, _value ?? 0d);
/// <inheritdoc cref="IChartEntity.Coordinate"/>
public Coordinate Coordinate { get; private set; } = Coordinate.Empty;

/// <summary>
/// Occurs when a property value changes.
Expand All @@ -89,6 +92,7 @@ public DateTimePoint(DateTime dateTime, double? value)
/// <param name="propertyName">Name of the property.</param>
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
Coordinate = _value is null ? Coordinate.Empty : new(_dateTime.Ticks, _value.Value);
PropertyChanged?.Invoke(propertyName, new PropertyChangedEventArgs(propertyName));
}
}

0 comments on commit 38923ab

Please sign in to comment.