Page 1 of 3

legend with checkbox style

Posted: Tue Jun 02, 2009 11:10 am
by 13045482
Hi
I have a bar chart which has a legend with labels and respective checkboxes(tcStraAssessRC.Legend.CheckBoxes = True). so if i unclick some checkbox then that bar become invisible and vice versa. Now i want if user clicks the legend label checkbox then the bar should become invisible BUT the label associated with that bar in that chart should continue to remain visible. is that possible?

Posted: Tue Jun 02, 2009 12:20 pm
by yeray
Hi shikha,

I'm afraid that there isn't an easy way (or property) to do that. On the other hand you could do as follows to achieve this effect:

Code: Select all

public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }

        Color[] Colors;
        Boolean[] SeriesVisible;

        private void InitializeChart()
        {
            tChart1.Aspect.View3D = false;

            Colors = new Color[4];
            SeriesVisible = new Boolean[4];

            for (int i = 0; i < Colors.Length; i++)
            {
                new Bar(tChart1.Chart);
                tChart1[i].FillSampleValues(5);
                Colors[i] = tChart1[i].Color;
                SeriesVisible[i] = true;
            }
            
            tChart1.Legend.CheckBoxes = true;
            tChart1.ClickLegend += new MouseEventHandler(tChart1_ClickLegend);
            tChart1.AfterDraw += new Steema.TeeChart.PaintChartEventHandler(tChart1_AfterDraw);
        }

        void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
            int SymbolTop, SymbolLeft, SymbolBottom, SymbolRight;
            int SymbolsTopMargin = 4;
            int SymbolsLeftMargin = 5;
            int SymbolsSize = 12;
            int SymbolsSeparation = 17;

            for (int i = 0; i < tChart1.Series.Count; i++)
            {
                if (tChart1[i].Color == Color.Transparent)
                {
                    SymbolLeft = tChart1.Legend.Left + SymbolsLeftMargin;
                    SymbolRight = tChart1.Legend.Left + SymbolsLeftMargin + SymbolsSize;
                    SymbolTop = tChart1.Legend.Top + (i * SymbolsSeparation) + SymbolsTopMargin;
                    SymbolBottom = tChart1.Legend.Top + (i * SymbolsSeparation) + SymbolsSize + SymbolsTopMargin;
                    tChart1.Graphics3D.Brush.Color = Color.White;
                    tChart1.Graphics3D.Pen.Color = Color.Gray;
                    tChart1.Graphics3D.Rectangle(tChart1.Legend.Left + SymbolsLeftMargin,
                                                tChart1.Legend.Top + (i * SymbolsSeparation) + SymbolsTopMargin,
                                                tChart1.Legend.Left + SymbolsLeftMargin + SymbolsSize,
                                                tChart1.Legend.Top + (i * SymbolsSeparation) + SymbolsSize + SymbolsTopMargin);
                }
            } 
        }

        void tChart1_ClickLegend(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < tChart1.Series.Count; i++)
			{
                if (!tChart1[i].Active)
                {
                    tChart1[i].Active = true;
                    SeriesVisible[i] = !SeriesVisible[i];
                }
                if (SeriesVisible[i])
                {
                    tChart1[i].Color = Colors[i];
                }
                else
                {
                    tChart1[i].Color = Color.Transparent;
                }
			}
                
        }

Posted: Wed Jun 03, 2009 10:13 am
by 13045482
thanks so much. can you give me the same in VB.net?

Posted: Wed Jun 03, 2009 10:33 am
by 13045482
also i think in this case the legend ( the color in legend on left side of the legendkey)will also become transparent along with bar, right? any way to avoid that?

Posted: Wed Jun 03, 2009 12:03 pm
by yeray
Hi shikha,

As NarcĂ­s said here you can use the following conversors:
Kamal Patel's conversor
Carlos Aguilar's conversor

Regarding the legend symbols, you could force the symbols colors, drawing over them the according rectangle. In your case, simply add the following into the for loop at tChart1_AfterDraw metod:

Code: Select all

tChart1.Graphics3D.Brush.Color = Colors[i];
tChart1.Graphics3D.Pen.Color = Color.Gray;
tChart1.Graphics3D.Rectangle(tChart1.Legend.Items[i].SymbolRectangle);

Posted: Tue Jun 09, 2009 9:27 am
by 13045482
HEY thanks a tonne..just one thing left now.. i actually have the legend on bottom rather tahn right side of my bar chart. so using this code , the second item on my legend when clicked , craetes the empty rectangle below the first itenm checkbox and so on( as this code is for right side legend). can u please tell me how to do for this.. thanx in advance..

Posted: Tue Jun 09, 2009 10:49 am
by 13045482
its a bit urgent.. a swift reply would be really appreciated.. tahnx in advance

Posted: Tue Jun 09, 2009 11:55 am
by yeray
Hi shikha,

I've seen that the code I made before was easy to improve calculating the checkboxes rectangle positions from the legend symbol square instead of legend rectangle:

