Skip to content

Commit 515e4a0

Browse files
authored
Fix BarSeries hit test with invalid items (#2038)
1 parent 85dd07d commit 515e4a0

5 files changed

Lines changed: 50 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## Unreleased
55

66
### Added
7+
- Example to demonstrate BarSeries HitTest bug (#2038)
78
- Example to Show/Hide Legend (#1470)
89
- Example of BarSeries stacked and with labels (#1979)
910
- Example of issue with AreaSeries tracker (#1982)
@@ -29,6 +30,7 @@ All notable changes to this project will be documented in this file.
2930
- Unused LabelColor property from TornadoBarSeries, IntervalBarSeries, and RectangleBarSeries (#2030)
3031

3132
### Fixed
33+
- fixed issue with BarSeries, when the HitTest returns the wrong BarItem, when there are invalid items in the list. (#2038)
3234
- Placement of BarSeries labels when stacked (#1979)
3335
- SystemInvalidException in LineSeries when only Double.Nan values are added (#1991)
3436
- Issue with tracking AreaSeries with monotonic data points (#1982)

‎CONTRIBUTORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ csabar <rumancsabi@gmail.com>
3434
Curt Mullin <curt.mullin@gmail.com>
3535
Cyril Martin <cyril.martin.cm@gmail.com>
3636
Dan Aizenstros
37+
Daniel Gross <dgross@intronik.de>
3738
danpaul88 <danpaul88@users.noreply.github.com>
3839
darrelbrown
3940
David Funk <funk.david1985@gmail.com>

‎Source/Examples/ExampleLibrary/Issues/Issues.cs‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,7 @@ public static PlotModel Smooth()
17621762
var model = new PlotModel { Title = "LineSeries with Smooth = true (zoomed in)" };
17631763
var l = new Legend
17641764
{
1765-
LegendSymbolLength = 24
1765+
LegendSymbolLength = 24
17661766
};
17671767

17681768
model.Legends.Add(l);
@@ -2290,9 +2290,9 @@ public static PlotModel EmojiText()
22902290
public static PlotModel FindWindowsStartIndex()
22912291
{
22922292
var plotModel1 = new PlotModel { Title = "AreaSeries broken in time" };
2293-
var axis = new LinearAxis {Position = AxisPosition.Left, MinimumPadding = 0, MaximumPadding = 0.06, AbsoluteMinimum = 0};
2294-
var xAxis = new LinearAxis() {Position = AxisPosition.Bottom, Minimum = 4};
2295-
2293+
var axis = new LinearAxis { Position = AxisPosition.Left, MinimumPadding = 0, MaximumPadding = 0.06, AbsoluteMinimum = 0 };
2294+
var xAxis = new LinearAxis() { Position = AxisPosition.Bottom, Minimum = 4 };
2295+
22962296
plotModel1.Axes.Add(axis);
22972297
plotModel1.Axes.Add(xAxis);
22982298

@@ -2307,14 +2307,14 @@ public static PlotModel FindWindowsStartIndex()
23072307
areaSeries.Points.Add(DataPoint.Undefined);
23082308
areaSeries.Points2.Add(DataPoint.Undefined);
23092309
}
2310-
2310+
23112311
currentValue += random.NextDouble();
23122312
areaSeries.Points.Add(new DataPoint(currentValue, currentValue));
23132313
areaSeries.Points2.Add(new DataPoint(currentValue, currentValue));
23142314
}
2315-
2315+
23162316
plotModel1.Series.Add(areaSeries);
2317-
2317+
23182318

23192319
return plotModel1;
23202320
}
@@ -2567,6 +2567,38 @@ public DataPoint GetDataPoint()
25672567
}
25682568
}
25692569

2570+
[Example("#2038 BarSeries: HitTest does not deliver the correct BarItem (Closed)")]
2571+
public static PlotModel BarSeriesHitTestBug2038()
2572+
{
2573+
var model = new PlotModel
2574+
{
2575+
Title = "#2038 HitTest does not deliver the correct BarItem, when there are invalid items in Data",
2576+
Subtitle = "Click on top item, the top item, should change color!"
2577+
};
2578+
model.MouseDown += (s, e) =>
2579+
{
2580+
if (e.ChangedButton == OxyMouseButton.Left)
2581+
{
2582+
foreach (var hit in model.HitTest(new HitTestArguments(e.Position, 0)))
2583+
{
2584+
if (hit.Item is BarItem barItem)
2585+
{
2586+
barItem.Color = OxyColors.Red;
2587+
model.InvalidatePlot(false);
2588+
}
2589+
}
2590+
}
2591+
};
2592+
var s1 = new BarSeries { Title = "Series 1", IsStacked = true, StrokeColor = OxyColors.Black, StrokeThickness = 1, StackGroup = "3" };
2593+
int i = 0;
2594+
s1.Items.Add(new BarItem { Value = 25, CategoryIndex = i++ });
2595+
s1.Items.Add(new BarItem { Value = double.NaN, CategoryIndex = i++ });
2596+
s1.Items.Add(new BarItem { Value = 18, CategoryIndex = i++ });
2597+
s1.Items.Add(new BarItem { Value = 40, CategoryIndex = i++ });
2598+
model.Series.Add(s1);
2599+
return model;
2600+
}
2601+
25702602
/* NEW ISSUE TEMPLATE
25712603
[Example("#123: Issue Description")]
25722604
public static PlotModel IssueDescription()

‎Source/Examples/ExampleLibrary/Series/BarSeriesExamples.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static PlotModel WithLabels()
5252
categoryAxis.Labels.Add("Category C");
5353
categoryAxis.Labels.Add("Category D");
5454
var valueAxis = new LinearAxis { Position = AxisPosition.Bottom, MinimumPadding = 0.06, MaximumPadding = 0.06, ExtraGridlines = new[] { 0d } };
55-
55+
5656
foreach (var s in series)
5757
{
5858
model.Series.Add(s);

‎Source/OxyPlot/Series/BarSeries/BarSeriesBase.cs‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,14 @@ protected void UpdateValidData()
331331
this.ValidItems.Clear();
332332
this.ValidItemsIndexInversion.Clear();
333333
var numberOfCategories = this.Manager.Categories.Count;
334-
var valueAxis = this.XAxis;
335-
336-
var i = 0;
337-
var items = this.ActualItems
338-
.Where(item => item.GetCategoryIndex(i) < numberOfCategories)
339-
.Where(this.IsValid);
340-
341-
foreach (var item in items)
334+
for (int index = 0; index < this.ActualItems.Count; index++)
342335
{
343-
this.ValidItemsIndexInversion.Add(this.ValidItems.Count, i);
344-
this.ValidItems.Add(item);
345-
i++;
336+
var item = this.ActualItems[index];
337+
if (item != null && item.GetCategoryIndex(index) < numberOfCategories && this.IsValid(item))
338+
{
339+
this.ValidItemsIndexInversion.Add(this.ValidItems.Count, index);
340+
this.ValidItems.Add(item);
341+
}
346342
}
347343
}
348344
}

0 commit comments

Comments
 (0)