FinancialChart for WinForms
範囲セレクタ
ユーザー操作機能 > 範囲セレクタ

FinancialChart の RangeSelector を使用すると、ユーザーが特定の範囲のデータを選択してチャートに表示できます。さまざまなタイプの株価チャートに簡単に RangeSelector を連結できます。これは主に金融業界で、さまざまなデータ範囲に対して株価の分析を実行するために使用されています。

RangeSelector には左スクロールボックス(最小値用)と右スクロールボックス(最大値用)があり、チャートの特定の時間をスクロールできます。ユーザーは RangeSelector の最小値と最大値を変更し、これらのスクロールボックスを左側や右側にドラッグして、チャート内でのデータの表示範囲を調整できます。範囲バーでスクロールボックスを左にドラッグすると値が減少し、右にドラッグする範囲バーの値が増加します。

RangeSelector を使用するには、最初にコードで RangeSelector クラスを含む名前空間 C1.Win.Chart.RangeSelector を作成する必要があります。次に、RangeSelector を使用するために、RangeSelector クラスのインスタンスを作成します。

次のコードスニペットは、アプリケーションで RangeSelector を使用する方法を具体的に示します。

Public Class DataService

    Public Class DataService
        Private _companies As New List(Of Company)()

        Public Sub New()
            _companies.Add(New Company() With {
            .Symbol = "box",
            .Name = "Box Inc"
        })
            _companies.Add(New Company() With {
            .Symbol = "fb",
            .Name = "Facebook"
        })
        End Sub

        Public Function GetCompanies() As List(Of Company)
            Return _companies
        End Function

        Public Function GetSymbolData(symbol As String) As List(Of Quote)
            Dim path As String = String.Format("FinancialChartExplorer.
            Resources.{0}.json", symbol)
            Dim stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path)
            Dim ser = New System.Runtime.Serialization.Json
            .DataContractJsonSerializer(GetType(Quote()))
            Dim data = DirectCast(ser.ReadObject(stream), Quote())
            Return data.ToList()
        End Function

        Shared _ds As DataService
        Public Shared Function GetService() As DataService
            If _ds Is Nothing Then
                _ds = New DataService()
            End If
            Return _ds
        End Function
    End Class

    Public Class Quote
        Public Property [date]() As String
            Get
                Return m_date
            End Get
            Set
                m_date = Value
            End Set
        End Property
        Private m_date As String
        Public Property high() As Double
            Get
                Return m_high
            End Get
            Set
                m_high = Value
            End Set
        End Property
        Private m_high As Double
        Public Property low() As Double
            Get
                Return m_low
            End Get
            Set
                m_low = Value
            End Set
        End Property
        Private m_low As Double
        Public Property open() As Double
            Get
                Return m_open
            End Get
            Set
                m_open = Value
            End Set
        End Property
        Private m_open As Double
        Public Property close() As Double
            Get
                Return m_close
            End Get
            Set
                m_close = Value
            End Set
        End Property
        Private m_close As Double
        Public Property volume() As Double
            Get
                Return m_volume
            End Get
            Set
                m_volume = Value
            End Set
        End Property
        Private m_volume As Double
    End Class

    Public Class Company
        Public Property Symbol() As String
            Get
                Return m_Symbol
            End Get
            Set
                m_Symbol = Value
            End Set
        End Property
        Private m_Symbol As String
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
    End Class

End Class
public class DataService
{
    List<Company> _companies = new List<Company>();

    public DataService()
    {
        _companies.Add(new Company() { Symbol = "box", Name = "Box Inc" });
        _companies.Add(new Company() { Symbol = "fb", Name = "Facebook" });
    }

    public List<Company> GetCompanies()
    {
        return _companies;
    }

    public List<Quote> GetSymbolData(string symbol)
    {
        string path = string.Format("FinancialChartExplorer.Resources.{0}.json", symbol);
        var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
        var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Quote[]));
        var data = (Quote[])ser.ReadObject(stream);
        return data.ToList();
    }

    static DataService _ds;
    public static DataService GetService()
    {
        if (_ds == null)
            _ds = new DataService();
        return _ds;
    }
}

