microcharts-dotnet / Microcharts

Create cross-platform (Xamarin, Windows, ...) simple charts.
MIT License
2.03k stars 360 forks source link

[Question] 'An item with the same key has already been added. Key: Microcharts.ChartEntry' #311

Closed twofingerrightclick closed 2 years ago

twofingerrightclick commented 2 years ago

I bumped to the 1.0.0-preview1 of Microcharts.Forms and now get this runtime error: 'An item with the same key has already been added. Key: Microcharts.ChartEntry' when adding entries to a LineChart.

What prop is being used as the "Key" in the chart entries? I will comment if I figure this out...

twofingerrightclick commented 2 years ago

The issue is that ChartEntry's are used as the Key in a dictionary when calculating the ValueLabels height at AxisBasedChart. MeasureValueLabels. ChartEntry doesn't override GetHashCode or Equals and so the respective Object methods get used and so its based on reference equals, and I was filling the chart with a default entry: ChartEntry _DefaultChartEntry = new ChartEntry(0); and so that wont work no more.

Is the reference comparison desired elsewhere in 1.0.0-preview1?

TL:DR Use "new" when adding a chart entry i.e. Each ChartEntry needs to be a unique object.