Livechart - Fill Cartesian Chart with Sql Server Query Data

Asked

Viewed 223 times

2

I have a Datacontext set in a Cartesianchart which does not display the data... no error is displayed, just think I’m making wrong use of the Datacontext and I can’t fix it, I’m using the package Livechart-WPF

Xaml:

        <lvc:CartesianChart x:Name="Teste" DataContext="{Binding}" Hoverable="False" Foreground="Black">
        <lvc:CartesianChart.Series>
            <lvc:ColumnSeries Title="{Binding OS}" Values="14" MaxWidth="1000" ColumnPadding="0" Foreground="Black"/>
        </lvc:CartesianChart.Series>
        <lvc:CartesianChart.AxisX>
            <lvc:Axis Labels="{Binding DESCRICAO}" Foreground="Black">
                <lvc:Axis.Separator>
                    <lvc:Separator Step="1"></lvc:Separator>
                </lvc:Axis.Separator>
            </lvc:Axis>
        </lvc:CartesianChart.AxisX>
    </lvc:CartesianChart>

Codebehind:

    private void LoadChart()
    {
        strConexao = ConfigurationManager.ConnectionStrings["BellaContext"].ConnectionString;
        try
        {

            conn = new SqlConnection(strConexao);
            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT * FROM PRODUTOS", conn);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(dt);
            Teste.DataContext = dt.DefaultView;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
        finally
        {
            conn.Close();
            MessageBox.Show("Tudo Ok!");
        }
    }

1 answer

2


Thiago, try it this way:

XAML

  <Grid>
        <wpf:CartesianChart Series="{Binding SeriesCollection}" />
  </Grid>

C#

private void LoadChart()
{
    var strConexao = @"Data Source=localhost\sqlexpress2005; Initial Catalog=TestDB; Integrated Security=SSPI";
    try
    {
        using (SqlConnection conn = new SqlConnection(strConexao))
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT * FROM PRODUTOS", conn);

            var dr = comm.ExecuteReader();
            while (dr.Read())
            {
                os.Add(Convert.ToDouble(dr["OS"]));
            }

            SeriesCollection = new SeriesCollection
            {
                new LineSeries
                {
                    Values = new ChartValues<double>(os)
                }
            };
            DataContext = this;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

State also:

public SeriesCollection SeriesCollection { get; set; }
public string[] Labels { get; set; }
private List<double> os = new List<double>();
private List<double> description = new List<double>();

It was missing the Description that you add as Label.

inserir a descrição da imagem aqui

Take a look at their website, some information I got from there:

https://lvcharts.net/App/examples/v1/wpf/Basics

Ah, I removed the Finally and I put the clause using, looks more elegant and better :)

I put the example on Github:

https://github.com/thiagoloureiro/LiveChartExample

  • 1

    Totally correct, thank you for the reply.

  • would have an example of the use of titles and subtitles?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.