Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/LiveChartsCore/CartesianChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public CartesianChart(

///<inheritdoc cref="Chart{TDrawingContext}.Series"/>
public override IEnumerable<IChartSeries<TDrawingContext>> Series =>
_chartView.Series.Cast<IChartSeries<TDrawingContext>>();
_chartView.Series?.Cast<IChartSeries<TDrawingContext>>() ?? [];

///<inheritdoc cref="Chart{TDrawingContext}.VisibleSeries"/>
public override IEnumerable<IChartSeries<TDrawingContext>> VisibleSeries =>
Expand Down Expand Up @@ -403,8 +403,20 @@ protected internal override void Measure()
var viewDrawMargin = _chartView.DrawMargin;
ControlSize = _chartView.ControlSize;

YAxes = _chartView.YAxes.Cast<ICartesianAxis<TDrawingContext>>().ToArray();
XAxes = _chartView.XAxes.Cast<ICartesianAxis<TDrawingContext>>().ToArray();
var x = _chartView.XAxes;
var y = _chartView.YAxes;

if (x is null || y is null)
{
// in theory nulls are not valid, see ChartTest.cs for more context.
var provider = LiveCharts.DefaultSettings.GetProvider<TDrawingContext>();

x = [provider.GetDefaultCartesianAxis()];
y = [provider.GetDefaultCartesianAxis()];
}

XAxes = x.Cast<ICartesianAxis<TDrawingContext>>().ToArray();
YAxes = y.Cast<ICartesianAxis<TDrawingContext>>().ToArray();

if (XAxes.Length == 0 || YAxes.Length == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/LiveChartsCore/PieChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class PieChart<TDrawingContext>(

///<inheritdoc cref="Chart{TDrawingContext}.Series"/>
public override IEnumerable<IChartSeries<TDrawingContext>> Series =>
view.Series.Cast<IChartSeries<TDrawingContext>>();
view.Series?.Cast<IChartSeries<TDrawingContext>>() ?? [];

///<inheritdoc cref="Chart{TDrawingContext}.VisibleSeries"/>
public override IEnumerable<IChartSeries<TDrawingContext>> VisibleSeries =>
Expand Down
23 changes: 20 additions & 3 deletions src/LiveChartsCore/PolarChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class PolarChart<TDrawingContext>(

///<inheritdoc cref="Chart{TDrawingContext}.Series"/>
public override IEnumerable<IChartSeries<TDrawingContext>> Series =>
view.Series.Cast<IChartSeries<TDrawingContext>>();
view.Series?.Cast<IChartSeries<TDrawingContext>>() ?? [];

///<inheritdoc cref="Chart{TDrawingContext}.VisibleSeries"/>
public override IEnumerable<IChartSeries<TDrawingContext>> VisibleSeries =>
Expand Down Expand Up @@ -157,8 +157,25 @@ protected internal override void Measure()
var viewDrawMargin = view.DrawMargin;
ControlSize = view.ControlSize;

AngleAxes = view.AngleAxes.Cast<IPolarAxis>().ToArray();
RadiusAxes = view.RadiusAxes.Cast<IPolarAxis>().ToArray();
var a = view.AngleAxes;
var r = view.RadiusAxes;

if (a is null || r is null)
{
// in theory nulls are not valid, see ChartTest.cs for more context.
var provider = LiveCharts.DefaultSettings.GetProvider<TDrawingContext>();

a = [provider.GetDefaultPolarAxis()];
r = [provider.GetDefaultPolarAxis()];
}

AngleAxes = a.Cast<IPolarAxis>().ToArray();
RadiusAxes = r.Cast<IPolarAxis>().ToArray();

if (AngleAxes.Length == 0 || RadiusAxes.Length == 0)
{
throw new Exception($"{nameof(AngleAxes)} and {nameof(RadiusAxes)} must contain at least one element.");
}

var theme = LiveCharts.DefaultSettings.GetTheme<TDrawingContext>();

Expand Down
96 changes: 96 additions & 0 deletions tests/LiveChartsCore.UnitTesting/ChartTests/ChartTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// The MIT License(MIT)
//
// Copyright(c) 2021 Alberto Rodriguez Orozco & LiveCharts Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Net.NetworkInformation;
using LiveChartsCore.SkiaSharpView.SKCharts;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LiveChartsCore.UnitTesting.ChartTests;

[TestClass]
public class ChartTests
{
// based on https://github.com/beto-rodriguez/LiveCharts2/issues/1422

// in theory LiveCharts properties should never be null, but we can't control the user input
// specially on this case where DataContext could be null

[TestMethod]
public void CartesianShouldHandleNullParams()
{
// we are testing the properties defined on LiveChartsCore/Kernel/Sketches/ICartesianChartView.cs

var chart = new SKCartesianChart
{
Width = 1000,
Height = 1000,
XAxes = null,
YAxes = null,
Sections = null,
Series = null,
DrawMarginFrame = null,
VisualElements = null
};

var image = chart.GetImage();

Assert.IsTrue(image is not null);
}

[TestMethod]
public void PieShouldHandleNullParams()
{
// we are testing the properties defined on LiveChartsCore/Kernel/Sketches/IPieChartView.cs

var chart = new SKPieChart
{
Width = 1000,
Height = 1000,
Series = null,
VisualElements = null
};

var image = chart.GetImage();

Assert.IsTrue(image is not null);
}

[TestMethod]
public void PolarShouldHandleNullParams()
{
// we are testing the properties defined on LiveChartsCore/Kernel/Sketches/IPolarChartView.cs

var chart = new SKPolarChart
{
Width = 1000,
Height = 1000,
Series = null,
AngleAxes = null,
RadiusAxes = null,
VisualElements = null
};

var image = chart.GetImage();

Assert.IsTrue(image is not null);
}
}