Fill LineChart (Line Chart) wpf with data from a sql server query

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 do not know how to apply it in the chart, I do not know how to create it automatically, I just insert values.

Currently my chart is powered by data entered through code-behind:

Wpf:

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

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

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

C # code:

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 the sql server, and then add or remove them without changing the code.

    
asked by anonymous 09.11.2017 / 18:36

1 answer

2

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

    
10.11.2017 / 20:38