Chart of the Week: Creating a .NET MAUI Bubble Chart to Visualize Healthcare Spending and Life Expectancy Data
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Chart of the Week Creating a .NET MAUI Bubble Chart to Visualize Healthcare Spending and Life Expectancy Data

Chart of the Week: Creating a .NET MAUI Bubble Chart to Visualize Healthcare Spending and Life Expectancy Data

Welcome to our Chart of the Week blog series!

Today, we delve into healthcare expenditure and life expectancy data, two pivotal indicators of a nation’s well-being. A country’s healthcare spending significantly influences its citizens’ health and lifespans. We’ll plot this correlation using a bubble chart.

Like a scatter plot, a bubble chart is an effective tool for visualizing the relationship between three numeric variables. Each data point is represented by a bubble. The x- and y-values determine the bubbles’ positions, while their size represents the z-value. This allows for easy comparison and a comprehensive view of the data. The chart, composed of closely packed circles proportional to the quantities they represent, uses value axes instead of a category axis, enhancing its effectiveness for data analysis.

In this blog, we’ll use the Syncfusion .NET MAUI Bubble Chart to visually present the relationship between healthcare expenditure and life expectancy in various countries. This will shed light on diverse healthcare scenarios around the globe.Visualizing Healthcare Spending and Life Expectancy by countries using .NET MAUI Bubble Chart

Let’s get started!

Step 1: Gathering the data

Before proceeding, we need to gather the data. We’ll get the data on Life Expectancy and Healthcare Expenditure for various countries in 2019 from Our World in Data.

Step 2: Preparing the data for the chart

Create the LifeExpectancyModel class with Country, LifeExpectancy, HealthExpenditure, Population, and Continent properties to store the life expectancy and health expenditure data in different countries.

Refer to the following code example.

public class LifeExpectancyModel
{
    public string Country { get; set; }
    public double LifeExpectancy { get; set; }
    public double HealthExpenditure { get; set; }
    public double Population { get; set; }
    public string Continent { get; set; }
}

Then, store the data by generating the life expectancy data collection using the LifeExpectancyData class and AllCountriesData properties. Assign the CSV data to the life expectancy data collection using the ReadCSV method and store it in the AllCountriesData property.

Additionally, expose the AsianCountries, AfricanCountries, SouthAmerica, NorthAmerica, Europe, and Oceania properties to store the countries’ data respective to their continents.

Refer to the following code example.

public class LifeExpectancyData
{

    List<LifeExpectancyModel> allCountriesdata;

    public List<LifeExpectancyModel> AllCountriesData
    {
        get
        {
            return allCountriesdata;
        }
        set
        {
            allCountriesdata = value;
        }
    }

    List<LifeExpectancyModel> asianCountries;
    public List<LifeExpectancyModel> AsianCountries
    {
        get
        {
            return asianCountries;
        }
        set
        {
            asianCountries = value;
        }
    }

    List<LifeExpectancyModel> africanCountries;
    public List<LifeExpectancyModel> AfricanCountries
    {
        get
        {
            return africanCountries;
        }
        set
        {
            africanCountries = value;
        }
    }

    List<LifeExpectancyModel> southAmerica;
    public List<LifeExpectancyModel> SouthAmerica
    {
        get
        {
            return southAmerica;
        }
        set
        {
            southAmerica = value;
        }
    }

    List<LifeExpectancyModel> northAmerica;
    public List<LifeExpectancyModel> NorthAmerica
    {
        get
        {
            return northAmerica;
        }
        set
        {
            northAmerica = value;
        }
    }

    List<LifeExpectancyModel> oceania;
    public List<LifeExpectancyModel> Oceania
    {
        get
        {
            return oceania;
        }
        set
        {
            oceania = value;
        }
    }

    List<LifeExpectancyModel> europe;
    public List<LifeExpectancyModel> Europe
    {
        get
        {
            return europe;
        }
        set
        {
            europe = value;
        }
    }

