Page 1 of 1

Stacked Bar for Horizontal

Posted: Sun Apr 08, 2018 3:44 am
by 16080748
would like to know to can I set horizontal stacked bar chart as attached image.
i cant seems to get the stacked to work correctly.

Re: Stacked Bar for Horizontal

Posted: Mon Apr 09, 2018 7:52 am
by Christopher
bpamit wrote:would like to know to can I set horizontal stacked bar chart as attached image.
i cant seems to get the stacked to work correctly.
Well, some code to help you get started would be something like this:

Code: Select all

		private void InitializeChart()
		{
			Random rnd = new Random();

			tChart1.Header.Text = "My Chart";

			for (int i = 0; i < 3; i++)
			{
				HorizBar series = new HorizBar(tChart1.Chart);
				series.Title = "My Horizontal " + (i + 1).ToString();

				for (int t = 0; t < 3; t++)
				{
					series.Add(rnd.Next(200), (t + 1).ToString() + "Y");
				}

				series.Marks.Style = MarksStyles.Value;
				series.MultiBar = MultiBars.Stacked;
				series.MarksOnBar = true;
			}

			tChart1.Axes.Left.Labels.Style = AxisLabelStyle.Text;
			tChart1.Axes.Left.Grid.Visible = false;
			tChart1.Axes.Bottom.Grid.Visible = true;
		}
which gives me:
2018-04-09_09-49-53.png
2018-04-09_09-49-53.png (12.14 KiB) Viewed 21632 times

Re: Stacked Bar for Horizontal

Posted: Tue Apr 10, 2018 7:08 am
by 16080748
Hi Christopher, thanks for the prompt reply. I managed to get the chart working.
But I having issue to set the chart to start at a specific value instead of 0.

Please refer to the attachment. Thanks.

Re: Stacked Bar for Horizontal

Posted: Mon Apr 23, 2018 11:03 am
by Christopher
Many apologies for the delay in response to this question - do you still need help in this area?

Re: Stacked Bar for Horizontal

Posted: Sun May 13, 2018 12:32 pm
by 16080748
Hi Christopher, Yes I still cannot figure it out. Thanks.

Re: Stacked Bar for Horizontal

Posted: Mon May 14, 2018 9:24 am
by Christopher
Hello!

Okay, well, taking your data set I've been able to come up with the following code:

Code: Select all

		private void InitializeChart()
		{
			double[] YearOne = new double[] { 2.145, 4.132, 4.409, 15.614 };
			double[] YearThree = new double[] { 3.632, 4.509, 4.8008, 7.162 };
			double[] YearFive = new double[] { 3.806, 4.478, 4.743, 5.941 };
			double[] YearSeven = new double[] { 4.105, 4.439, 4.729, 5.912 };
			double[] YearTen = new double[] { 4.124, 4.492, 4.773, 5.599 };
			string[] labels = new string[] { "1Y", "3Y", "5Y", "7Y", "10Y" };

			List<double[]> years = new List<double[]>() { YearOne, YearThree, YearFive, YearSeven, YearTen };

			void AddBar(int index)
			{
				HorizBar bar = new HorizBar(tChart1.Chart);
				bar.Title = "My Horizontal " + (index + 1).ToString();

				for (int i = 0; i < years.Count; i++)
				{
					bar.Add(years[i][index], labels[i]);
				}

				bar.MultiBar = MultiBars.Stacked;
				bar.Marks.Style = MarksStyles.Value;
				bar.MarksOnBar = true;
			}


			for (int i = 0; i < 4; i++)
			{
				AddBar(i);
			}

			tChart1.Axes.Left.Labels.Style = AxisLabelStyle.Text;
			tChart1.Axes.Left.Grid.Visible = false;
			tChart1.Axes.Bottom.Grid.Visible = true;
		}
This gives me the following chart:
2018-05-14_11-17-31.png
2018-05-14_11-17-31.png (21.87 KiB) Viewed 21520 times
I would suggest this chart is correct - take the "1Y" row - this correctly reflects the sum of the 1Y data (26.3) and also reflects the first data point accurately on the bar (2.145). All the rows (1Y->10Y) are correct in this sense also. Would you agree that this chart is correct? If so, what chart would you like to see if it is not this one?

