我想在我的 VB.net 代码中使用Windows-form和Visual Studio 2015的实时图表库来实现笛卡尔图,但我找不到任何 VB.net 代码示例。
有人可以为我提供一个笛卡尔图的工作 vb.net 代码示例吗?
和信
我在Visual Studio 2017中使用VB并创建了一个WPF-App。
我假设你已经使用Nuget包管理器在Visual Studio项目中安装了LiveCharts和LiveCharts.WPF。下面的示例绘制了一个包含两个序列的笛卡尔条形图。第一个系列的值在代码中静态输入。第二个系列显示通过简单方程计算的动态数据。这应该让你开始。
我使用以下 XAML 代码创建了一个 WPF 窗口:
<Window
x:Class="Mycolumn"
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:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<lvc:CartesianChart Name="Mychart" Series="{Binding MySeriesCollection}" LegendLocation="Left" Margin="25,29,21,10">
<lvc:CartesianChart.AxisX>
<lvc:Axis Title="Size" Labels="{Binding MyLabels}">
<lvc:Axis.Separator>
<lvc:Separator IsEnabled="False" Step="1"></lvc:Separator>
</lvc:Axis.Separator>
</lvc:Axis>
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis Title="Frequency" LabelFormatter="{Binding MyFormatter}"></lvc:Axis>
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
</Grid>
</Window>
然后在 VB 代码隐藏中我添加了这个(不需要"导入"语句(:
Public Class Mycolumn
'---Need to declare binding properties here so that XAML can find them---
'---Remember XAML is case sensitive---
Public Property MySeriesCollection As LiveCharts.SeriesCollection
Public Property MyLabels As New List(Of String)
Public Property MyFormatter As Func(Of Double, String)
Public Sub New()
InitializeComponent()
'---Create a seriescollection and add first series as a columnseries (index 0) and some static values to show---
'---The first series will show just 4 columns---
MySeriesCollection = New LiveCharts.SeriesCollection From {
New LiveCharts.Wpf.ColumnSeries With {
.Title = "Granite",
.Values = New LiveCharts.ChartValues(Of Double) From {
110,
350,
239,
550
}
}
}
'---Add a second columnseries(index 1) with nothing in it yet---
MySeriesCollection.Add(New LiveCharts.Wpf.ColumnSeries With {
.Title = "Marble",
.Values = New LiveCharts.ChartValues(Of Double)})
'---Now add some dynamic values to columnseries (1) - will show 10 columns of results ---
'---These values can come from a list or array of double calculated elsewhere in the program---
For i = 1 To 10
MySeriesCollection(1).Values.Add(CDbl(i + (2 * i) ^ 2))
Next
'---Add 10 labels to show on the x-axis---
For i = 1 To 10
MyLabels.Add(CStr(i))
Next
'---Define formatter to change double values on y-axis to string labels---
MyFormatter = Function(value) value.ToString("N")
DataContext = Me
End Sub
End Class