Page 1 of 1

Legend position change

Posted: Mon Jun 08, 2009 5:05 am
by 13051032
My users like to have the ability to grab the legend and drop it in a position within the chart area and have the legend stick to that location (similar to MS Excel legends). Can you please suggest a way to achieve it?

Please note that all of my charts are code generated with legend click event opening the legend editor.

Thanks,

Posted: Mon Jun 08, 2009 8:16 am
by 10050769
Hello asupriya,


I made a simple example, it move legend around the Chart and the position that user wish. Please check next example works fine in your application.

InitializeChart:

Code: Select all

       Steema.TeeChart.Styles.Line line1;
        private void InitializeChart()
        {
            line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
            line1.FillSampleValues(5);
            moveLegend = false;
            tChart1.MouseDown += new MouseEventHandler(tChart1_MouseDown);
            tChart1.MouseMove += new MouseEventHandler(tChart1_MouseMove);
            tChart1.MouseUp += new MouseEventHandler(tChart1_MouseUp);
        }
MouseDown Event:

Code: Select all

    private bool moveLegend;

        void tChart1_MouseDown(object sender, MouseEventArgs e)
        {
            if (tChart1.Legend.Clicked(e.X, e.Y) != -1)
            {
                tChart1.Legend.CustomPosition = true;
                moveLegend = true;
            }
        }
MouseMove Event:

Code: Select all

        void tChart1_MouseMove(object sender, MouseEventArgs e)
        {
            if (moveLegend)
            {
                tChart1.Legend.Left = e.X;
                tChart1.Legend.Top = e.Y; 
            }
        }

MouseUp Event:

Code: Select all

        void tChart1_MouseUp(object sender, MouseEventArgs e)
        {
            moveLegend = false;
        }

I hope that will helps.


Thanks,

Posted: Tue Jun 09, 2009 2:13 am
by 13051032
Thanks for the code snippet.

I have a handler for ClickLegend as well and when the mouse down happens on the legend, its triggering the clicklegend event along with mousedown event. How can I define a legend doubleclick event so that legendclick even can be changed to double click.

Here is the code snippet I have

Code: Select all

AddHandler MyChart.ClickLegend, AddressOf MyChart_ClickLegend
            AddHandler MyChart.MouseMove, AddressOf MyChart_MouseMove
            AddHandler MyChart.MouseDown, AddressOf MyChart_MouseDown
            AddHandler MyChart.MouseUp, AddressOf MyChart_MouseUp
The event handlers are:

Code: Select all

Private Sub MyChart_ClickLegend(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim MyChart1 As Steema.TeeChart.Chart = CType(sender, Steema.TeeChart.Chart)

        Dim Index As Integer = MyChart1.Legend.Clicked(e.X, e.Y)
        Dim Series As Steema.TeeChart.Styles.FastLine

        If ((Index <> -1) And (MyChart1.Series.Count >= Index)) Then
            Series = CType(MyChart1.Series(Index), Steema.TeeChart.Styles.FastLine)
            Steema.TeeChart.Editors.PenEditor.Edit(Series.LinePen)            
        End If
    End Sub

dim moveLegend  as boolean = false
Private Sub MyChart_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        moveLegend = False
    End Sub

    Private Sub MyChart_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        activeChart = CType(sender, Steema.TeeChart.TChart)
        If activeChart.Legend.Clicked(e.X, e.Y) <> -1 Then
            activeChart.Legend.CustomPosition = True
            moveLegend = True
        End If
    End Sub

    Private Sub MyChart_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Try
            activeChart = CType(sender, Steema.TeeChart.TChart)

            'See if the legend is being moved
            If moveLegend Then
                activeChart.Legend.Left = e.X
                activeChart.Legend.Top = e.Y
            End If
end sub


Posted: Tue Jun 09, 2009 8:46 am
by 10050769
Hello asupriya,

If you want to use doubleClick event, because legends move when you do double click in the chart, needs next code in your application.

MouseDown event:

Code: Select all

    Private Sub TChart1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TChart1.MouseDown
        x = e.X
        y = e.Y

        If (moveLegend) Then
            moveLegend = False
        End If
    End Sub
MouseMove event:

Code: Select all

        Private Sub TChart1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TChart1.MouseMove
        If (moveLegend) Then
            TChart1.Legend.Left = e.X
            TChart1.Legend.Top = e.Y
        End If
    End Sub

MouseDoubleClick event:

Code: Select all

    Private Sub TChart1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TChart1.DoubleClick
        If (TChart1.Legend.Clicked(x, y)) Then
            TChart1.Legend.CustomPosition = True
            moveLegend = True
        End If
        x = -1
        y = -1

    End Sub
I hope that will helps,