Search code examples
wpfdata-bindinglivecharts

Refreshing Live Chart on button click event


I have Live Chart that I am trying to perform a fresh of the values on Button_Click event but the Chart is not refreshing.

I have two TextBoxes where the user can select the start and end date they would like to review and use the butn_ExecuteQuery_Click to display the data.

    public HBDBreakdown()
    {
        InitializeComponent();
        ChartValues();
    }

    private void ChartValues()
    {
        try
        {
            // Defines the variable for differnt lines.
            List<double> SmallCommercialIndustValues = new List<double>();
            List<double> ResidentialValues = new List<double>();
            List<string> AnalystName = new List<string>();

            SqlConnection connection = new SqlConnection("Data Source=WINDOWS-B1AT5HC\\MSSQLSERVER2;Initial Catalog=CustomerRelations;user id=sa; password=Westside2$; Integrated Security=False;");

            string selectQuery = ("SELECT Users.TX_EMPLOYEE, SUM(CASE WHEN d .REV_CLS <> 2 THEN 1 ELSE 0 END) AS Residential, SUM(CASE WHEN d .REV_CLS = 2 THEN 1 ELSE 0 END) AS SmallCommercialIndust FROM hb_Disputes AS d INNER JOIN Users ON d.ASSGNTO = Users.KY_USER_ID WHERE(d.OPENED >=@OPENED) AND(d.OPENED < @CLOSED) GROUP BY Users.TX_EMPLOYEE; ");
            connection.Open();

            SqlCommand command = new SqlCommand(selectQuery, connection);

            command.Parameters.Add("@OPENED", SqlDbType.DateTime).Value = dtepicker_Open.Text;
            command.Parameters.Add("@CLOSED", SqlDbType.DateTime).Value = dtepicker_DateResolved.Text;

            SqlDataReader sqlReader = command.ExecuteReader();

            while (sqlReader.Read())
            {
                // Check for DBNull and then assign the variable
                if (sqlReader["SmallCommercialIndust"] != DBNull.Value)
                    SmallCommercialIndustValues.Add(Convert.ToInt32(sqlReader["SmallCommercialIndust"]));

                // Check for DBNull and then assign the variable
                if (sqlReader["Residential"] != DBNull.Value)
                    ResidentialValues.Add(Convert.ToInt32(sqlReader["Residential"]));

                // Check for DBNull and then assign the variable
                AnalystName.Add(Convert.ToString(sqlReader["TX_EMPLOYEE"]));
            }


            SeriesCollection = new SeriesCollection
           {
            new StackedColumnSeries
            {
                Title = "Residential",
                Values = new ChartValues<double>(ResidentialValues),
                StackMode = StackMode.Values, // this is not necessary, values is the default stack mode
                DataLabels = true
            },
            new StackedColumnSeries
            {
                Title = "Small Commercial Indust",
                Values = new ChartValues<double>(SmallCommercialIndustValues),
                StackMode = StackMode.Values,
                DataLabels = true
            }
            };

            Labels = AnalystName.ToArray();
            //Formatter = value => value + " Disputes";

           DataContext = this;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    public SeriesCollection SeriesCollection { get; set; }
    public string[] Labels { get; set; }
    public Func<double, string> Formatter { get; set; }

    private void butn_ExecuteQuery_Click(object sender, RoutedEventArgs e)
    {
        // Refresh Chart
        ChartValues();
    }

Solution

  • Set DataContext = null; before you call ChartValues() or implement INotifyPropertyChanged and raise the PropertyChanged event from the setters of the source properties.