LiveChart - Fill Cartesian Graph with Query Data Sql Server

2
I have a DataContext set in a CartesianChart that does not display data ... no error is displayed, just guess I am making wrong use of the DataContext and I can not fix it, I'm using the LiveChart-WPF package

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!");
        }
    }
    
asked by anonymous 13.12.2017 / 22:46

1 answer

2

Thiago, try this:

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);
    }
}

Declare 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>();

The Description you added as Label was missing.

Takealookattheirsite,someinformationIgotfromthere:

link

Oh, I removed Finally and I put the using clause, it gets more elegant and better:)

I put the example in GitHub :

link

    
14.12.2017 / 11:18