stacked area

main
syneffort 1 year ago
parent fcfca60577
commit f385096d7f
  1. 29
      LC_Tutorial/Tutorial/BasicPlots/StackedAreaWindow.xaml
  2. 145
      LC_Tutorial/Tutorial/BasicPlots/StackedAreaWindow.xaml.cs
  3. 1
      LC_Tutorial/Tutorial/MainWindow.xaml
  4. 6
      LC_Tutorial/Tutorial/MainWindow.xaml.cs

@ -0,0 +1,29 @@
<Window x:Class="Tutorial.BasicPlots.StackedAreaWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Tutorial.BasicPlots"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
Title="StackedAreaWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<lvc:CartesianChart Grid.Row="0" Series="{Binding SeriesCollection}" LegendLocation="Right">
<lvc:CartesianChart.AxisX>
<lvc:Axis Title="Year" LabelFormatter="{Binding XFormatter}"/>
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis Title="Population" LabelFormatter="{Binding YFormatter}"/>
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
<Button x:Name="btnChageMode" Grid.Row="1" Content="Change Stack Mode" Click="btnChageMode_Click"/>
<Button x:Name="btnAdd" Grid.Row="2" Content="Add" Click="btnAdd_Click"/>
</Grid>
</Window>

@ -0,0 +1,145 @@
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Tutorial.BasicPlots
{
/// <summary>
/// StackedAreaWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class StackedAreaWindow : Window
{
private int _index = 2014;
public event PropertyChangedEventHandler? PropertyChanged;
public SeriesCollection SeriesCollection { get; set; }
public string[] Labels { get; set; }
public StackMode StackMode { get; set; }
public Func<double, string> XFormatter { get; set; }
public Func<double, string> YFormatter { get; set; }
public StackedAreaWindow()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
SeriesCollection = new SeriesCollection
{
new StackedAreaSeries
{
Title = "Africa",
Values = new ChartValues<DateTimePoint>
{
new DateTimePoint(new DateTime(1950, 1, 1), .228),
new DateTimePoint(new DateTime(1960, 1, 1), .285),
new DateTimePoint(new DateTime(1970, 1, 1), .366),
new DateTimePoint(new DateTime(1980, 1, 1), .478),
new DateTimePoint(new DateTime(1990, 1, 1), .629),
new DateTimePoint(new DateTime(2000, 1, 1), .808),
new DateTimePoint(new DateTime(2010, 1, 1), 1.031),
new DateTimePoint(new DateTime(2013, 1, 1), 1.110)
},
LineSmoothness = 1,
PointGeometry = DefaultGeometries.Square
},
new StackedAreaSeries
{
Title = "N & S America",
Values = new ChartValues<DateTimePoint>
{
new DateTimePoint(new DateTime(1950, 1, 1), .339),
new DateTimePoint(new DateTime(1960, 1, 1), .424),
new DateTimePoint(new DateTime(1970, 1, 1), .519),
new DateTimePoint(new DateTime(1980, 1, 1), .618),
new DateTimePoint(new DateTime(1990, 1, 1), .727),
new DateTimePoint(new DateTime(2000, 1, 1), .841),
new DateTimePoint(new DateTime(2010, 1, 1), .942),
new DateTimePoint(new DateTime(2013, 1, 1), .972)
},
LineSmoothness = 1,
PointGeometry = DefaultGeometries.Square
},
new StackedAreaSeries
{
Title = "Asia",
Values = new ChartValues<DateTimePoint>
{
new DateTimePoint(new DateTime(1950, 1, 1), 1.395),
new DateTimePoint(new DateTime(1960, 1, 1), 1.694),
new DateTimePoint(new DateTime(1970, 1, 1), 2.128),
new DateTimePoint(new DateTime(1980, 1, 1), 2.634),
new DateTimePoint(new DateTime(1990, 1, 1), 3.213),
new DateTimePoint(new DateTime(2000, 1, 1), 3.717),
new DateTimePoint(new DateTime(2010, 1, 1), 4.165),
new DateTimePoint(new DateTime(2013, 1, 1), 4.298)
},
LineSmoothness = 1,
PointGeometry = DefaultGeometries.Square
},
new StackedAreaSeries
{
Title = "Europe",
Values = new ChartValues<DateTimePoint>
{
new DateTimePoint(new DateTime(1950, 1, 1), .549),
new DateTimePoint(new DateTime(1960, 1, 1), .605),
new DateTimePoint(new DateTime(1970, 1, 1), .657),
new DateTimePoint(new DateTime(1980, 1, 1), .694),
new DateTimePoint(new DateTime(1990, 1, 1), .723),
new DateTimePoint(new DateTime(2000, 1, 1), .729),
new DateTimePoint(new DateTime(2010, 1, 1), .740),
new DateTimePoint(new DateTime(2013, 1, 1), .742)
},
LineSmoothness = 1,
PointGeometry = DefaultGeometries.Square
}
};
XFormatter = val => new DateTime((long)val).ToString("yyyy");
YFormatter = val => $"{val.ToString("N")} M";
DataContext = this;
}
private void btnChageMode_Click(object sender, RoutedEventArgs e)
{
foreach (var series in SeriesCollection.Cast<StackedAreaSeries>())
{
series.StackMode = series.StackMode == StackMode.Percentage ? StackMode.Values : StackMode.Percentage;
}
if (((StackedAreaSeries)SeriesCollection[0]).StackMode == StackMode.Values)
YFormatter = val => $"{val.ToString("N")} M";
else
YFormatter = val => val.ToString("P");
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random();
int year = _index++;
foreach (var series in SeriesCollection.Cast<StackedAreaSeries>())
{
series.Values.Add(new DateTimePoint(new DateTime(year, 1, 1), ((DateTimePoint)series.Values[series.Values.Count - 1]).Value + rand.NextDouble()));
}
}
}
}

@ -16,6 +16,7 @@
<Button x:Name="btnColumns" Content="Columns" Margin="3" Click="btnColumns_Click"/> <Button x:Name="btnColumns" Content="Columns" Margin="3" Click="btnColumns_Click"/>
<Button x:Name="btnStackedColumns" Content="Stacked Columns" Margin="3" Click="btnStackedColumns_Click"/> <Button x:Name="btnStackedColumns" Content="Stacked Columns" Margin="3" Click="btnStackedColumns_Click"/>
<Button x:Name="btnRows" Content="Rows" Margin="3" Click="btnRows_Click"/> <Button x:Name="btnRows" Content="Rows" Margin="3" Click="btnRows_Click"/>
<Button x:Name="btnStackedArea" Content="Stacked Area" Margin="3" Click="btnStackedArea_Click"/>
</StackPanel> </StackPanel>
</Grid> </Grid>
</Window> </Window>

@ -49,5 +49,11 @@ namespace Tutorial
RowsWindow win = new RowsWindow(); RowsWindow win = new RowsWindow();
win.Show(); win.Show();
} }
private void btnStackedArea_Click(object sender, RoutedEventArgs e)
{
StackedAreaWindow win = new StackedAreaWindow();
win.Show();
}
} }
} }

Loading…
Cancel
Save