本帖最后由 dddsssssqq 于 2022-5-17 15:15 编辑
MainWindow.xaml:
<Window
Height="450"
Title="MainWindow"
Width="800"
mc:Ignorable="d"
x:Class="FlexChartDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel
Grid.Column="0"
Orientation="Vertical"
Width="200">
<Button Click="CheckData1_OnClick">Check Data1</Button>
<Button Click="CheckData2_OnClick">Check Data2</Button>
</StackPanel>
<GridSplitter
Background="Black"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
Width="3" />
<ScrollViewer
Grid.Column="2"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Disabled">
<ScrollViewer.Resources>
<c1:ChartStyle
FontWeight="Bold"
LinePattern="Dash"
Stroke="Black"
StrokeThickness="0.2"
x:Key="ChartStyle" />
</ScrollViewer.Resources>
<c1:FlexChart
BindingX="Time"
ChartType="Column"
Margin="0,30,0,0"
MinWidth="500"
x:Name="flexChart">
<c1:FlexChart.DataLabel>
<c1:DataLabel Content="{}{y}" Position="Top" />
</c1:FlexChart.DataLabel>
<c1:FlexChart.ToolTip>
<ToolTip Visibility="Collapsed" />
</c1:FlexChart.ToolTip>
<c1:Series Binding="DF" Tooltip="{}{y}" />
<c1:Series
Binding="CDf"
ChartType="LineSymbols"
SymbolMarker="Dot"
SymbolSize="5"
Visibility="Hidden" />
<c1:FlexChart.AxisY>
<c1:Axis
AxisLine="False"
Format="0"
MajorGrid="True"
MajorGridStyle="{StaticResource ChartStyle}"
MajorUnit="0.05"
Max="1"
Min="0"
Position="Left"
TickLength="0" />
</c1:FlexChart.AxisY>
<c1:FlexChart.AxisX>
<c1:Axis OverlappingLabels="WordWrap" />
</c1:FlexChart.AxisX>
</c1:FlexChart>
</ScrollViewer>
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using C1.WPF.Chart;
namespace FlexChartDemo;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
private readonly List<SolidColorBrush> _brushs = new() {
new SolidColorBrush(Colors.Red),
new SolidColorBrush(Colors.OrangeRed),
new SolidColorBrush(Colors.Orange),
new SolidColorBrush(Colors.Yellow),
new SolidColorBrush(Colors.YellowGreen),
new SolidColorBrush(Colors.GreenYellow)
};
private readonly List<int> _data = new();
private readonly Random rnd = new();
private List<DataItem> _data1;
private List<DataItem> _data2;
public MainWindow() {
InitializeComponent();
for (var i = 0; i < 12; i++) {
_data.Add(rnd.Next(20, 100));
}
flexChart.ItemsSource = Data1;
flexChart.Series[0].SymbolRendering += FlexchartSymbolRendering;
flexChart.Series[1].SymbolRendering += FlexchartSymbolRendering;
}
public List<DataItem> Data1 {
get {
if (_data1 == null) {
var sum = _data.Sum() * 1.0;
var count = 0;
_data1 = new List<DataItem>();
for (var i = 0; i < 7; i++) {
count += _data[i];
_data1.Add(new DataItem {
Time = $"{i + 1}",
PDF = Math.Round(_data[i] / sum, 4),
CDf = Math.Round(count / sum, 4)
});
}
}
return _data1;
}
}
public List<DataItem> Data2 {
get {
if (_data2 == null) {
var sum = _data.Sum() * 1.0;
var count = 0;
_data2 = new List<DataItem>();
for (var i = 0; i < 9; i++) {
count += _data[i];
_data2.Add(new DataItem {
Time = $"{i + 1},{i + 1},{i + 1},{i + 1}, {i + 1},{i + 1},{i + 1} ",
PDF = Math.Round(_data[i] / sum, 4),
CDf = Math.Round(count / sum, 4)
});
}
}
return _data2;
}
}
private void FlexchartSymbolRendering(object? sender, RenderSymbolEventArgs e) {
e.Engine.SetFill(_brushs[e.Index % 6]);
}
private void CheckData1_OnClick(object sender, RoutedEventArgs e) {
flexChart.ItemsSource = Data1;
}
private void CheckData2_OnClick(object sender, RoutedEventArgs e) {
flexChart.ItemsSource = Data2;
}
public class DataItem {
public string Time { get; set; }
public double CDf { get; set; }
public double PDF { get; set; }
}
}
|