Code: Select all

        public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }

        Color[] Colors;
        Boolean[] SeriesVisible;

        private void InitializeChart()
        {
            tChart1.Aspect.View3D = false;

            Colors = new Color[4];
            SeriesVisible = new Boolean[4];

            for (int i = 0; i < Colors.Length; i++)
            {
                new Bar(tChart1.Chart);
                tChart1[i].FillSampleValues(5);
                Colors[i] = tChart1[i].Color;
                SeriesVisible[i] = true;
            }

            tChart1.Legend.CheckBoxes = true;
            tChart1.Legend.Alignment = Steema.TeeChart.LegendAlignments.Bottom;
            tChart1.ClickLegend += new MouseEventHandler(tChart1_ClickLegend);
            tChart1.AfterDraw += new Steema.TeeChart.PaintChartEventHandler(tChart1_AfterDraw);
        }

        void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
            int CheckBoxTop, CheckBoxLeft, CheckBoxBottom, CheckBoxRight;
            int CheckBoxSize = 12;
            int SymbolMargin = 20;

            tChart1.Graphics3D.Pen.Color = Color.Gray;

            for (int i = 0; i < tChart1.Series.Count; i++)
            {
                if (tChart1[i].Color == Color.Transparent)
                {
                    CheckBoxLeft = tChart1.Legend.Items[i].Left - tChart1.Legend.Items[i].SymbolRectangle.Width - SymbolMargin;
                    CheckBoxRight = CheckBoxLeft + CheckBoxSize;
                    CheckBoxTop = tChart1.Legend.Items[i].Top + 2;
                    CheckBoxBottom = CheckBoxTop + CheckBoxSize;

                    tChart1.Graphics3D.Brush.Color = Color.White;
                    tChart1.Graphics3D.Rectangle(CheckBoxLeft, CheckBoxTop, CheckBoxRight, CheckBoxBottom);
                }
                tChart1.Graphics3D.Brush.Color = Colors[i];
                tChart1.Graphics3D.Rectangle(tChart1.Legend.Items[i].SymbolRectangle);
            }
        }

        void tChart1_ClickLegend(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < tChart1.Series.Count; i++)
            {
                if (!tChart1[i].Active)
                {
                    tChart1[i].Active = true;
                    SeriesVisible[i] = !SeriesVisible[i];
                }
                if (SeriesVisible[i])
                {
                    tChart1[i].Color = Colors[i];
                }
                else
                {
                    tChart1[i].Color = Color.Transparent;
                }
            }
        }

Posted: Tue Jun 09, 2009 12:06 pm
by 13045482
EXCELLENT...This was really fantastic. IT WORKED PERFECTLY!!Thanks so much for your help.

Re: legend with checkbox style

Posted: Fri Jun 12, 2009 11:25 am
by 13045482
Hi
Just got one issue with this- sorry did not figure it out earlier-
As you know i am currently using
barSeriesBase.MultiBar = Steema.TeeChart.Styles.MultiBars.Stacked

WHOLE CODE-
************************************************************
Public Class teechart_legend_label_issue

Private Colors As Color()
Private SeriesVisible As [Boolean]()
Public Sub New()
InitializeComponent()
InitializeChart()
End Sub


Private Sub InitializeChart()
tChart1.Aspect.View3D = False

Colors = New Color(2) {}
SeriesVisible = New [Boolean](5) {}
Dim barSeriesBase As Steema.TeeChart.Styles.Bar
Dim barSeriesIncrement As Steema.TeeChart.Styles.Bar
Dim bsDecrement As Steema.TeeChart.Styles.Bar

barSeriesBase = New Steema.TeeChart.Styles.Bar
'barSeriesBase.Marks.Visible = False
'barSeriesBase.Title = "Preserved Existing Space"
'barSeriesBase.Add(6, 7, "jj")
'barSeriesIncrement = New Steema.TeeChart.Styles.Bar
'barSeriesIncrement.Title = "Increase Space"
'barSeriesIncrement.Marks.Visible = False
'barSeriesIncrement.Add(9, 4, "kk")
'bsDecrement = New Steema.TeeChart.Styles.Bar
'bsDecrement.Title = "Reduced Space"
'bsDecrement.Marks.Visible = False


' barSeriesBase.MultiBar = Steema.TeeChart.Styles.MultiBars.Stacked

'TChart1.Series.Add(barSeriesBase)
'TChart1.Series.Add(bsDecrement)
'TChart1.Series.Add(barSeriesIncrement)

'TChart1.Series.Add(New Steema.TeeChart.Styles.Bar())

'barSeriesBase.Add(123, "AB")

barSeriesBase.Add(123, "A")
barSeriesBase.Add(456, "dr")
barSeriesBase.Add(321, "rea")

