How to set autosize font in zedgraph - c#

I'm using ZedGraph in my c# project.
My X axis has text labels (used for bar chart), but with the default setting of XAxis.Scale.IsPreventLabelOverlap = true every second label is missing. When I change it to false with XAxis.Scale.MajorStep = 1 every label is shown, but font size remains the same, and labels overlap.
Is there any way to change font size of labels ? or preferably switch it to autosize ?

I hate to let you know but as far as I know you cannot change the axis label font size directly. You can change the axis title font size, but not the labels themselves. You can change whether or not they autosize though and scale at which they autosize and it seems that is sort of what you want and that may end up helping you. This is the resource at which I was looking.
Set the PaneBase.IsFontsScaled property to true and then you can change the scale factor by using the PaneBase.ScaleFactor() method. Look through that resource I linked and I think you will be able to get it done. I don't have ZedGraph installed so I can't test it but I'm sure it will be something like that.
Good luck!

my solution is ;
curve.Label.FontSpec = zg1.GraphPane.Legend.FontSpec.Clone();
curve.Label.FontSpec.Size = 6;

I've forgotten about this question long ago.
I've found my own solution, which isn't so clean. I've rewritten the PaneBase.CalcScaleFactor() method by changing return scaleFactor; to something like return scaleFactor * 0.75f;. Now it works as it should.

Related

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.

Get number of rows of a text

I have a string that can be anything and I have a text object in my scene which has a set width and a changeable height.
Is there a built-in function that counts how many rows my string would have in that text object so that I can adjust the height of it? I want to do this because I need a background only at the text and not anywhere else.
I want to do this because I need a background only at the text and not anywhere else.
It appears to me that this should be able to solve your problem:
In order to make a Rect Transform with a Text component on it fit the text content, add a Content Size Fitter component to the same Game Object which has the Text component. Then set both the Horizontal Fit and Vertical Fit dropdowns to the Preferred setting.
In your case you want to control the Horizontal but not the vertical, so try set the Vertical setting to "Preferred".
If this does not work directly on your text component you may want to make the text component a child of the grey panel box and then add the contentfitter to it as well.
Source: https://docs.unity3d.com/Manual/HOWTO-UIFitContentSize.html
Disclaimer: I didn't test this Component based attempt, but I believe it may be the solution or lead you closer to one.
While Doh09's answer is certainly accurrate, you can also query the UI.Text.preferredHeight property, asking the Text object how tall the text is and using that value to change the size of your background object yourself.
Of course, both answers assume you are using the New UI text and not TextMesh or OnGUI. If you are not using the New UI, then I suggest changing to using the New UI.
You can see an example usage of this in my own project here where I compute the text's desired width and height and modify the RectTransform until the object's preferred size is within a layout-preferred ratio. Here's one of the final sections (with some alterations to make it understandable):
Text t = <some Text object>
float height = t.preferredHeight;
float width = t.preferredWidth;
((RectTransform)tooltipGO.transform).SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, (width / 4) + 8);
((RectTransform)tooltipGO.transform).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, (height / 4) + 7.5f);
The divide-by-four and offsets to account for relative scaling between the text and the background and some edge padding.

Adjust label height to height of string

So I'm making a C# Windows Forms application in which I have a label. This label's size is 100x100 by default*, but I want to automatically increase the label's height so that any string fits in it regardless of its "height". How would I do this? I haven't tried anything myself yet because I don't really have an idea of what to do. I'm just a beginner, after all!
Thanks in advance.
*100x100 is just an example, the real size is different (I'm not sure yet what it'll be)
Thanks everyone, I managed to get what I want by setting the label's MaximumSize property. (Answered by #LarsTech in comments)
AutoSize defaults to true - so the default behaviour of the textbox should resize to any font size/content that is in use.
The label control in Windows Forms is a container which accepts your input strings. Therefore, if you do change its initial values it will get to its content size anyways. So, just change its Text Property.
You could try it first.
here's an example code you can use. It uses the AutoSize property.
If you label is called Label1 you can change it like this:
Label1.AutoSize = true;
Label1.Text = "The text in this label is longer than the set size.";
And it will automatically change it.

How to make sure endlabels are always visible on c# chart x axis

I have created a chart in the image below and I need to insert in addition to the existing labels, the end labels on the extreme left and right of the x-axis. There are numerous datapoints and therefore I cannot set the interval to 1 for the labels as they'll clutter the axis. I have already tried setting the property
chartHistory.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = true;
but it doesn't seem to work. How can I achieve this?
You may try:
chartHistory.ChartAreas[0].AxisX.IsMarginVisible = true;
chartHistory.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = true;
Or probably you will have to add the labels yourself,
you can either use DataPoint.AxisLabel:
chartHistory.Series[0].Points[0].AxisLabel = "5/4/2010";
or more flexibly:
chartHistory.Series[0].Points[0].AxisLabel =
System.DateTime.FromOADate(chartHistory.Series[0].Points[0].XValue).ToShortDateString();
(and the same for the last point in series)
or you can add a custom label to the AxisX control:
chartHistory.ChartAreas[0].AxisX.CustomLabels.Add(0, 20, "5/4/2010");
See also this answer and this answer.

How to move a rectangle that's used as a Meter bar in unity

I want to make a vertical meter for values between 2 ranges.
so what i tried so far, was to make a frame, and under it put a mask, and under that a rectangle.
with the idea, of lowering/increasing the rectangle Y position to make it seems to move.
rect.position = new Vector3(rect.position.x, (rect.position.y - rect.sizeDelta.y));
so i did that, to first lower by it's height, so the bar will seem empty.
but it doesn't work right, the position depends on the screen size, and the rect Y end up way lower then exactly his height.
so, tl;dr, i want a bar that i can give a value between 0-100 and it will fill it according to that.
and i might have gone wrong about it.
so, any suggestions would be appreciated.
edit:
Add picture to show what i want the bar to look like:
Well i feel kinda dumb
I didn't realize how customizable the Slider control is.
I removed it's handle, unchecked interactble and removed it's background, and now i can give it a value between 0-100 and it works exactly as i wanted.
thanks Mathias Siig Nørregaard for referencing me to the video link that opened my eyes.

Categories