点击获取工具 >>
针对那些心愿以新鲜独特的形式可视化数据的用户来说,新版本对数据可视化进行了重大降级,v20.1 蕴含一个新的 Sankey Diagram 控件(用于 WinForms 和 WPF)。
Sankey Diagrams 显示节点和关联数据之间的门路,每个门路的粗细依据相应节点的值而变动。应用时,最终用户能够轻松地看到次要门路,并发现给定流程中的低效率或损失。这些图通常用于显示资源流(销售,估算)以及信息或能源应用状况。
自定义 Colorizer
要基于自定义算法绘制链接和节点,请创立一个实现 ISankeyColorizer 接口的类。而后,将此类的对象调配给 Colorizer 属性。
以下代码实现了一个 colorizer,该 colorizer 将随机色彩利用于节点,并指定用于将突变填充利用于链接的色彩:
C#
`private void Form1_Load(object sender, EventArgs e) {
sankeyDiagramControl1.Colorizer = new MyColorizer {
LinkSourceColor = Color.Red,
LinkTargetColor = Color.Yellow
};
public class MyColorizer : ISankeyColorizer {
public event EventHandler ColorizerChanged;
Random rand = new Random();
Dictionary<string, Color> KeyColorPairs = new Dictionary<string, Color>();
public Color LinkSourceColor {get; set;}
public Color LinkTargetColor {get; set;}
public Color GetLinkSourceColor(SankeyLink link) {
return LinkSourceColor;
}
public Color GetLinkTargetColor(SankeyLink link) {
return LinkTargetColor;
}
public Color GetNodeColor(SankeyNode info) {
if (!KeyColorPairs.TryGetValue((string)info.Tag, out Color nodeColor)) {
nodeColor = GenerateColor();
KeyColorPairs.Add((string)info.Tag, nodeColor);
}
return nodeColor;
}
private Color GenerateColor() {
return Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
}
}
`
VB.NET
`Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
sankeyDiagramControl1.Colorizer = New MyColorizer With {
.LinkSourceColor = Color.Red,
.LinkTargetColor = Color.Yellow
}
End Sub
Public Class MyColorizer
Inherits ISankeyColorizer
Public Event ColorizerChanged As EventHandler
Private rand As Random = New Random()
Private KeyColorPairs As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
Public Property LinkSourceColor As Color
Public Property LinkTargetColor As Color
Public Function GetLinkSourceColor(ByVal link As SankeyLink) As Color
Return LinkSourceColor
End Function
Public Function GetLinkTargetColor(ByVal link As SankeyLink) As Color
Return LinkTargetColor
End Function
Public Function GetNodeColor(ByVal info As SankeyNode) As Color
Dim nodeColor As Color = Nothing
If Not KeyColorPairs.TryGetValue(CStr(info.Tag), nodeColor) Then
nodeColor = GenerateColor()
KeyColorPairs.Add(CStr(info.Tag), nodeColor)
End If
Return nodeColor
End Function
Private Function GenerateColor() As Color
Return Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))
End Function
End Class
`
自定义工具提醒
当鼠标指针悬停在节点或链接上时,将显示工具提醒。应用 SankeyToolTipOptions.NodeToolTipEnabled、SankeyToolTipOptions.LinkToolTipEnabled 和 ToolTipController 属性能够禁用 / 启用工具提醒并自定义其外观。要格式化工具提醒文本,请解决 CustomizeNodeToolTip 和 CustomizeLinkToolTip 事件,并在事件处理程序中指定 CustomizeSankeyToolTipEventArgs.Title 和 CustomizeSankeyToolTipEventArgs.Content 属性。
以下代码格式化了工具提醒中应用的文本,e.Node.Tag、e.Link.Source.Tag 和 e.Link.Target.Tag 属性存储在默认工具提醒题目中显示的值。要获取节点和链接权重,请应用 e.Node.TotalWeight 和 e.Link.TotalWeight 属性。
C#
`private void Form1_Load(object sender, EventArgs e) {
sankeyDiagramControl1.ToolTipOptions.LinkToolTipEnabled = DevExpress.Utils.DefaultBoolean.True;
sankeyDiagramControl1.ToolTipOptions.NodeToolTipEnabled = DevExpress.Utils.DefaultBoolean.True;
sankeyDiagramControl1.ToolTipController = new DevExpress.Utils.ToolTipController {
ToolTipType = DevExpress.Utils.ToolTipType.Flyout,
AllowHtmlText = true
};
sankeyDiagramControl1.CustomizeNodeToolTip += OnCustomizeNodeToolTip;
sankeyDiagramControl1.CustomizeLinkToolTip += OnCustomizeLinkToolTip;
}
private void OnCustomizeNodeToolTip(object sender, CustomizeSankeyNodeToolTipEventArgs e) {
e.Title = $”Country: <u>{e.Node.Tag}</u>”;
e.Content = $”{e.Node.TotalWeight:f1}”;
}
private void OnCustomizeLinkToolTip(object sender, CustomizeSankeyLinkToolTipEventArgs e) {
e.Title = $”{e.Link.Source.Tag} → {e.Link.Target.Tag}”;
e.Content = $”{e.Link.TotalWeight}”;
}
`
VB.NET
`Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
sankeyDiagramControl1.ToolTipOptions.LinkToolTipEnabled = DevExpress.Utils.DefaultBoolean.[True]
sankeyDiagramControl1.ToolTipOptions.NodeToolTipEnabled = DevExpress.Utils.DefaultBoolean.[True]
sankeyDiagramControl1.ToolTipController = New DevExpress.Utils.ToolTipController With {
.ToolTipType = DevExpress.Utils.ToolTipType.Flyout,
.AllowHtmlText = True
}
sankeyDiagramControl1.CustomizeNodeToolTip += AddressOf OnCustomizeNodeToolTip
sankeyDiagramControl1.CustomizeLinkToolTip += AddressOf OnCustomizeLinkToolTip
End Sub
Private Sub OnCustomizeNodeToolTip(ByVal sender As Object, ByVal e As CustomizeSankeyNodeToolTipEventArgs)
e.Title = $”Country: <u>{e.Node.Tag}</u>”
e.Text = $”{e.Node.TotalWeight:f1}”
End Sub
Private Sub OnCustomizeLinkToolTip(ByVal sender As Object, ByVal e As CustomizeSankeyLinkToolTipEventArgs)
e.Title = $”{e.Link.Source.Tag} → {e.Link.Target.Tag}”
e.Text = $”{e.Link.TotalWeight}”;
End Sub
`
打印和导出
要打印控件,请抉择应用以下办法:
- ShowPrintDialog
- ShowPrintPreview
- ShowRibbonPrintPreview
应用以下办法能够将控件导出为各种格局:
- ExportToDocx
- ExportToHtml
- ExportToImage
- ExportToMht
- ExportToPdf
- ExportToRtf
- ExportToSvg
- ExportToXls
上面的代码将生成的 Sankey 图图像的宽度设置为文档宽度,并将 Sankey 图导出为 PDF 文件:
C#
`sankeyDiagramControl1.OptionsPrint.SizeMode = DevExpress.XtraCharts.Printing.PrintSizeMode.Zoom;
sankeyDiagramControl1.ExportToPdf(“D://sankey.pdf”);
`
VB.NET
`sankeyDiagramControl1.OptionsPrint.SizeMode = DevExpress.XtraCharts.Printing.PrintSizeMode.Zoom
sankeyDiagramControl1.ExportToPdf(“D://sankey.pdf”)`