mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-04-14 05:06:36 +08:00
项目结构调整
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<UserControl x:Class="Wpf.PieChart.DoughnutChartExample"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Wpf.PieChart"
|
||||
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="300" d:DesignWidth="300" d:DataContext="{d:DesignInstance local:DoughnutChartExample}">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"></RowDefinition>
|
||||
<RowDefinition Height="*"></RowDefinition>
|
||||
</Grid.RowDefinitions>
|
||||
<StackPanel Grid.Row="0" Orientation="Horizontal">
|
||||
<Button Click="UpdateAllOnClick" Padding="10 0">
|
||||
Move All
|
||||
</Button>
|
||||
<Button Click="RestartOnClick">Restart</Button>
|
||||
<TextBlock VerticalAlignment="Center" Padding="10 0">Series</TextBlock>
|
||||
<Button Click="AddSeriesOnClick" MinWidth="30">
|
||||
+
|
||||
</Button>
|
||||
<Button Click="RemoveSeriesOnClick" MinWidth="30">
|
||||
-
|
||||
</Button>
|
||||
<TextBlock VerticalAlignment="Center" Padding="10 0">Values</TextBlock>
|
||||
<Button Height="30" Click="AddValueOnClick" MinWidth="30">
|
||||
+
|
||||
</Button>
|
||||
<Button Height="30" Click="RemoveValueOnClick" MinWidth="30">
|
||||
-
|
||||
</Button>
|
||||
|
||||
</StackPanel>
|
||||
<lvc:PieChart Name="Chart" Grid.Row="1" Series="{Binding SeriesCollection}"
|
||||
LegendLocation="Right" InnerRadius="100" Margin="0 15">
|
||||
<lvc:PieChart.ChartLegend>
|
||||
<lvc:DefaultLegend BulletSize="20"></lvc:DefaultLegend>
|
||||
</lvc:PieChart.ChartLegend>
|
||||
<lvc:PieChart.DataTooltip>
|
||||
<lvc:DefaultTooltip BulletSize="20"></lvc:DefaultTooltip>
|
||||
</lvc:PieChart.DataTooltip>
|
||||
</lvc:PieChart>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using LiveCharts;
|
||||
using LiveCharts.Defaults;
|
||||
using LiveCharts.Wpf;
|
||||
|
||||
namespace Wpf.PieChart
|
||||
{
|
||||
public partial class DoughnutChartExample
|
||||
{
|
||||
public DoughnutChartExample()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
SeriesCollection = new SeriesCollection
|
||||
{
|
||||
new PieSeries
|
||||
{
|
||||
Title = "Chrome",
|
||||
Values = new ChartValues<ObservableValue> { new ObservableValue(8) },
|
||||
DataLabels = true
|
||||
},
|
||||
new PieSeries
|
||||
{
|
||||
Title = "Mozilla",
|
||||
Values = new ChartValues<ObservableValue> { new ObservableValue(6) },
|
||||
DataLabels = true
|
||||
},
|
||||
new PieSeries
|
||||
{
|
||||
Title = "Opera",
|
||||
Values = new ChartValues<ObservableValue> { new ObservableValue(10) },
|
||||
DataLabels = true
|
||||
},
|
||||
new PieSeries
|
||||
{
|
||||
Title = "Explorer",
|
||||
Values = new ChartValues<ObservableValue> { new ObservableValue(4) },
|
||||
DataLabels = true
|
||||
}
|
||||
};
|
||||
|
||||
//adding values or series will update and animate the chart automatically
|
||||
//SeriesCollection.Add(new PieSeries());
|
||||
//SeriesCollection[0].Values.Add(5);
|
||||
|
||||
DataContext = this;
|
||||
}
|
||||
|
||||
public SeriesCollection SeriesCollection { get; set; }
|
||||
|
||||
private void UpdateAllOnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var r = new Random();
|
||||
|
||||
foreach (var series in SeriesCollection)
|
||||
{
|
||||
foreach (var observable in series.Values.Cast<ObservableValue>())
|
||||
{
|
||||
observable.Value = r.Next(0, 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddSeriesOnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var r = new Random();
|
||||
var c = SeriesCollection.Count > 0 ? SeriesCollection[0].Values.Count : 5;
|
||||
|
||||
var vals = new ChartValues<ObservableValue>();
|
||||
|
||||
for (var i = 0; i < c; i++)
|
||||
{
|
||||
vals.Add(new ObservableValue(r.Next(0, 10)));
|
||||
}
|
||||
|
||||
SeriesCollection.Add(new PieSeries
|
||||
{
|
||||
Values = vals
|
||||
});
|
||||
}
|
||||
|
||||
private void RemoveSeriesOnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (SeriesCollection.Count > 0)
|
||||
SeriesCollection.RemoveAt(0);
|
||||
}
|
||||
|
||||
private void AddValueOnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var r = new Random();
|
||||
foreach (var series in SeriesCollection)
|
||||
{
|
||||
series.Values.Add(new ObservableValue(r.Next(0, 10)));
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveValueOnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
foreach (var series in SeriesCollection)
|
||||
{
|
||||
if (series.Values.Count > 0)
|
||||
series.Values.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void RestartOnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Chart.Update(true, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Wpf.PieChart.DropDowns
|
||||
{
|
||||
public static class DataProvider
|
||||
{
|
||||
public static IEnumerable<double> Values
|
||||
{
|
||||
get
|
||||
{
|
||||
var r = new Random();
|
||||
|
||||
for (var i = 0; i < 30; i++)
|
||||
{
|
||||
yield return r.NextDouble() * 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
using LiveCharts;
|
||||
|
||||
namespace Wpf.PieChart.DropDowns
|
||||
{
|
||||
public class DropDownCommand : ICommand
|
||||
{
|
||||
private Action<DropDownPoint> _action;
|
||||
|
||||
public DropDownCommand(Action<DropDownPoint> action)
|
||||
{
|
||||
_action = action;
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
var chartPoint = (ChartPoint) parameter;
|
||||
var dropDownPoint = (DropDownPoint) chartPoint.Instance;
|
||||
|
||||
_action(dropDownPoint);
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Windows.Media;
|
||||
using LiveCharts;
|
||||
using LiveCharts.Helpers;
|
||||
using LiveCharts.Wpf;
|
||||
|
||||
namespace Wpf.PieChart.DropDowns
|
||||
{
|
||||
public class DropDownViewModel : INotifyPropertyChanged
|
||||
{
|
||||
private SeriesCollection _series;
|
||||
|
||||
public DropDownViewModel()
|
||||
{
|
||||
var navigation = new List<SeriesCollection>();
|
||||
var initialValues = DataProvider.Values.ToArray();
|
||||
|
||||
Series = GroupSeriesByTheshold(content: initialValues, threshold: initialValues.Max() * .7);
|
||||
|
||||
SliceClickCommand = new DropDownCommand(dropDownPoint =>
|
||||
{
|
||||
//if the point has no content to display...
|
||||
if (dropDownPoint.Content.Length == 1) return;
|
||||
|
||||
navigation.Add(Series.Select(x => new PieSeries
|
||||
{
|
||||
Values = x.Values,
|
||||
Title = x.Title,
|
||||
Fill = ((Series) x).Fill
|
||||
}).AsSeriesCollection());
|
||||
|
||||
Series = GroupSeriesByTheshold(content: dropDownPoint.Content, threshold: dropDownPoint.Content.Max() * .7);
|
||||
});
|
||||
|
||||
GoBackCommand = new RelayCommand(() =>
|
||||
{
|
||||
if (!navigation.Any())return;
|
||||
var previous = navigation.Last();
|
||||
if(previous == null) return;
|
||||
Series = previous;
|
||||
navigation.Remove(previous);
|
||||
});
|
||||
|
||||
Formatter = x => x.ToString("N1");
|
||||
}
|
||||
|
||||
public SeriesCollection Series
|
||||
{
|
||||
get { return _series; }
|
||||
set
|
||||
{
|
||||
_series = value;
|
||||
OnPropertyChanged("Series");
|
||||
}
|
||||
}
|
||||
public DropDownCommand SliceClickCommand { get; set; }
|
||||
public RelayCommand GoBackCommand { get; set; }
|
||||
public Func<double, string> Formatter { get; set; }
|
||||
|
||||
private static SeriesCollection GroupSeriesByTheshold(double[] content, double threshold)
|
||||
{
|
||||
var series = content
|
||||
.Where(x => x > threshold)
|
||||
.Select((value, index) => new PieSeries
|
||||
{
|
||||
Values = new ChartValues<DropDownPoint>
|
||||
{
|
||||
new DropDownPoint {Content = new[] {value}}
|
||||
},
|
||||
Title = "Series " + index
|
||||
}).AsSeriesCollection();
|
||||
|
||||
var thresholdSeries = new PieSeries
|
||||
{
|
||||
Values = new ChartValues<DropDownPoint>
|
||||
{
|
||||
new DropDownPoint
|
||||
{
|
||||
Content = content.Where(x => x < threshold).ToArray()
|
||||
}
|
||||
},
|
||||
Title = "DropDown Slice",
|
||||
Fill = new SolidColorBrush(Color.FromRgb(30,30,30)),
|
||||
PushOut = 5
|
||||
};
|
||||
series.Add(thresholdSeries);
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
#region INotifyPropertyChanged implementation
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected virtual void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
var handler = PropertyChanged;
|
||||
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<UserControl x:Class="Wpf.PieChart.DropDowns.PieDropDownSample"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Wpf.PieChart.DropDowns"
|
||||
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="300" d:DesignWidth="300">
|
||||
<UserControl.DataContext>
|
||||
<local:DropDownViewModel></local:DropDownViewModel>
|
||||
</UserControl.DataContext>
|
||||
<UserControl.Resources>
|
||||
<Style TargetType="lvc:PieSeries">
|
||||
<Setter Property="StrokeThickness" Value="0"></Setter>
|
||||
<Setter Property="DataLabels" Value="True"></Setter>
|
||||
<Setter Property="LabelPosition" Value="OutsideSlice"></Setter>
|
||||
<Setter Property="Foreground" Value="#303030"></Setter>
|
||||
<Setter Property="FontSize" Value="16"></Setter>
|
||||
</Style>
|
||||
</UserControl.Resources>
|
||||
<Grid>
|
||||
<Button VerticalAlignment="Top" HorizontalAlignment="Left"
|
||||
Command="{Binding GoBackCommand}">
|
||||
Go Back
|
||||
</Button>
|
||||
<TextBlock VerticalAlignment="Bottom" TextWrapping="Wrap">Click in the black slice to open more content, click back to go to the previous state</TextBlock>
|
||||
<lvc:PieChart Series="{Binding Series}"
|
||||
DataClickCommand="{Binding SliceClickCommand}"
|
||||
Margin="40"
|
||||
DataTooltip="{x:Null}"
|
||||
AnimationsSpeed="0:0:0.25">
|
||||
<lvc:PieChart.AxisY>
|
||||
<lvc:Axis LabelFormatter="{Binding Formatter}"></lvc:Axis>
|
||||
</lvc:PieChart.AxisY>
|
||||
</lvc:PieChart>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
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.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Wpf.PieChart.DropDowns
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for PieDropDownSample.xaml
|
||||
/// </summary>
|
||||
public partial class PieDropDownSample : UserControl
|
||||
{
|
||||
public PieDropDownSample()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Linq;
|
||||
using LiveCharts;
|
||||
using LiveCharts.Configurations;
|
||||
|
||||
namespace Wpf.PieChart.DropDowns
|
||||
{
|
||||
public class DropDownPoint
|
||||
{
|
||||
static DropDownPoint()
|
||||
{
|
||||
//In this case we are plotting our own point to have
|
||||
//more control over the current plot
|
||||
//configuring a custom type is quite simple
|
||||
|
||||
//first we define a mapper
|
||||
var mapper = Mappers.Pie<DropDownPoint>()
|
||||
.Value(x => x.Value);//use the value property in the plot
|
||||
|
||||
//then we save the mapper globally, there are many ways
|
||||
//so configure a series, for more info see:
|
||||
//https://lvcharts.net/App/examples/v1/wpf/Types%20and%20Configuration
|
||||
Charting.For<DropDownPoint>(mapper);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the value to plot
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The value.
|
||||
/// </value>
|
||||
public double Value
|
||||
{
|
||||
get { return Content.Sum(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the content, all the values that represent this point
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The content.
|
||||
/// </value>
|
||||
public double[] Content { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the title.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The title.
|
||||
/// </value>
|
||||
public double Title { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Wpf.PieChart.DropDowns
|
||||
{
|
||||
public class RelayCommand : ICommand
|
||||
{
|
||||
private Action _action;
|
||||
|
||||
public RelayCommand(Action action)
|
||||
{
|
||||
_action = action;
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_action();
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<UserControl x:Class="Wpf.PieChart.PieChartExample"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Wpf.PieChart"
|
||||
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="300" d:DesignWidth="500"
|
||||
d:DataContext="{d:DesignInstance local:PieChartExample}">
|
||||
<Grid>
|
||||
<lvc:PieChart LegendLocation="Bottom" DataClick="Chart_OnDataClick" Hoverable="False" DataTooltip="{x:Null}">
|
||||
<lvc:PieChart.Series>
|
||||
<lvc:PieSeries Title="Maria" Values="3" DataLabels="True"
|
||||
LabelPoint="{Binding PointLabel}"/>
|
||||
<lvc:PieSeries Title="Charles" Values="4" DataLabels="True"
|
||||
LabelPoint="{Binding PointLabel}"/>
|
||||
<lvc:PieSeries Title="Frida" Values="6" DataLabels="True"
|
||||
LabelPoint="{Binding PointLabel}"/>
|
||||
<lvc:PieSeries Title="Frederic" Values="2" DataLabels="True"
|
||||
LabelPoint="{Binding PointLabel}"/>
|
||||
</lvc:PieChart.Series>
|
||||
</lvc:PieChart>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Windows.Controls;
|
||||
using LiveCharts;
|
||||
using LiveCharts.Wpf;
|
||||
|
||||
namespace Wpf.PieChart
|
||||
{
|
||||
public partial class PieChartExample : UserControl
|
||||
{
|
||||
public PieChartExample()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
PointLabel = chartPoint =>
|
||||
string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
|
||||
|
||||
DataContext = this;
|
||||
}
|
||||
|
||||
public Func<ChartPoint, string> PointLabel { get; set; }
|
||||
|
||||
private void Chart_OnDataClick(object sender, ChartPoint chartpoint)
|
||||
{
|
||||
var chart = (LiveCharts.Wpf.PieChart) chartpoint.ChartView;
|
||||
|
||||
//clear selected slice.
|
||||
foreach (PieSeries series in chart.Series)
|
||||
series.PushOut = 0;
|
||||
|
||||
var selectedSeries = (PieSeries) chartpoint.SeriesView;
|
||||
selectedSeries.PushOut = 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user