Scaling Bar Chart Height With Data - c#

I am using the Microsoft Chart Control.
I am trying to adjust the height of the chart area programmatically. Depending on the rows. Because the Microsoft Chart control requires a height, which cannot be in percentages, nor Auto. Depending on the selected query, the chart can have 1 bar or it can have 100's. This is why I need to adjust the height because bigger data sets appear squished and not readable.
What I have tried is adding a counter heightCounter which is the number of bars on the chart.
int newHeight = (heightCounter * 10);
// Maximum height aloud on Chart Control
if (newHeight > 32767)
newHeight = 32767;
Chart1.Height = newHeight;
This sometimes works O.K. with smaller data sets
but when I have very large datasets the title looks very spaced out because the height is too large.
Is there a proper way to achieve this?

I think that the way you are approaching the problem is along the right lines, taking a count of the number of series' in the datasource in order to obtain a proportional height.
However, I would be inclined to declare the width of each bar as described here. This would give you more control than using a static multiplier such as the 10 you have.
I would also be inclined to set the height permanently as the calculated height, unless of course the minimum height is a desired feature.
Also, as a side note, isnt increasing by 10 for each quite significant? Have you tried halving that? If I remember rightly, the width of the bars will autmatically scale themselves (if not specifically declared by you).

Related

How to keep PointWidth fixed and add scrollbar in Range bar Chart

I have a gantt chart contrains tasks.
I declared the PointWidth as 0.25
GanttChart.Series["Tasks"]["PointWidth"] = "0.25";
this works good when I have few tasks but whenever I have more tasks the pointWidth (range) becomes smaller and smaller!
I want to keep it 0.25 and to put scrollbar when there are many tasks.
The CustomProperty PointWidth is in percent of the visible axis.
So with one point the bar or column is really fat and the more data points you add the thinner they will get.
If you want to keep a fixed width while changing the number of data points you need to use PixelPointWidth instead.
Note however that by default now the bars/columns get closer and closer to each other until the overlap.
To give them enough room and show scollbars instead, you need to enable the built-in zooming mechanism as MSChart will not use normal Scrollbars.
This should help:
Series s = chart1.Series[0];
s.SetCustomProperty("PixelPointWidth", "12"); // 12 pixels
var ca = chart1.ChartAreas[0];
ca.AxisX.ScrollBar.Enabled = true;
ca.AxisX.ScaleView.Size = 30; // show a value range of 30
chart1.Refresh(); // usally not needed, but we change a custom property
Note that the ScaleView.Size is in data values. This is the 3rd of the three coordinate system in the chart: percentages, pixels and values! Very powerful and rather tricky..

Dynamical rectangle plot c# wpf

I want to create a plot that dynamically displays active elements as rectangles. I have achieved a first version that is actually ok using OxyPlot.Annotations.RectangleAnnotation which I add to myPlotModel.Annotations, you can see it in the image hereafter:
Example of wanted display
The thing is that after a while, the amount of drawn rectangles make the update not smooth as I update the shown timewindow (which is set to 15 seconds). I have already set a maximum of drawn elements that suffice to cover the displayed window (i.e. the rectangles get removed as they are too far in the past), but the rendering is still jerky. I draw the rectangles by allocating them to an equal fraction of the Y-axis, that is the third one from the top gets:
rowNumber= 3.0
minimumY = maximalY - maximalY / totalElements * rowNumber
maximumY = maximalY - maximalY / totalElements * (rowNumber + 1.0)
And the Y-axis is hidden.
My question:
Is there a smarter way of creating such a display that would be less computationally heavy, and therefore allow a smoother update? I do not have to stick to OxyPlot, it is simply the easiest way that I found to obtain what I wanted.
Thanks for your answers!
Technically, the answer to your question is "Yes".
There are a number of ways to do this.
You could have a vertical itemscontrol that had an itemscontrol in it's template. That could have a canvas as it's itemspresenter and you could bind canvas.top and canvas.left to properties in it's content. Template each into a rectangle and bind height and width.
And of course do something about the scale on the bottom and the column of activity labels or whatever you want to call them there.
Unless you're using an absolutely ancient machine, that'd just fly.
It's quite a lot of work but it would probably be quicker to write that than to search through a load of alternative packages and decide which was optimal.

C# Grid - Get height of all children

I have stumbled upon a minor problem, which leaves me unable to continue my code.
I need to be able to recieve the accumulated height of all children in a grid.
For example: There are 3 children. Each of them has a height of 40, a Top Margin of 5 and a Bot Margin of 5. Which means their height is actually 50x3 = 150 together.
How can I check that height?
I need it for a part of the code:
_contentGrid.Height = ??? -> Height of all Children together
Why I want that does not matter. Better options do not matter either. I need to recieve this height somehow.
Thank you for your time.
Narakuvera
If they all have the same Height you should look at the ActualHeight-Property:
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.actualheight%28v=vs.110%29.aspx
_contentGrid.Height = (ActualHeight+10)*3;

Knowing width and height of grid where the amount of elements are known?

I've stumpled upon a problem that I can't solve. I am given the amount of elements to populate a grid with, but through this, I need to know (beforehand) what the width and height (in amount of elements) the grid should be.
I would like the aspect ratio between width and height of the grid to be 1:1. Even with this simple aspect ratio, I fail at doing the calculations.
Could someone help me?
Example provided below
Let's say I have 9 elements in total. The width and height of the grid would then be 3. This is easy (by using the square root of the amount of elements).
But what about (for instance) 10 elements, or 11? With 10 elements, I would like either the width or height of the grid to be 4. Same with 11.
How do I perform these calculations?
It all depends on how you want to handle non-square layouts. Either way, though, you can take the square root of the number of items, and then take either the floor or ceiling to get the desired width/height.
For example, Math.Sqrt(11) is ~3.3166, so Math.Ceiling(Math.Sqrt(11)) == 4, which is your desired height/width.

How to manipulate the LargeChange and SmallChange on a scrollbar to work

I have a number of panels in a single window in C# application and I created 2 scrollbars, one for horizontal and one vertical. This is what I currently have:
picture with 2 scroolbars http://www.simnet.is/elinnils52/scrollbar.jpg
I have 1 variable and that is the total height all the items take & need.
Here is my code on scroll change:
for (int i = 0; i < this._splitMainView.Panel2.Controls.Count; i++)
{
this._splitMainView.Panel2.Controls[i].Location = new Point(
3 - _scrollBarX.Value,
3 + (132 + 6) * (i - 2) - _scrollBarY.Value);
this._splitMainView.Panel2.Controls[i].Refresh();
}
The scrollbar maximum is the total amount of all the containers height, the space in between and a few pixels extra.
As you can see from the picture, it doesn't look good. Even if the maximum in this case is a little around 50 - 100 pixels it still looks like it's a thousand pages long. When I change the SmallChange and LargeChange, the scrollbar bar itself lengthens but then it wont reach the maximum pixels. It will be able to get almost at the end (depening on the SmallChange and LargeChange value) and leave around 5 - 29 px left. And as everyone knows, seeing half is not good.
Does anyone know how to overcome this obstacle or a better way to implement it?
Why not just make the maximum value of the scrollbar the overflow (visible area height - panel height)? Then just set the top of the panel to the value of the scrollbar * -1.
Is there a reason not to use the AutoScroll property?

Categories