Back to Devexpress

ChartControl.RefreshData() Method

windowsforms-devexpress-dot-xtracharts-dot-chartcontrol-05803472.md

latest7.3 KB
Original Source

ChartControl.RefreshData() Method

Reloads data from the underlying data source and repaints the diagram area.

Namespace : DevExpress.XtraCharts

Assembly : DevExpress.XtraCharts.v25.2.UI.dll

NuGet Package : DevExpress.Win.Charts

Declaration

csharp
public void RefreshData()
vb
Public Sub RefreshData

Remarks

If a data source implements the IBindingList, INotifyCollectionChanged or DevExpress.Data.ChartDataSources.IChartDataSource interface, XtraCharts refreshes a chart’s data only when data in the data source has been changed. If the data source does not implement these interfaces, you can manually handle the way in which chart data should be refreshed via the ChartControl.RefreshDataOnRepaint property.

Example

In the following code, a new data point is added to a data source when a user clicks the Add Point button. The Chart Control uses a data source of the List<T> type that does not implement previously mentioned interfaces. For this reason, you should call the RefreshData method to notify the chart about the data source changes and make the chart re-render the diagram area.

csharp
using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ChartExample {
    public partial class Form1 : Form {
        Random rand = new Random();
        List<DataPoint> data;
        public Form1() {
            InitializeComponent();
            data = GetPoints();
        }
        private void Form1_Load(object sender, EventArgs e) {
            Series series = new Series("Series 1", ViewType.Line);
            series.DataSource = data;
            series.ArgumentDataMember = "Argument";
            series.ValueDataMembers.AddRange("Value");
            chartControl1.Series.Add(series);

            XYDiagram diagram = chartControl1.Diagram as XYDiagram;
            diagram.AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Continuous;

            LineSeriesView view = (LineSeriesView)series.View;
            view.MarkerVisibility = DevExpress.Utils.DefaultBoolean.True;
        }

        private void OnAddPointButtonClick(object sender, EventArgs e) {
            data.Add(new DataPoint(DateTime.Now, rand.NextDouble() * 50));
            chartControl1.RefreshData();
        }

        public List<DataPoint> GetPoints() {
             return new List<DataPoint> {
                new DataPoint(DateTime.Now.AddSeconds(- 5), 56.1226),
                new DataPoint(DateTime.Now.AddSeconds(- 4), 50.18432),
                new DataPoint(DateTime.Now.AddSeconds(- 3), 51.51443),
                new DataPoint(DateTime.Now.AddSeconds(- 2), 60.2624),
                new DataPoint(DateTime.Now.AddSeconds(- 1), 65.6321)};

        }
        public class DataPoint {
            public DateTime Argument { get; set; }
            public double Value { get; set; }
            public DataPoint(DateTime argument, double value) {
                this.Argument = argument;
                this.Value = value;
            }
        }
    }
}
vb
Imports DevExpress.XtraCharts
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Namespace ChartExample
    Public Partial Class Form1
        Inherits Form
        Private rand As Random = New Random()
        Private data As List(Of DataPoint)
        Public Sub New()
            InitializeComponent()
            data = GetPoints()
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim series As Series = New Series("Series 1", ViewType.Line)
            series.DataSource = data
            series.ArgumentDataMember = "Argument"
            series.ValueDataMembers.AddRange("Value")
            chartControl1.Series.Add(series)
            Dim diagram As XYDiagram = TryCast(chartControl1.Diagram, XYDiagram)
            diagram.AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Continuous
            Dim view As LineSeriesView = CType(series.View, LineSeriesView)
            view.MarkerVisibility = DevExpress.Utils.DefaultBoolean.[True]
        End Sub
        Private Sub OnAddPointButtonClick(ByVal sender As Object, ByVal e As EventArgs)
            data.Add(New DataPoint(Date.Now, rand.NextDouble() * 50))
            chartControl1.RefreshData()
        End Sub
        Public Function GetPoints() As List(Of DataPoint)
            Return New List(Of DataPoint) From {
                New DataPoint(Date.Now.AddSeconds(-5), 56.1226),
                New DataPoint(Date.Now.AddSeconds(-4), 50.18432),
                New DataPoint(Date.Now.AddSeconds(-3), 51.51443),
                New DataPoint(Date.Now.AddSeconds(-2), 60.2624),
                New DataPoint(Date.Now.AddSeconds(-1), 65.6321)
            }
        End Function
        Public Class DataPoint
            Public Property Argument As Date
            Public Property Value As Double

            Public Sub New(ByVal argument As Date, ByVal value As Double)
                Me.Argument = argument
                Me.Value = value
            End Sub
        End Class
    End Class
End Namespace

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the RefreshData() method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-chart-make-points-in-a-chartcontrol-adjustable-interactively/CS/Form1.cs#L36

csharp
((ChartControl)sender).RefreshData();
lastY = e.HitInfo.HitPoint.Y;

winforms-chart-make-points-in-a-chartcontrol-adjustable-interactively/VB/Form1.vb#L32

vb
CType(sender, ChartControl).RefreshData()
lastY = e.HitInfo.HitPoint.Y

See Also

RefreshDataOnRepaint

ChartControl Class

ChartControl Members

DevExpress.XtraCharts Namespace