Skip to content

Commit

Permalink
perfomance buff
Browse files Browse the repository at this point in the history
  • Loading branch information
beto-rodriguez committed Aug 11, 2016
1 parent f62215a commit f330039
Show file tree
Hide file tree
Showing 22 changed files with 156 additions and 252 deletions.
5 changes: 2 additions & 3 deletions Core/ChartUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
using System;
using System.Diagnostics;
using LiveCharts.Charts;
using LiveCharts.Definitions.Charts;
using LiveCharts.Definitions.Series;
using LiveCharts.Helpers;

namespace LiveCharts
{
public class ChartUpdater : IChartUpdater
public class ChartUpdater
{
public ChartCore Chart { get; set; }
public bool IsUpdating { get; set; }
Expand Down Expand Up @@ -73,7 +72,7 @@ protected void Update(bool restartsAnimations = false)
foreach (var series in Chart.View.ActualSeries)
{
InitializeSeriesView(series);
series.ActualValues.GetLimits(series);
series.ActualValues.Initialize(series);
series.InitializeColors();
series.DrawSpecializedElements();
}
Expand Down
152 changes: 94 additions & 58 deletions Core/ChartValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Linq;
using LiveCharts.Charts;
using LiveCharts.Configurations;
using LiveCharts.Definitions.Points;
using LiveCharts.Definitions.Series;
using LiveCharts.Dtos;
using LiveCharts.Helpers;
Expand All @@ -36,8 +37,6 @@ namespace LiveCharts
/// <typeparam name="T">Type to plot, notice you could need to configure the type.</typeparam>
public class ChartValues<T> : NoisyCollection<T>, IChartValues
{
private IPointEvaluator<T> DefaultConfiguration { get; set; }

#region Constructors

/// <summary>
Expand All @@ -52,17 +51,16 @@ public ChartValues()
#endregion

#region Properties

private IPointEvaluator<T> DefaultConfiguration { get; set; }
private Dictionary<ISeriesView, PointTracker> Trackers { get; set; }

#endregion

#region Public Methods

/// <summary>
/// Evaluates the limits in the chart values
/// </summary>
public void GetLimits(ISeriesView seriesView)
public void Initialize(ISeriesView seriesView)
{
var config = GetConfig(seriesView);
if (config == null) return;
Expand All @@ -76,20 +74,53 @@ public void GetLimits(ISeriesView seriesView)

var tracker = GetTracker(seriesView);

for (var index = 0; index < Count; index++)
var cp = new ChartPoint();

if (seriesView is IFinancialSeriesView)
{
for (var index = 0; index < Count; index++)
{
var item = this[index];
config.Evaluate(index, item, cp);

if (cp.X < xMin) xMin = cp.X;
if (cp.X > xMax) xMax = cp.X;

if (cp.Low < yMin) yMin = cp.Low;
if (cp.High > yMax) yMax = cp.High;

if (cp.Weight < wMin) wMin = cp.Weight;
if (cp.Weight > wMax) wMax = cp.Weight;
}
} else if (seriesView is IScatterSeriesView)
{
var item = this[index];
var pair = new KeyValuePair<int, T>(index, item);
var xyw = config.GetEvaluation(pair);
for (var index = 0; index < Count; index++)
{
var item = this[index];
config.Evaluate(index, item, cp);

if (xyw[0].X < xMin) xMin = xyw[0].X;
if (xyw[1].X > xMax) xMax = xyw[1].X;
if (cp.X < xMin) xMin = cp.X;
if (cp.X > xMax) xMax = cp.X;

if (cp.Y < yMin) yMin = cp.Y;
if (cp.Y > yMax) yMax = cp.Y;

if (cp.Weight < wMin) wMin = cp.Weight;
if (cp.Weight > wMax) wMax = cp.Weight;
}
} else
{
for (var index = 0; index < Count; index++)
{
var item = this[index];
config.Evaluate(index, item, cp);

if (xyw[0].Y < yMin) yMin = xyw[0].Y;
if (xyw[1].Y > yMax) yMax = xyw[1].Y;
if (cp.X < xMin) xMin = cp.X;
if (cp.X > xMax) xMax = cp.X;

if (xyw[0].W < wMin) wMin = xyw[0].W;
if (xyw[1].W > wMax) wMax = xyw[1].W;
if (cp.Y < yMin) yMin = cp.Y;
if (cp.Y > yMax) yMax = cp.Y;
}
}

tracker.Limit1 = new CoreLimit(double.IsInfinity(xMin)
Expand Down Expand Up @@ -133,40 +164,14 @@ public IEnumerable<ChartPoint> GetPoints(ISeriesView seriesView)
}
}

ChartPoint cp;

if (!isClass)
{
if (!tracker.Indexed.TryGetValue(index, out cp))
{
cp = new ChartPoint
{
Instance = value,
Key = index
};
tracker.Indexed[index] = cp;
}
}
else
{
if (!tracker.Referenced.TryGetValue(value, out cp))
{
cp = new ChartPoint
{
Instance = value,
Key = index
};
tracker.Referenced[value] = cp;
}
}
var cp = GetChartPoint(isClass, tracker, index, value);

cp.Gci = gci;

cp.Instance = value;
cp.Key = index;
cp.SeriesView = seriesView;

config.SetAll(new KeyValuePair<int, T>(index, value), cp);
config.Evaluate(index, value, cp);

yield return cp;
}
Expand Down Expand Up @@ -220,15 +225,7 @@ public PointTracker GetTracker(ISeriesView view)

#endregion

#region Private Methods

private IEnumerable<ChartPoint> GetGarbagePoints(ISeriesView view)
{
var tracker = GetTracker(view);

return tracker.Indexed.Values.Where(x => IsGarbage(x, tracker)).Concat(
tracker.Referenced.Values.Where(x => IsGarbage(x, tracker)));
}
#region Privates

private IPointEvaluator<T> GetConfig(ISeriesView view)
{
Expand All @@ -248,6 +245,50 @@ private IPointEvaluator<T> GetConfig(ISeriesView view)
ChartCore.Configurations.GetConfig<T>(view.Model.SeriesOrientation) as IPointEvaluator<T>);
}

private ChartPoint GetChartPoint(bool isClass, PointTracker tracker, int index, T value)
{
ChartPoint cp;

if (!isClass)
{
if (!tracker.Indexed.TryGetValue(index, out cp))
{
cp = new ChartPoint
{
Instance = value,
Key = index
};
tracker.Indexed[index] = cp;
}
}
else
{
if (!tracker.Referenced.TryGetValue(value, out cp))
{
cp = new ChartPoint
{
Instance = value,
Key = index
};
tracker.Referenced[value] = cp;
}
}
return cp;
}

private void ObservableOnPointChanged()
{
Trackers.Keys.ForEach(x => x.Model.Chart.Updater.Run());
}

private IEnumerable<ChartPoint> GetGarbagePoints(ISeriesView view)
{
var tracker = GetTracker(view);

return tracker.Indexed.Values.Where(x => IsGarbage(x, tracker)).Concat(
tracker.Referenced.Values.Where(x => IsGarbage(x, tracker)));
}

private void ValidateGarbageCollector(ISeriesView view)
{
var tracker = GetTracker(view);
Expand All @@ -259,12 +300,7 @@ private void ValidateGarbageCollector(ISeriesView view)
foreach (var point in tracker.Indexed.Values.Concat(tracker.Referenced.Values))
point.Gci = 0;
}

private void ObservableOnPointChanged()
{
Trackers.Keys.ForEach(x => x.Model.Chart.Updater.Run());
}


private bool IsGarbage(ChartPoint point, PointTracker tracker)
{
return point.Gci < tracker.Gci
Expand Down
5 changes: 2 additions & 3 deletions Core/Charts/CartesianChartCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ public class CartesianChartCore : ChartCore
{
#region Constructors

public CartesianChartCore(IChartView view, IChartUpdater updater) : base(view, updater)
public CartesianChartCore(IChartView view, ChartUpdater updater) : base(view, updater)
{
updater.Chart = this;
}

#endregion


#region Publics

public override void PrepareAxes()
Expand Down Expand Up @@ -168,7 +167,7 @@ private void PrepareUnitWidth()
foreach (var series in View.ActualSeries)
{
if (series is IStackedColumnSeriesView || series is IColumnSeriesView ||
series is IOhlcSeriesView || series is IHeatSeriesView)
series is IFinancialSeriesView || series is IHeatSeriesView)
{
AxisX[series.ScalesXAt].EvaluatesUnitWidth = true;
}
Expand Down
4 changes: 2 additions & 2 deletions Core/Charts/ChartCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class ChartCore
{

#region Constructors
protected ChartCore(IChartView view, IChartUpdater updater)
protected ChartCore(IChartView view, ChartUpdater updater)
{
View = view;
Updater = updater;
Expand All @@ -56,7 +56,7 @@ static ChartCore()
public static Charting Configurations { get; set; }
public bool SeriesInitialized { get; set; }
public IChartView View { get; set; }
public IChartUpdater Updater { get; set; }
public ChartUpdater Updater { get; set; }
public CoreSize ControlSize { get; set; }
public CoreRectangle DrawMargin { get; set; }
public bool HasUnitaryPoints { get; set; }
Expand Down
3 changes: 1 addition & 2 deletions Core/Charts/PieChartCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ namespace LiveCharts.Charts
public class PieChartCore : ChartCore
{
#region Constructors

public PieChartCore(IChartView view, IChartUpdater updater) : base(view, updater)
public PieChartCore(IChartView view, ChartUpdater updater) : base(view, updater)
{
updater.Chart = this;
}
Expand Down
26 changes: 7 additions & 19 deletions Core/Configurations/CartesianMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
//SOFTWARE.

using System;
using System.Collections.Generic;
using LiveCharts.Dtos;

namespace LiveCharts.Configurations
{
Expand All @@ -40,25 +38,15 @@ public class CartesianMapper<T> : IPointEvaluator<T>
/// <summary>
/// Sets values for a specific point
/// </summary>
/// <param name="valuePair">Key and value</param>
/// <param name="point">Point to set</param>
public void SetAll(KeyValuePair<int, T> valuePair, ChartPoint point)
/// <param name="value"></param>
/// <param name="key"></param>
public void Evaluate(int key, T value, ChartPoint point)
{
point.X = _x(valuePair.Value, valuePair.Key);
point.Y = _y(valuePair.Value, valuePair.Key);
if (_stroke != null) point.Stroke = _stroke(valuePair.Value, valuePair.Key);
if (_fill != null) point.Fill = _fill(valuePair.Value, valuePair.Key);
}

/// <summary>
/// Evaluates a point with a given value and key
/// </summary>
/// <param name="valuePair">Value and Key</param>
/// <returns>evaluated point</returns>
public Xyw[] GetEvaluation(KeyValuePair<int, T> valuePair)
{
var xyw = new Xyw(_x(valuePair.Value, valuePair.Key), _y(valuePair.Value, valuePair.Key), 0);
return new [] {xyw, xyw};
point.X = _x(value, key);
point.Y = _y(value, key);
if (_stroke != null) point.Stroke = _stroke(value, key);
if (_fill != null) point.Fill = _fill(value, key);
}

/// <summary>
Expand Down
34 changes: 9 additions & 25 deletions Core/Configurations/FinancialMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
//SOFTWARE.

using System;
using System.Collections.Generic;
using LiveCharts.Dtos;

namespace LiveCharts.Configurations
{
Expand All @@ -42,31 +40,17 @@ public class FinancialMapper<T> : IPointEvaluator<T>
/// <summary>
/// Sets values for a specific point
/// </summary>
/// <param name="valuePair">Key and value</param>
/// <param name="point">Point to set</param>
public void SetAll(KeyValuePair<int, T> valuePair, ChartPoint point)
/// <param name="value"></param>
/// <param name="key"></param>
public void Evaluate(int key, T value, ChartPoint point)
{
point.X = _x(valuePair.Value, valuePair.Key);
point.Y = _y(valuePair.Value, valuePair.Key);
point.Open = _open(valuePair.Value, valuePair.Key);
point.High = _high(valuePair.Value, valuePair.Key);
point.Close = _close(valuePair.Value, valuePair.Key);
point.Low = _low(valuePair.Value, valuePair.Key);
}

/// <summary>
/// Evaluates a point with a given value and key
/// </summary>
/// <param name="valuePair">Value and Key</param>
/// <returns>point evaluation</returns>
public Xyw[] GetEvaluation(KeyValuePair<int, T> valuePair)
{
var x = _x(valuePair.Value, valuePair.Key);
return new[]
{
new Xyw(x, _low(valuePair.Value, valuePair.Key), 0),
new Xyw(x, _high(valuePair.Value, valuePair.Key), 0)
};
point.X = _x(value, key);
point.Y = _y(value, key);
point.Open = _open(value, key);
point.High = _high(value, key);
point.Close = _close(value, key);
point.Low = _low(value, key);
}

/// <summary>
Expand Down
Loading

0 comments on commit f330039

Please sign in to comment.