    public LifeExpectancyData()
    {
        AllCountriesData = new List<LifeExpectancyModel>(ReadCSV());
        AsianCountries = AllCountriesData.Where(d => d.Continent == "Asia").ToList();
        AfricanCountries = AllCountriesData.Where(d => d.Continent == "Africa").ToList();
        Europe = AllCountriesData.Where(d => d.Continent == "Europe").ToList();
        SouthAmerica = AllCountriesData.Where(d => d.Continent == "South America").ToList();
        NorthAmerica = AllCountriesData.Where(d => d.Continent == "North America").ToList();
        Oceania = AllCountriesData.Where(d => d.Continent == "Oceania").ToList();
    }
    public static IEnumerable<LifeExpectancyModel> ReadCSV()
    {
        Assembly executingAssembly = typeof(App).GetTypeInfo().Assembly;
        Stream inputStream = executingAssembly.GetManifestResourceStream("LifeExpectancy.Resources.Raw.data.csv");

        string line;
        List<string> lines = new();

        using StreamReader reader = new(inputStream);
        while((line = reader.ReadLine())!= null)
        {
            lines.Add(line);
        }

        return lines.Select(line =>
        {
            string[] data = line.Split(',');
            return new LifeExpectancyModel(data[0], Convert.ToDouble(data[1]), Convert.ToDouble(data[2]), Convert.ToDouble(data[3]), data[4]);
        });
     }
}

Step 3: Configuring the Syncfusion .NET MAUI Cartesian Charts control

Now, configure the Syncfusion .NET MAUI Cartesian Charts control using this documentation.

Refer to the following code example.

<chart:SfCartesianChart>                            
 <chart:SfCartesianChart.XAxes>
  <chart:LogarithmicAxis/>
 </chart:SfCartesianChart.XAxes>
 <chart:SfCartesianChart.YAxes>
  <chart:NumericalAxis/>
 </chart:SfCartesianChart.YAxes>                       
</chart:SfCartesianChart>

Step 4: Bind the data to the chart

To visualize the relation between healthcare spending and life expectancy data, we’ll use the bubble series.

Refer to the following code example.

<chart:BubbleSeries ItemsSource="{Binding AsianCountries}"
                    XBindingPath="HealthExpenditure"
                    YBindingPath="LifeExpectancy"
                    SizeValuePath="Population"/>

<chart:BubbleSeries ItemsSource="{Binding AfricanCountries}"
                    XBindingPath="HealthExpenditure"
                    YBindingPath="LifeExpectancy"
                    SizeValuePath="Population"/>

<chart:BubbleSeries ItemsSource="{Binding Europe}"
                    XBindingPath="HealthExpenditure"
                    YBindingPath="LifeExpectancy"
                    SizeValuePath="Population"/>

<chart:BubbleSeries ItemsSource="{Binding SouthAmerica}"
                    XBindingPath="HealthExpenditure"
                    YBindingPath="LifeExpectancy"
                    SizeValuePath="Population"/>

<chart:BubbleSeries ItemsSource="{Binding NorthAmerica}"
                    XBindingPath="HealthExpenditure"
                    YBindingPath="LifeExpectancy"
                    SizeValuePath="Population"/>

<chart:BubbleSeries ItemsSource="{Binding Oceania}"
                    XBindingPath="HealthExpenditure"
                    YBindingPath="LifeExpectancy"
                    SizeValuePath="Population"/>

In this example, we bound the bubble chart with the AsianCountries, AfricanCountries, NorthAmerica, SouthAmerica, Europe, and Oceania data collections with the ItemsSource property, which contains the information on life expectancy, health expenditure, and population for different countries. Additionally, we’ve specified the XBindingPath, YBindingPath, and SizeValuePath with the HealthExpenditure, LifeExpectancy, and Population properties, respectively.