public class Quote
{
    public string date { get; set; }
    public double high { get; set; }
    public double low { get; set; }
    public double open { get; set; }
    public double close { get; set; }
    public double volume { get; set; }
}

public class Company
{
    public string Symbol { get; set; }
    public string Name { get; set; }
}
Partial Public Class RangeSelector
    Inherits UserControl
    Private rs As C1.Win.Chart.RangeSelector.RangeSelector

    Public Sub New()
        InitializeComponent()

        Dim dataService__1 = DataService.GetService()
        Dim data = dataService__1.GetSymbolData("fb")

        financialChart1.BeginUpdate()
        financialChart1.BindingX = "date"
        financialChart1.Binding = "high,low,open,close,volume"
        financialChart1.ToolTip.Content = "Date: {date}" & vbLf & "Open: {open}"
 & vbLf & "High: {high}" & vbLf & "Low: {low}" & vbLf & "Close: {close}"
        financialChart1.Series.Add(New FinancialSeries())
        financialChart1.ChartType = C1.Chart.Finance.FinancialChartType.Candlestick
        financialChart1.DataSource = data
        financialChart1.EndUpdate()

        financialChart2.BeginUpdate()
        financialChart2.BindingX = "date"
        financialChart2.Binding = "close"
        financialChart2.Series.Add(New FinancialSeries())
        financialChart2.ChartType = C1.Chart.Finance.FinancialChartType.Line
        financialChart2.DataSource = data
        financialChart2.ToolTip.Content = Nothing

        financialChart2.EndUpdate()
    End Sub

    Private Sub RangeSelector_Load(sender As Object, e As EventArgs)
        AddHandler financialChart2.Rendered, AddressOf financialChart2_Rendered
    End Sub

    Private Sub financialChart2_Rendered(sender As Object, 
e As C1.Win.Chart.RenderEventArgs)
        If rs Is Nothing AndAlso financialChart2.AxisX.ActualMin <> 
financialChart2.AxisX.ActualMax Then
            rs = New C1.Win.Chart.RangeSelector.RangeSelector(financialChart2)
            AddHandler rs.ValueChanged, AddressOf rs_ValueChanged
        End If
    End Sub

    Private Sub rs_ValueChanged(sender As Object, e As EventArgs)

        financialChart1.AxisX.Min = rs.LowerValue
        financialChart1.AxisX.Max = rs.UpperValue
    End Sub
End Class
public partial class RangeSelector : UserControl
{
    C1.Win.Chart.RangeSelector.RangeSelector rs;

    public RangeSelector()
    {
        InitializeComponent();

        var dataService = DataService.GetService();
        var data = dataService.GetSymbolData("fb");

        financialChart1.BeginUpdate();
        financialChart1.BindingX = "date";
        financialChart1.Binding = "high,low,open,close,volume";
        financialChart1.ToolTip.Content = 
        "Date: {date}\nOpen: {open}\nHigh: {high}\nLow: {low}\nClose: {close}";
        financialChart1.Series.Add(new FinancialSeries());
        financialChart1.ChartType = C1.Chart.Finance.FinancialChartType.Candlestick;
        financialChart1.DataSource = data;
        financialChart1.EndUpdate();

        financialChart2.BeginUpdate();
        financialChart2.BindingX = "date";
        financialChart2.Binding = "close";
        financialChart2.Series.Add(new FinancialSeries());
        financialChart2.ChartType = C1.Chart.Finance.FinancialChartType.Line;
        financialChart2.DataSource = data;
        financialChart2.ToolTip.Content = null;
        financialChart2.EndUpdate();

    }

    private void RangeSelector_Load(object sender, EventArgs e)
    {
        financialChart2.Rendered += financialChart2_Rendered;
    }

    void financialChart2_Rendered(object sender, C1.Win.Chart.RenderEventArgs e)
    {
        if (rs == null && financialChart2.AxisX.ActualMin != financialChart2.AxisX.ActualMax)
        {
            rs = new C1.Win.Chart.RangeSelector.RangeSelector(financialChart2);
            rs.ValueChanged += rs_ValueChanged;
        }
    }

    void rs_ValueChanged(object sender, EventArgs e)
    {

        financialChart1.AxisX.Min = rs.LowerValue;
        financialChart1.AxisX.Max = rs.UpperValue;
    }
}