Skip to content

Commit

Permalink
Merge pull request #2 from SyncfusionExamples/WinUI-848397-Stacked-Ar…
Browse files Browse the repository at this point in the history
…ea-Chart

Updated the sample
  • Loading branch information
SyncfusionKarthikeyan authored Sep 27, 2023
2 parents a1af526 + 8fd6181 commit 687d058
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ We can display the chart area while zoomed in by enabling panning using the Enab

### Output

![StackedAreaChart_Output](https://github.com/SyncfusionExamples/Creating-a-Stacked-Area-Chart-for-U.S.-Primary-Energy-Consumption/assets/105482474/2af3e7da-97c4-4591-b214-af346a392ccd)
![Updated_Output](https://github.com/SyncfusionExamples/Creating-a-Stacked-Area-Chart-for-U.S.-Primary-Energy-Consumption/assets/105482474/9425c6a7-0765-4b43-8a97-6fc09231c9f5)
32 changes: 18 additions & 14 deletions US_Energy_Consumption/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
mc:Ignorable="d">

<Border CornerRadius="10" Margin="20" BorderThickness="1" BorderBrush="Black">
<chart:SfCartesianChart Name="chart" Margin="10" ManipulationCompleted="SfCartesianChart_ManipulationCompleted" ManipulationStarted="SfCartesianChart_ManipulationStarted" >
<chart:SfCartesianChart x:Name="chart" Margin="10" ManipulationCompleted="SfCartesianChart_ManipulationCompleted" ManipulationStarted="SfCartesianChart_ManipulationStarted" >
<chart:SfCartesianChart.Resources>
<local:ToolTipConverter x:Key="tooltip" />
<local:FillConverter x:Key="fillConverter" />
Expand Down Expand Up @@ -196,7 +196,7 @@
</chart:SfCartesianChart.YAxes>

<chart:SfCartesianChart.TooltipBehavior>
<chart:ChartTooltipBehavior Duration="10000" EnableAnimation="True"
<chart:ChartTooltipBehavior Duration="2000" EnableAnimation="True"
Style="{StaticResource style}"/>
</chart:SfCartesianChart.TooltipBehavior>

Expand All @@ -205,40 +205,44 @@
EnablePinchZooming="False" />
</chart:SfCartesianChart.ZoomPanBehavior>

<chart:StackedAreaSeries Fill="Black" Name="PetroleumCoke" ItemsSource="{Binding PetroleumCoke}"
<chart:SfCartesianChart.Legend>
<chart:ChartLegend/>
</chart:SfCartesianChart.Legend>

<chart:StackedAreaSeries Fill="#FFE55C" Name="PetroleumCoke" ItemsSource="{Binding CollectionOfData}"
TooltipTemplate="{StaticResource toolTipTemplate5}"
EnableTooltip="True" XBindingPath="Month"
EnableTooltip="True" XBindingPath="Month" Label="PetroleumCoke"
YBindingPath="PetroleumCoke">
</chart:StackedAreaSeries>


<chart:StackedAreaSeries Fill="#FFE55C" Name="PetroleumLiquid"
<chart:StackedAreaSeries Fill="#f54c4c" Name="PetroleumLiquid" Label="PetroleumLiquid"
TooltipTemplate="{StaticResource toolTipTemplate4}"
ItemsSource="{Binding PetroleumLiquid}" EnableTooltip="True"
ItemsSource="{Binding CollectionOfData}" EnableTooltip="True"
XBindingPath="Month" YBindingPath="PetroleumLiquid" >
</chart:StackedAreaSeries>

<chart:StackedAreaSeries Fill="#88CA5E" Name="Nuclear"
ItemsSource="{Binding Nuclear}" EnableTooltip="True"
<chart:StackedAreaSeries Fill="#88CA5E" Name="Nuclear" Label="Nuclear"
ItemsSource="{Binding CollectionOfData}" EnableTooltip="True"
TooltipTemplate="{StaticResource toolTipTemplate3}" XBindingPath="Month"
YBindingPath="Nuclear">
</chart:StackedAreaSeries>

<chart:StackedAreaSeries Fill="#9CA89E" Name="HydroElectric"
<chart:StackedAreaSeries Fill="#9CA89E" Name="HydroElectric" Label="HydroElectric"
TooltipTemplate="{StaticResource toolTipTemplate2}"
ItemsSource="{Binding HydroElectric}" EnableTooltip="True"
ItemsSource="{Binding CollectionOfData}" EnableTooltip="True"
XBindingPath="Month" YBindingPath="HydroElectric">
</chart:StackedAreaSeries>

<chart:StackedAreaSeries Fill="#FF67b4" Name="Coal"
<chart:StackedAreaSeries Fill="#FF67b4" Name="Coal" Label="Coal"
TooltipTemplate="{StaticResource toolTipTemplate1}"
ItemsSource="{Binding Coal}" EnableTooltip="True" XBindingPath="Month"
ItemsSource="{Binding CollectionOfData}" EnableTooltip="True" XBindingPath="Month"
YBindingPath="Coal" >
</chart:StackedAreaSeries>

<chart:StackedAreaSeries Fill="#32CBF1" Name="NaturalGas"
<chart:StackedAreaSeries Fill="#32CBF1" Name="NaturalGas" Label="NaturalGas"
TooltipTemplate="{StaticResource toolTipTemplate}"
ItemsSource="{Binding NaturalGas}" EnableTooltip="True"
ItemsSource="{Binding CollectionOfData}" EnableTooltip="True"
XBindingPath="Month" YBindingPath="NaturalGas" >
</chart:StackedAreaSeries>
</chart:SfCartesianChart>
Expand Down
11 changes: 0 additions & 11 deletions US_Energy_Consumption/Model/EnergyUtilityProducts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,5 @@ public class EnergyUtilityProducts
public double NaturalGas { get; set; }
public double Nuclear { get; set; }
public double HydroElectric { get; set; }

public EnergyUtilityProducts(DateTime month,double coal,double petroleumLiquid,double petroleumCoke,double naturalGas,double nuclear, double hydroElectric)
{
Month = month;
Coal = coal;
PetroleumLiquid = petroleumLiquid;
PetroleumCoke = petroleumCoke;
NaturalGas = naturalGas;
Nuclear = nuclear;
HydroElectric = hydroElectric;
}
}
}
40 changes: 12 additions & 28 deletions US_Energy_Consumption/ViewModel/EnergyUtilityConsumption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,22 @@
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace US_Energy_Consumption
{
public class EnergyUtilityConsumption
{
public ObservableCollection<EnergyUtilityProducts> Coal { get; set; }

public ObservableCollection<EnergyUtilityProducts> PetroleumLiquid { get; set; }

public ObservableCollection<EnergyUtilityProducts> PetroleumCoke { get; set; }

public ObservableCollection<EnergyUtilityProducts> NaturalGas { get; set; }

public ObservableCollection<EnergyUtilityProducts> Nuclear { get; set; }

public ObservableCollection<EnergyUtilityProducts> HydroElectric { get; set; }
public ObservableCollection<EnergyUtilityProducts> CollectionOfData { get; set; }

public EnergyUtilityConsumption()
{

Coal = new ObservableCollection<EnergyUtilityProducts>(ReadCSV("US_Energy_Consumption.Resources.us_energy_consumption_data.csv"));
PetroleumLiquid = new ObservableCollection<EnergyUtilityProducts>(ReadCSV("US_Energy_Consumption.Resources.us_energy_consumption_data.csv"));
PetroleumCoke = new ObservableCollection<EnergyUtilityProducts>(ReadCSV("US_Energy_Consumption.Resources.us_energy_consumption_data.csv"));
NaturalGas = new ObservableCollection<EnergyUtilityProducts>(ReadCSV("US_Energy_Consumption.Resources.us_energy_consumption_data.csv"));
Nuclear = new ObservableCollection<EnergyUtilityProducts>(ReadCSV("US_Energy_Consumption.Resources.us_energy_consumption_data.csv"));
HydroElectric = new ObservableCollection<EnergyUtilityProducts>(ReadCSV("US_Energy_Consumption.Resources.us_energy_consumption_data.csv"));

CollectionOfData = new ObservableCollection<EnergyUtilityProducts>();
ReadCSV("US_Energy_Consumption.Resources.us_energy_consumption_data.csv");
}


private IEnumerable<EnergyUtilityProducts> ReadCSV(string resourceStream)
private void ReadCSV(string resourceStream)
{
Assembly executingAssembly = typeof(App).GetTypeInfo().Assembly;
Stream inputStream = executingAssembly.GetManifestResourceStream(resourceStream);
Expand All @@ -47,15 +28,18 @@ private IEnumerable<EnergyUtilityProducts> ReadCSV(string resourceStream)
using StreamReader reader = new StreamReader(inputStream);
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
lines.Add(line);
}

lines.RemoveAt(0);
return lines.Select(line =>

foreach(var dataPoint in lines)
{
string[] data = line.Split(',');
string[] data = dataPoint.Split(',');
DateTime date = DateTime.ParseExact(data[0], "MMM-yy", CultureInfo.InvariantCulture);
return new EnergyUtilityProducts(date, Convert.ToDouble(data[1]), Convert.ToDouble(data[2]), Convert.ToDouble(data[3]), Convert.ToDouble(data[4]), Convert.ToDouble(data[5]), Convert.ToDouble(data[6]));
});
CollectionOfData.Add(new EnergyUtilityProducts() { Month = date, Coal = Convert.ToDouble(data[1]), PetroleumLiquid = Convert.ToDouble(data[2]), PetroleumCoke = Convert.ToDouble(data[3]), NaturalGas = Convert.ToDouble(data[4]),Nuclear = Convert.ToDouble(data[5]), HydroElectric = Convert.ToDouble(data[6]) });

}
}
}
}

0 comments on commit 687d058

Please sign in to comment.