Re: Stacked Bar for Horizontal

Posted: Mon May 21, 2018 11:57 am
by 16080748
Hi Christopher, chart is incorrect. I would like to start the chart for 1Y at 2.145, 3Y at 3.3632 and 5Y at 3.806. please refer to the first image. Your sample is all starting from 0.

Re: Stacked Bar for Horizontal

Posted: Mon May 21, 2018 12:22 pm
by Christopher
bpamit wrote:Hi Christopher, chart is incorrect. I would like to start the chart for 1Y at 2.145, 3Y at 3.3632 and 5Y at 3.806. please refer to the first image. Your sample is all starting from 0.
Yes, and this is correct if each point is a *value*.

Code: Select all

double[] YearOne = new double[] { 2.145, 4.132, 4.409, 15.614 };
so taking the value 2.145, the bar should end at this value, that is, the bar should end at 2.145, which is where it ends. The bar begins at zero precisely because 2.145 is a value, and all values start at zero.

Another, quite different, type of chart is where the points form a *range*. In these charts there are two points, the start point of the range and the end point of the range. So, for example, in the YearOne array above we could say there are two ranges:

2.145 -> 4.132
4.409 -> 15.614

or we could say there are three ranges:

2.145 -> 4.132
4.132 -> 4.409
4.409 -> 15.614

so the question is - are the points in the YearOne, YearTwo etc. arrays of my example *values* or *ranges*? The chart that my example drew is correct if the points are *values*. If the points are *ranges*, however, a completely different series type has to be used.

Re: Stacked Bar for Horizontal

Posted: Tue May 22, 2018 2:30 am
by 16080748
Hi Christopher, my bad. Now I understand what you mean. I would like to know which chart type to use if I need to draw bar in range.

Re: Stacked Bar for Horizontal

Posted: Wed May 23, 2018 9:32 am
by Christopher
bpamit wrote:Hi Christopher, my bad. Now I understand what you mean. I would like to know which chart type to use if I need to draw bar in range.
No problem - one way of drawing a horizontal range chart is to use the Gantt series, e.g.

Code: Select all

		private void InitializeChart()
		{
			double[] YearOne = new double[] { 2.145, 4.132, 4.409, 15.614 };
			double[] YearThree = new double[] { 3.632, 4.509, 4.8008, 7.162 };
			double[] YearFive = new double[] { 3.806, 4.478, 4.743, 5.941 };
			double[] YearSeven = new double[] { 4.105, 4.439, 4.729, 5.912 };
			double[] YearTen = new double[] { 4.124, 4.492, 4.773, 5.599 };
			string[] labels = new string[] { "1Y", "3Y", "5Y", "7Y", "10Y" };
			Color[] colors = new Color[] { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue };

			List<double[]> years = new List<double[]>() { YearOne, YearThree, YearFive, YearSeven, YearTen };

			Gantt bar = new Gantt(tChart1.Chart);
			bar.XValues.DateTime = false;
			bar.Pointer.VertSize = 20;
			bar.Marks.Visible = true;
			bar.Marks.Style = MarksStyles.Value;
			tChart1.Legend.Visible = false;
			tChart1.Axes.Left.MinimumOffset = 25;
			tChart1.Axes.Left.MaximumOffset = 25;
			tChart1.Axes.Bottom.Labels.Separation = 0;
			tChart1.Axes.Bottom.Increment = 1;

			void AddBar(int index)
			{
				double[] values = years[index];

				for (int i = 0; i < values.Length - 1; i++)
				{
					bar.Add(values[i], values[i + 1], index, labels[index], colors[i]);
				}
			}

			for (int i = 0; i < 5; i++)
			{
				AddBar(i);
			}
		}
2018-05-23_11-30-32.png
2018-05-23_11-30-32.png (12.86 KiB) Viewed 21483 times

Re: Stacked Bar for Horizontal

Posted: Mon May 28, 2018 3:24 am
by 16080748
Hi Christopher, thanks for the codes. It works.