The SizeValuePath is used to indicate the quantitative value associated with each data point. This quantitative value could be anything relevant to the dataset, such as population size, revenue, or sales volume. Here, the size of the bubble will correspond to the country’s population, allowing us to compare healthcare and life expectancy with the additional context of the scale of the healthcare system.

Step 5: Customizing the chart appearance

Now, we’ll customize the Syncfusion .NET MAUI Bubble Chart appearance to enhance the readability of the chart data.

Customizing the chart title

Let’s improve the readability of the plotted data by including a title with a description in the chart.

Refer to the following code example.

<chart:SfCartesianChart.Title>
 <Grid>
  <Grid.ColumnDefinitions>
   <ColumnDefinition Width="1*"/>
   <ColumnDefinition Width="59*"/>
  </Grid.ColumnDefinitions>
  <VerticalStackLayout Background="#FF855FF2" Margin="10,35,0,15" Grid.Column="0" Grid.RowSpan="2"/>
  <VerticalStackLayout Grid.Column="1" Margin="5">
   <Label Text="Healthcare Expenditure vs. Life Expectancy by Country in 2019" FontSize="Subtitle" FontFamily="centurygothic" Padding="0,30,5,5" HorizontalTextAlignment="Start"/>
   <Label Text="This graph depicts Healthcare Expenditure in dollars, which accounts for price differences between countries, as well as Life Expectancy at Birth." HorizontalTextAlignment="Start" FontSize="10" FontFamily="centurygothic" TextColor="Grey" Padding="0,0,0,10"/>
  </VerticalStackLayout>
 </Grid>
            
</chart:SfCartesianChart.Title>

Customizing the legend

Customize the chart legend by adding a Label for each chart series and providing the corresponding label text. Then, use the ToggleSeriesVisibility property to show or hide the continents’ countries in the chart.

Refer to the following code example.

<chart:SfCartesianChart.Legend>
 <chart:ChartLegend ToggleSeriesVisibility="True"/>
</chart:SfCartesianChart.Legend>

<chart:BubbleSeries Label="Asia"/>
<chart:BubbleSeries Label="Africa"/>
<chart:BubbleSeries Label="Europe"/>
<chart:BubbleSeries Label="South America"/>
<chart:BubbleSeries Label="North America"/>
<chart:BubbleSeries Label="Oceania"/>

Customizing the chart axis

Next, customize the x- and y-axes with the Minimum, Maximum, EdgeLabelsDrawingMode, and ShowMajorGridLines properties, as well as the Title, LabelStyle, LineStyle, and TickStyle.

Refer to the following code example.

<chart:SfCartesianChart.XAxes>
 <chart:LogarithmicAxis Minimum="30" Maximum="10000" EdgeLabelsDrawingMode="Shift" ShowMajorGridLines="False">
  <chart:LogarithmicAxis.Title>
   <chart:ChartAxisTitle Text="Healthcare Expenditure per capita(in current international $)" FontSize="16"/>
  </chart:LogarithmicAxis.Title>
  <chart:LogarithmicAxis.LabelStyle>
   <chart:ChartAxisLabelStyle LabelFormat="$0"/>
  </chart:LogarithmicAxis.LabelStyle>
  <chart:LogarithmicAxis.AxisLineStyle>
   <chart:ChartLineStyle StrokeWidth="0"/>
  </chart:LogarithmicAxis.AxisLineStyle>
  <chart:LogarithmicAxis.MajorTickStyle>
   <chart:ChartAxisTickStyle Stroke="#e6e6e6"/>
  </chart:LogarithmicAxis.MajorTickStyle>
 </chart:LogarithmicAxis>
</chart:SfCartesianChart.XAxes>

