-1

I Would like to use the Live-Charts library for windows-form and visual studio 2015 in my VB.net code in order to implement a Cartesian chart, but I couldn't find any VB.net code sample.

could someone provide me a working vb.net code sample for Cartesian chart please.

Kazunobu

1 Answers1

2

I am using VB in Visual Studio 2017 and created a WPF-App.

I assume you have installed LiveCharts and LiveCharts.WPF in your Visual Studio project using the Nuget package manager. The example below plots a cartesian bar chart with two series. The values for the first series are entered statically in the code. The second series displays dynamic data calculated by a simple equation. This should get you going.

I created a WPF window with the following XAML code:

<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>

Then in the VB code-behind I added this (No "Imports" statements needed):

  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
Gawie
  • 46
  • 2