LiveCharts库和VB.net

时间:2018-03-22 04:03:04

标签: livecharts

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

请有人为我提供笛卡尔图表的工作vb.net代码示例。

Kazunobu

1 个答案:

答案 0 :(得分:1)

我在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代码隐藏中我添加了这个(不需要“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