Fill Linechart(Line chart) wpf with data from a sql server query

Asked

Viewed 365 times

1

Hello, I’m having trouble finding a solution to fill my linechart with data from a query, my connection class is complete and working, but I don’t know how to apply on Chart, I don’t know how to create automatically, I just enter values.

Currently my chart is powered by data entered via code-Behind:

Exemplo

Wpf:

        <chartingToolkit:Chart  Name="lineChart" Title="Series Demo" Margin="0,10,10,54">
        <chartingToolkit:LineSeries DependentValuePath="Value" 

    IndependentValuePath="Key" ItemsSource="{Binding}" 

    IsSelectionEnabled="True" />
    </chartingToolkit:Chart>

Code c#:

public partial class Charts : Window
{

    public Charts()
    {
        InitializeComponent();
        showColumnChart();
    }

    private void showColumnChart()
    {
        List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
        valueList.Add(new KeyValuePair<string, int>("oculos", 60));
        valueList.Add(new KeyValuePair<string, int>("lentes prontas", 200));
        valueList.Add(new KeyValuePair<string, int>("manutenção", 10));
        lineChart.DataContext = valueList;
    }
}

I have a table with these same values and would like to use them from the table in sql server, can add or remove later without changing the code.

1 answer

2


Try the following, extract the data from the database query p/ an Observablecollection(you will need to have the class of this query) or a List. Then you can create a foreach p/ go through and insert into Chart.

Browser other questions tagged

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