<chart:SfCartesianChart.YAxes>
 <chart:NumericalAxis EdgeLabelsDrawingMode="Center" Maximum="86" Minimum="50">
  <chart:NumericalAxis.Title>
   <chart:ChartAxisTitle Text="Life Expectancy at birth(Year)" FontSize="16"/>
  </chart:NumericalAxis.Title>
  <chart:NumericalAxis.AxisLineStyle>
   <chart:ChartLineStyle StrokeWidth="0"/>
  </chart:NumericalAxis.AxisLineStyle>
  <chart:NumericalAxis.MajorTickStyle>
   <chart:ChartAxisTickStyle Stroke="#e6e6e6"/>
  </chart:NumericalAxis.MajorTickStyle>
 </chart:NumericalAxis>
</chart:SfCartesianChart.YAxes>

Adding Interactivity to the chart

We can enhance the readability of the data series by enabling tooltips with the TooltipTemplate property. This will allow us to show the details of each country.

Refer to the following code example.

<chart:SfCartesianChart.Resources>
 <ResourceDictionary>
  <model:PopulationValueConver x:Key="populationValueConverter"/>
  <DataTemplate x:Key="template">
   <StackLayout>
    <Label Text="{Binding Item.Country}" HorizontalTextAlignment="Center" HorizontalOptions="Center" VerticalTextAlignment="Center" TextColor="White" FontAttributes="Bold" FontFamily="Helvetica" Margin="0,2,0,2" FontSize="12" Grid.Row="0" Padding="0,1"/>
    <BoxView Color="Gray" HeightRequest="1" HorizontalOptions="Fill" Margin="2,0,2,0"/>
    <Grid Padding="3">
     <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition Height="*"/>
      <RowDefinition Height="*"/>
     </Grid.RowDefinitions>
     <Label Grid.Row="0" Text="{Binding Item.HealthExpenditure,StringFormat='Health Expenditure  : ${0}'}" TextColor="white" FontFamily="Helvetica" FontSize="12"/>
     <Label Grid.Row="1" Text="{Binding Item.LifeExpectancy,StringFormat='Life Expectancy        : {0} years'}" TextColor="white" FontFamily="Helvetica" FontSize="12"/>
     <Label Grid.Row="2" Text="{Binding Item.Population,Converter={StaticResource populationValueConverter},StringFormat='Population: {0}'}" TextColor="white" FontFamily="Helvetica" FontSize="12"/>
    </Grid>
   </StackLayout>
  </DataTemplate>
 </ResourceDictionary>
</chart:SfCartesianChart.Resources>

<chart:BubbleSeries EnableTooltip="True" TooltipTemplate="{StaticResource template}"/>
<chart:BubbleSeries EnableTooltip="True" TooltipTemplate="{StaticResource template}"/>
<chart:BubbleSeries EnableTooltip="True" TooltipTemplate="{StaticResource template}"/>
<chart:BubbleSeries EnableTooltip="True" TooltipTemplate="{StaticResource template}"/>
<chart:BubbleSeries EnableTooltip="True" TooltipTemplate="{StaticResource template}"/>
<chart:BubbleSeries EnableTooltip="True" TooltipTemplate="{StaticResource template}"/>

After executing these code examples, the output we get will look like the following GIF image.

Visualizing Healthcare Spending and Life Expectancy by Countries Using Syncfusion’s .NET MAUI Bubble Chart
Visualizing Healthcare Spending and Life Expectancy by Countries Using Syncfusion’s .NET MAUI Bubble Chart

GitHub reference

For more details, refer to the complete project on GitHub.

Conclusion

Thanks for reading! In this blog, we have seen how to visualize countries’ life expectancy and healthcare expenditure data using the Syncfusion .NET MAUI Bubble Chart. We hope you’ll follow the steps discussed in this blog and share your thoughts in the comments below.

For current Syncfusion customers, the newest version of Essential Studio is available from the License & Downloads page. If you are not a customer, try our 30-day free trial to check out the latest features.

You can contact us through our support forums, support portal, or feedback portal if you require assistance. We are always happy to help you!

See you in our next blog!

Test Flight
App Center Badge
Google Play Store Badge
Microsoft Badge
Github Store Badge

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed