Create side and stacked bar in a same chart

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
Phoi San
Newbie
Newbie
Posts: 1
Joined: Tue Nov 22, 2011 12:00 am

Create side and stacked bar in a same chart

Post by Phoi San » Tue Jul 10, 2012 6:53 am

Hi,

I facing problem to create a bar chart with side bar and stacked bar together. Could you guide me how to do that?
My teechart .Net version is 4.1.2012.1033
Attachments
SampleBarChart.PNG
Here is the sample chart
SampleBarChart.PNG (19.94 KiB) Viewed 2891 times

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: Create side and stacked bar in a same chart

Post by Sandra » Thu Jul 12, 2012 12:17 pm

Hello Phoi San,

I have made a simple code for you that simulate a similar chart as you want, to do it I have used SubChartTool and a Chart:

Code: Select all

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

        }
        Steema.TeeChart.Tools.SubChartTool subchart1;
        Steema.TeeChart.TChart tChart2;
        private void InitializeChart()
        {
            subchart1 = new SubChartTool(tChart1.Chart);
            tChart2 = subchart1.Charts.AddChart("Chart0");
            InitializeSeries();
            CustomsizeCharts();
		}
        private void InitializeSeries()
        {
            Random rnd = new Random();
            for (int i = 0; i < 5; i++)
            {
                Steema.TeeChart.Styles.Bar bar = new Steema.TeeChart.Styles.Bar();
                if (i % 2 == 0)
                {
                    tChart2.Series.Add(bar);
                    for (int j = 1; j <= 8; j++)
                    {
                        bar.Add(j, rnd.Next(100),"Series"+i.ToString());
                        bar.SideMargins = false;
                        bar.VertAxis = VerticalAxis.Right;
                        bar.Marks.Style = MarksStyles.Value;
                    }
                }
                else
                {
                    tChart1.Series.Add(bar);
                    bar.FillSampleValues(1);
                    bar.Labels[0] = "Series" + i.ToString();
                    bar.SideMargins = false;
                    bar.VertAxis = VerticalAxis.Left;
                    bar.MultiBar = MultiBars.Stacked;
                    bar.Marks.Style = MarksStyles.Value;
                }

            }
              
            
        }
        private void CustomsizeCharts()
        {
            tChart1.Panel.MarginUnits = PanelMarginUnits.Pixels;
            tChart2.Panel.MarginUnits = PanelMarginUnits.Pixels;
            tChart1.Walls.Back.Visible = false;
            tChart2.Walls.Back.Visible = false;

            //tChart1
            tChart1.Aspect.View3D = false;
            tChart1.Legend.Visible = false;
            tChart1.Axes.Bottom.Labels.Style = AxisLabelStyle.PointValue;
            tChart1.Header.Visible = false;
            //tChart2(subChartTool1)
            tChart2.Aspect.View3D = false;
            tChart2.Chart.Panel.Gradient.Visible = false;
            tChart2.Chart.Panel.Color = Color.Transparent;
            tChart2.Header.Visible = false;
     
            tChart2.Axes.Bottom.SetMinMax(0, 8);
            tChart2.Axes.Bottom.Labels.Style = AxisLabelStyle.PointValue;
            //Draw Charts;
            tChart1.Draw();
            tChart2.Draw();

            //SubChart position.
            Rectangle rect = tChart1.Chart.ChartRect;
            tChart2.Left = tChart1.Left;
            tChart2.Top = rect.Top;
            tChart2.Width = rect.Right;
            tChart2.Height = tChart1.Height -rect.Top -4;
            //Draw
            tChart2.Draw();
            tChart2.Panel.MarginLeft = (tChart2.Axes.Bottom.CalcPosValue(1) - tChart2.Axes.Bottom.IStartPos);
            tChart2.Draw();
            //ChangeMarginPanel1
            tChart1.Panel.MarginRight = tChart2.Axes.Bottom.IEndPos - (tChart2.Axes.Bottom.CalcPosValue(1) - tChart2.Axes.Bottom.IStartPos);
           //ReCalculateAxes
            tChart2.Axes.Bottom.SetMinMax(1, 8);
            double max, min;
            min= Math.Min(tChart2.Axes.Right.Minimum, tChart1.Axes.Left.Minimum);
            max = Math.Max(tChart2.Axes.Right.Maximum, tChart1.Axes.Left.Maximum);
            tChart2.Axes.Right.Visible = false;
            tChart1.Axes.Left.SetMinMax(min,max);
            tChart2.Axes.Left.SetMinMax(min, max);
            tChart1.Axes.Left.Grid.Visible = false;
            //RecalculateMargins with values as you want.
            tChart1.Panel.MarginTop =20;
            tChart2.Panel.MarginTop = 20;
            tChart2.Panel.MarginRight = 30;
        //ReDraw All Charts.
            tChart1.Draw();
            tChart2.Draw();
          
        }
        private void button1_Click(object sender, EventArgs e)
        {
            tChart1.ShowEditor();
        }
Can you tell us if previous code works as you want?

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply