LiveCharts 库和 VB.net

LiveCharts library and VB.net

我想在我的 VB.net 代码中使用 windows-form 和 visual studio 2015 的实时图表库来实现笛卡尔图表,但我不能'找不到任何 VB.net 代码示例。

有人可以为我提供笛卡尔图表的有效 vb.net 代码示例吗?

和信

我在 Visual Studio 2017 年使用 VB 并创建了一个 WPF 应用程序。

我假设您已经使用 Nuget 包管理器在 Visual Studio 项目中安装了 LiveCharts 和 LiveCharts.WPF。下面的示例绘制了一个包含两个系列的笛卡尔条形图。第一个系列的值在代码中静态输入。第二个系列显示通过简单方程计算的动态数据。这应该让你继续。

我使用以下 XAML 代码创建了一个 WPF window:

<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 代码隐藏中我添加了这个(不需要 "Imports" 语句):

  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