Skip to content

Commit 3eddd6c

Browse files
Label cleanup, and option to disable zoom
EnableZoom property, XAxisMaxLabels allows to not draw all labels when over dense data points Zoom on Y Axis ticks and X Axis labels
1 parent f9d9e6a commit 3eddd6c

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

Sources/Microcharts.Forms/ChartView.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ public ChartView()
1717
{
1818
this.BackgroundColor = Color.Transparent;
1919
this.PaintSurface += OnPaintCanvas;
20-
21-
var pinchGesture = new PinchGestureRecognizer();
22-
pinchGesture.PinchUpdated += OnPinchUpdated;
23-
GestureRecognizers.Add(pinchGesture);
2420
}
2521

2622
public event EventHandler<SKPaintSurfaceEventArgs> ChartPainted;
@@ -68,6 +64,15 @@ private static void OnChartChanged(BindableObject d, object oldValue, object val
6864

6965
if (view.chart != null)
7066
{
67+
AxisBasedChart axisChart = view.chart as AxisBasedChart;
68+
if( axisChart != null && axisChart.EnableZoom )
69+
{
70+
//FIXME: how to handle disable zoom after already enabled
71+
var pinchGesture = new PinchGestureRecognizer();
72+
pinchGesture.PinchUpdated += view.OnPinchUpdated;
73+
view.GestureRecognizers.Add(pinchGesture);
74+
}
75+
7176
view.handler = view.chart.ObserveInvalidate(view, (v) => v.InvalidateSurface());
7277
}
7378
}