TChart1.Series.Add(barSeriesBase)
'TChart1.Series(0).Clear()
'TChart1.Series(0).Add(123, "AB")
'TChart1.Series(0).Add(456, "dr")
'TChart1.Series(0).Add(321, "rea")
barSeriesBase.MultiBar = Steema.TeeChart.Styles.MultiBars.Stacked
TChart1.Series.Add(New Steema.TeeChart.Styles.Bar())
TChart1.Series(1).Clear()
TChart1.Series(1).Add(221, "cd")
TChart1.Series(1).Add(456, "bg")
TChart1.Series(1).Add(321, "rea")
TChart1.Series.Add(New Steema.TeeChart.Styles.Bar())
TChart1.Series(2).Clear()
TChart1.Series(2).Add(100, "csd")
TChart1.Series(2).Add(456, "bsg")
TChart1.Series(2).Add(321, "resa")
For i As Integer = 0 To Colors.Length - 1
Colors(i) = TChart1(i).Color
SeriesVisible(i) = True
Next

TChart1.Legend.CheckBoxes = True
TChart1.Legend.LegendStyle = Steema.TeeChart.LegendStyles.Series


AddHandler tChart1.ClickLegend, AddressOf tChart1_ClickLegend
AddHandler tChart1.AfterDraw, AddressOf tChart1_AfterDraw
End Sub

Private Sub tChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D)
Dim SymbolTop As Integer, SymbolLeft As Integer, SymbolBottom As Integer, SymbolRight As Integer
Dim SymbolsTopMargin As Integer = 4
Dim SymbolsLeftMargin As Integer = 5
Dim SymbolsSize As Integer = 12
Dim SymbolsSeparation As Integer = 17

For i As Integer = 0 To TChart1.Series.Count - 1
If TChart1(i).Color = Color.Transparent Then
SymbolLeft = TChart1.Legend.Left + SymbolsLeftMargin
SymbolRight = TChart1.Legend.Left + SymbolsLeftMargin + SymbolsSize
SymbolTop = TChart1.Legend.Top + (i * SymbolsSeparation) + SymbolsTopMargin
SymbolBottom = TChart1.Legend.Top + (i * SymbolsSeparation) + SymbolsSize + SymbolsTopMargin
TChart1.Graphics3D.Brush.Color = Color.White
TChart1.Graphics3D.Pen.Color = Color.Gray
TChart1.Graphics3D.Rectangle(TChart1.Legend.Left + SymbolsLeftMargin, TChart1.Legend.Top + (i * SymbolsSeparation) + SymbolsTopMargin, TChart1.Legend.Left + SymbolsLeftMargin + SymbolsSize, TChart1.Legend.Top + (i * SymbolsSeparation) + SymbolsSize + SymbolsTopMargin)
End If
Next
End Sub

Private Sub tChart1_ClickLegend(ByVal sender As Object, ByVal e As MouseEventArgs)
For i As Integer = 0 To TChart1.Series.Count - 1
If Not TChart1(i).Active Then
TChart1(i).Active = True
SeriesVisible(i) = Not SeriesVisible(i)
End If
If SeriesVisible(i) Then
TChart1(i).Color = Colors(i)
Else
TChart1(i).Color = Color.Transparent
End If

Next
End Sub



End Class

******************************************************
Now the only issue is that when i unclick the checkbox of the Bar1, then it disappears but the bars of bar2 and bar3 keep hanging instead of moving down to x axis.Is there a workaround for this . tahnks.

Re: legend with checkbox style

Posted: Fri Jun 12, 2009 2:43 pm
by yeray
Hi shikha,

I'm not sure to understand you, could you please show us the problem in a picture?

Re: legend with checkbox style

Posted: Fri Jun 12, 2009 7:25 pm
by 13045482
Hi
I have uploaded screenshots of my problem with description(screenshot_barchart_issue.doc) at the teechart upload page(http://www.steema.net/upload/Default.aspx).Hope that helps.Thanks.

Re: legend with checkbox style

Posted: Mon Jun 15, 2009 8:48 am
by 13045482
Hi
its a bit urgent, will appreciate swift reply on this .. thanks a tonne..

Re: legend with checkbox style

Posted: Mon Jun 15, 2009 11:09 am
by yeray
Hi shikha,

Excuse me, I haven't seen in your code from 12th June that you are working with Stacked bars. I'm afraid that this changes everything. Even more, I don't understand how would you like the chart to appear. I guess that you would like the bars to appear like in the default way. Like in the simple following code:

Code: Select all

        private void InitializeChart()
        {
            tChart1.Aspect.View3D = false;

            for (int i = 0; i < 4; i++)
            {
                new Bar(tChart1.Chart);
            }

            (tChart1[0] as Bar).MultiBar = MultiBars.Stacked;

            tChart1.Legend.CheckBoxes = true;
        }
But then you would like the hided marks to appear, isn't it? But where? Note that the chart left axis has been rescaled in order to fit well the visible series.
Another picture showing us the desired result would be helpful.

Re: legend with checkbox style

Posted: Mon Jun 15, 2009 11:58 am
by 13045482
yes i m using Stacked bars( its in my code i posted in july 12th)..sorry i missed to tell earlier.I have posted another file which will display the expected result. Also i do not want the left axis to change( i dont think there is a need to change?).