LiveCharts是一款非常好用的WPF图表绘制类库,相比其他同类类库,LiveCharts的UI风格样式更加多样更加美观。
准备工作:安装以下两个类库:
1、甘特图
前台View代码
1
Gantt Chart xaml
后台ViewModel代码
1 private double _from;
2 private double _to;
3 private Func
4 private string[] _labels;
5 private ChartValues
6 public double From { get { return _from; } set { _from = value; NotifyOfPropertyChange(() => From); } }
7 public double To { get { return _to; } set { _to = value; NotifyOfPropertyChange(() => To); } }
8 public Func
9 public string[] Labels { get { return _labels; } set { _labels = value; NotifyOfPropertyChange(() => Labels); } }
10 public ChartValues
11
12 public void ShowGanttChart()
13 {
14 var now = DateTime.Now;
15
16 Values = new ChartValues
17 {
18 new GanttPoint(now.Ticks, now.AddSeconds(2).Ticks),
19 new GanttPoint(now.AddSeconds(1).Ticks, now.AddSeconds(3).Ticks),
20 new GanttPoint(now.AddSeconds(3).Ticks, now.AddSeconds(5).Ticks),
21 new GanttPoint(now.AddSeconds(5).Ticks, now.AddSeconds(8).Ticks),
22 new GanttPoint(now.AddSeconds(6).Ticks, now.AddSeconds(10).Ticks)
23 };
24
25 Formatter = value => new DateTime((long)value).ToString("MM-dd HH:mm:ss");
26
27 Labels = new[]
28 {
29 "原材料出库", "智能装配","个性化定制", "智能包装", "智能仓储"
30 };
31 From = _values.First().StartPoint;
32 To = _values.Last().EndPoint;
33 }
Gantt Chart cs
效果图
2、进度环
前台View代码
1
2
Gauge xaml
后台ViewModel代码
1 private double _orderProgress;
2 public double OrderProgress { get { return _orderProgress; } set { _orderProgress = value; NotifyOfPropertyChange(() => OrderProgress); } }
3
4 private void ShowGauge()
5 {
6 OrderProgress = 90;
7 }
Gauge cs
效果图
3、柱状堆积图
前台View代码
1
2
3
StackedColumn xaml
后台ViewModel代码
1 private string[] _labels;
2 private IChartValues _v1;
3 private IChartValues _v2;
4 private IChartValues _v3;
5 public IChartValues V1 { get { return _v1; } set { _v1 = value; NotifyOfPropertyChange(() => V1); } }
6 public IChartValues V2 { get { return _v2; } set { _v2 = value; NotifyOfPropertyChange(() => V2); } }
7 public IChartValues V3 { get { return _v3; } set { _v3 = value; NotifyOfPropertyChange(() => V3); } }
8 public string[] Labels { get { return _labels; } set { _labels = value; NotifyOfPropertyChange(() => Labels); } }
9 private void ShowStackedColumn()
10 {
11 V1 = new ChartValues
12 {
13 new ObservableValue(5),
14 new ObservableValue(8),
15 new ObservableValue(2),
16 new ObservableValue(4),
17 new ObservableValue(6),
18 new ObservableValue(2),
19 new ObservableValue(9),
20 new ObservableValue(4),
21 new ObservableValue(6),
22 new ObservableValue(2),
23 new ObservableValue(9),
24 new ObservableValue(3)
25 };
26 V2 = new ChartValues
27 {
28 new ObservableValue(5),
29 new ObservableValue(8),
30 new ObservableValue(2),
31 new ObservableValue(4),
32 new ObservableValue(6),
33 new ObservableValue(2),
34 new ObservableValue(9),
35 new ObservableValue(4),
36 new ObservableValue(6),
37 new ObservableValue(2),
38 new ObservableValue(9),
39 new ObservableValue(3)
40 };
41 V3 = new ChartValues
42 {
43 new ObservableValue(5),
44 new ObservableValue(8),
45 new ObservableValue(2),
46 new ObservableValue(4),
47 new ObservableValue(6),
48 new ObservableValue(2),
49 new ObservableValue(9),
50 new ObservableValue(4),
51 new ObservableValue(6),
52 new ObservableValue(2),
53 new ObservableValue(9),
54 new ObservableValue(3)
55 };
56
57 Labels = new[]
58 {
59 "1月", "2月","3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"
60 };
61 }
StackedColumn cs
效果图
4、饼状图
前台代码
1
2
3
Pie xaml
效果图
或者:
前台代码
1
Pie2 xaml
后台代码
1 private SeriesCollection _orderCountSeries;
2 public SeriesCollection OrderCountSeries { get { return _orderCountSeries; } set { _orderCountSeries = value; NotifyOfPropertyChange(() => OrderCountSeries); } }
3 public ObservableValue OrangeOrderCount { get; set; } = new ObservableValue();
4 public ObservableValue AppleOrderCount { get; set; } = new ObservableValue();
5 public ObservableValue PearOrderCount { get; set; } = new ObservableValue();
6
7 private void InitializeChartSeries()
8 {
9 OrderCountSeries = new SeriesCollection
10 {
11 new PieSeries
12 {
13 Title = "橙 汁 Orange",
14 Values = new ChartValues
15 DataLabels = true,
16 FontSize = 28
17 },
18 new PieSeries
19 {
20 Title = "苹果汁 Apple",
21 Values = new ChartValues
22 DataLabels = true,
23 FontSize = 28
24 },
25 new PieSeries
26 {
27 Title = "梨 汁 Pear",
28 Values = new ChartValues
29 DataLabels = true,
30 FontSize = 28
31 }
32 };
33 }
34
35 private void UpdateChartSeries()
36 {
37 OrangeOrderCount.Value = TodaysFinishedOrderList != null ? TodaysFinishedOrderList.FindAll(o => o.PRODUCT_CODE == (int)ItemType.Orange).Count : 0;
38 AppleOrderCount.Value = TodaysFinishedOrderList != null ? TodaysFinishedOrderList.FindAll(o => o.PRODUCT_CODE == (int)ItemType.Apple).Count : 0;
39 PearOrderCount.Value = TodaysFinishedOrderList != null ? TodaysFinishedOrderList.FindAll(o => o.PRODUCT_CODE == (int)ItemType.Pear).Count : 0;
40
41 OrderCountSeries[0].Values[0] = OrangeOrderCount;
42 OrderCountSeries[1].Values[0] = AppleOrderCount;
43 OrderCountSeries[2].Values[0] = PearOrderCount;
44 }
Pie2 cs
效果图
5、柱状图
前台代码
1
2
Column xaml
后台代码
1 private Func
2 private string[] _storageLabels;
3 private IChartValues _storagePercentages;
4 public Func
5 public string[] StorageLabels { get { return _storageLabels; } set { _storageLabels = value; NotifyOfPropertyChange(() => StorageLabels); } }
6 public IChartValues StoragePercentages { get { return _storagePercentages; } set { _storagePercentages = value; NotifyOfPropertyChange(() => StoragePercentages); } }
7 private void ShowStoragePercentageColumnChart()
8 {
9 StorageLabels = new[]
10 {
11 "单元一原料","单元一成品","单元二原料","单元二半成品","单元三原料","单元三半成品","单元四原料","单元四半成品"
12 };
13 AxisPercentage = val => val.ToString("P");
14 StoragePercentages = new ChartValues
15 }
Column cs
效果图
6、折线图
前台代码
1
Line xaml
后台代码
1 private string[] _xTimeLabels;
2 private Func
3 private ChartValues
4 private ChartValues
5 private ChartValues
6 private ChartValues
7 public string[] XTimeLabels { get { return _xTimeLabels; } set { _xTimeLabels = value; NotifyOfPropertyChange(() => XTimeLabels); } }
8 public Func
9 public ChartValues
10 public ChartValues
11 public ChartValues
12 public ChartValues
13 private void InitOccupancyRate()
14 {
15 YPercentageFormat = value => value.ToString("P0"); //P0表示只保留整数,P表示保留小数点后两位
16 XTimeLabels = new[] { "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" }; ;
17 OccupancyRatesCell1 = new ChartValues
18 OccupancyRatesCell2 = new ChartValues
19 OccupancyRatesCell3 = new ChartValues
20 OccupancyRatesCell4 = new ChartValues
21 }
Line cs
效果图
7、LiveCharts所用到的样式Style.xaml
1
2
3
10
11
18
19
26
27
34
35
42
43
style
手机扫一扫
移动阅读更方便
你可能感兴趣的文章