Sources/Microcharts.Samples.Forms/Microcharts.Samples.Forms/ChartPage.xaml.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,17 @@ protected void GenerateDynamicData()
5555
foreach (var curSeries in series)
5656
{
5757
var entries = curSeries.Entries.ToList();
58-
bool addLabel = (entries.Count % 1000) == 0;
5958

6059
if (s == curSeries)
6160
{
6261
var entry = Data.GenerateTimeSeriesEntry(r, idx, 1);
63-
if (!addLabel) entry.First().Label = null;
64-
6562
entries.AddRange(entry);
6663

6764
if (entries.Count() > count * 1.5) entries.RemoveAt(0);
6865
}
6966
else
7067
{
7168
var entry = new ChartEntry(null) { ValueLabel = null, Label = label };
72-
if (!addLabel) entry.Label = null;
73-
7469
entries.Add(entry);
7570
if (entries.Count() > count * 1.5) entries.RemoveAt(0);
7671
}

Sources/Microcharts.Samples/Data.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,7 @@ private static IEnumerable<ExampleChartItem> GenerateLineSeriesChartExample()
13401340
LabelOrientation = Orientation.Vertical,
13411341
ValueLabelOrientation = Orientation.Vertical,
13421342
LabelTextSize = 14,
1343+
EnableZoom = true,
13431344
LineMode = LineMode.Straight,
13441345
PointMode = PointMode.None,
13451346
LineAreaAlpha = 0,
@@ -1352,6 +1353,7 @@ private static IEnumerable<ExampleChartItem> GenerateLineSeriesChartExample()
13521353
MaxValue = 150,
13531354
MinValue = -150,
13541355
YAxisPosition = Position.Left,
1356+
XAxisMaxLabels = 20,
13551357
LegendOption = SeriesLegendOption.Bottom,
13561358

13571359
Series = new List<ChartSerie>()
@@ -1512,7 +1514,7 @@ public static IEnumerable<ChartEntry> GenerateTimeSeriesEntry( Random r, int idx
15121514
do
15131515
{
15141516
if (withNulls && (value.Value % 10) == 0) value = null;
1515-
entries.Add(new ChartEntry(value) { ValueLabel = value.ToString(), Label = count % 1000 == 0 ? label.ToString("mm:ss") : null });
1517+
entries.Add(new ChartEntry(value) { ValueLabel = value.ToString(), Label =label.ToString("mm:ss") });
15161518
valOffset = ((label - baseTime).TotalSeconds + phase) * valScale;
15171519
ampOffset = ((label - baseTime).TotalSeconds + phase) * ampScale;
15181520
value = valueShift + (float)(Math.Sin(valOffset) * (Math.Cos(ampOffset) * amp));

Sources/Microcharts/Charts/AxisBasedChart.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public float SerieLabelTextSize
130130
set => Set(ref serieLabelTextSize, value);
131131
}
132132

133+
/// <summary>
134+
/// Determines whether pinch to zoom will work on the chart
135+
/// </summary>
136+
public bool EnableZoom { get; set; } = false;
137+
138+
public ChartXForm XForm { get; } = new ChartXForm();
139+
133140
/// <summary>
134141
/// Show Y Axis Text?
135142
/// </summary>
@@ -162,7 +169,10 @@ public float SerieLabelTextSize
162169
public SKPaint YAxisLinesPaint { get; set; }
163170

164171

165-
public ChartXForm XForm { get; } = new ChartXForm();
172+
/// <summary>
173+
/// How many labels to draw, -1 means all of them
174+
/// </summary>
175+
public int XAxisMaxLabels { get; set; } = -1;
166176

167177
#endregion
168178

@@ -188,7 +198,8 @@ public override void DrawContent(SKCanvas canvas, int width, int height)
188198
float minValue = InternalMinValue.HasValue ? InternalMinValue.Value : MinValue;
189199

190200
//This function might change the min/max value
191-
var yAxisSize = MeasureHelper.CalculateYAxis(ShowYAxisText, ShowYAxisLines, entries, YAxisMaxTicks, YAxisTextPaint, YAxisPosition, width, fixedRange, ref maxValue, ref minValue, out float yAxisXShift, out List<float> yAxisIntervalLabels);
201+
int yMaxTicks = (int)(YAxisMaxTicks * XForm.Scale);
202+
var yAxisSize = MeasureHelper.CalculateYAxis(ShowYAxisText, ShowYAxisLines, entries, yMaxTicks, YAxisTextPaint, YAxisPosition, width, fixedRange, ref maxValue, ref minValue, out float yAxisXShift, out List<float> yAxisIntervalLabels);
192203
width = (int)yAxisSize.Width;
193204

194205
float valRange = maxValue - minValue;
@@ -238,7 +249,7 @@ public override void DrawContent(SKCanvas canvas, int width, int height)
238249
*/
239250

240251
canvas.Save();
241-
canvas.ClipRect(yAxisRect);
252+
if(EnableZoom) canvas.ClipRect(yAxisRect);
242253
DrawHelper.DrawYAxis(ShowYAxisText, ShowYAxisLines, YAxisPosition, YAxisTextPaint, YAxisLinesPaint, XForm.Offset, XForm.Scale, Margin, AnimationProgress, maxValue, valRange, canvas, width, yAxisXShift, yAxisIntervalLabels, headerHeight, itemSize, origin);
243254
canvas.Restore();
244255

@@ -252,9 +263,11 @@ public override void DrawContent(SKCanvas canvas, int width, int height)
252263
int entryCount = entries.Count();
253264

254265
canvas.Save();
255-
canvas.ClipRect(labelRect);
266+
if (EnableZoom) canvas.ClipRect(labelRect);
256267

257-
for (int i = 0; i < labels.Length; i++)
268+
int xMaxLabels = (int)(XAxisMaxLabels * XForm.Scale);
269+
int xlabelSkip = XAxisMaxLabels > 0 ? labels.Length / xMaxLabels : 1;
270+
for (int i = 0; i < labels.Length; i+= xlabelSkip)
258271
{
259272
var itemX = Margin + (itemSize.Width / 2) + (i * (itemSize.Width + Margin)) + yAxisXShift;
260273
float labelX = XForm.Offset.X + (itemX * XForm.Scale);
@@ -269,9 +282,13 @@ public override void DrawContent(SKCanvas canvas, int width, int height)
269282
canvas.Restore();
270283

271284
canvas.Save();
272-
canvas.ClipRect(chartRect);
273-
canvas.Translate(XForm.Offset);
274-
canvas.Scale(XForm.Scale);
285+
if (EnableZoom)
286+
{
287+
canvas.ClipRect(chartRect);
288+
canvas.Translate(XForm.Offset);
289+
canvas.Scale(XForm.Scale);
290+
}
291+
275292
for (int i = 0; i < labels.Length; i++)
276293
{
277294
if (i >= entryCount) break;
@@ -304,9 +321,12 @@ public override void DrawContent(SKCanvas canvas, int width, int height)
304321
DrawLegend(canvas, seriesSizes, legendHeight, height, width);
305322

306323
canvas.Save();
307-
canvas.ClipRect(chartRect);
308-
canvas.Translate(XForm.Offset);
309-
canvas.Scale(XForm.Scale);
324+
if (EnableZoom)
325+
{
326+
canvas.ClipRect(chartRect);
327+
canvas.Translate(XForm.Offset);
328+
canvas.Scale(XForm.Scale);
329+
}
310330
OnDrawContentEnd(canvas, itemSize, origin, valueLabelSizes);
311331
canvas.Restore();
312332
}

0 commit comments

Comments
 (0)