C# DataViusalization Chart Dynamic Sizing - c#

I currently have a chart created in my project as follows:
Chart c = new Chart{
Width = 1000,
Height = 1000,
};
This will create a chart that is 1000 pixels wide and 1000 pixels high. My question is, is there a way to have these values set dynamically so that it could take up a particular portion of the screen? For example if I wanted it to take up 80% of the screen's width and 50% of the height, is there a way to tell it to do that instead of giving static pixel values? If that cannot be done, is there a way to simply have it stretch to the size of its container in the HTML?

So pretty much right after I posted this, I figured out a solution. The chart is displayed in the View via <img> tag, so I can set the style width and height within that tag to change the size of the displayed chart

Related

How to resize chart more than screen resolution?

I have a C# 4.7 WinForms application with line chart. I want to save chart image with very large resolution e.g 10000x2000. Here is what I have archived:
Form1.Width = 3860; //my screen width resolution - maximum allowed
Form1.Height = 1080; //my screen height resolution - maximum allowed
chart1.Dock = DockStyle.Fill;
chart1.SaveImage(saveFile.FileName, ChartImageFormat.Jpeg);
That code saves jpeg image with 3860x1080 resolution and i can zoom in points I need. But it is not enough and I want to enlarge my form or chart to
get an image like 10000x2000, VS says it cannot be larger than my screen.
I have a lot of data on this chart, so bigger form provides me with more data on a chart. Picture is required for other people to see the maximum info.
How can I do it?
This is quite simple.
All you need to do is create the Chart in code without displaying it on screen.
Now you can size it as needed..:
Chart bigChart = new Chart();
bigChart.ChartAreas.Add("ca");
Series s1 = bigChart.Series.Add("s1");
s1.BorderWidth = 5;
s1.ChartType = SeriesChartType.Line;
// testdata
Random rnd = new Random(8);
for (int i = 0; i < 111; i++)
{
s1.Points.AddXY(rnd.Next(123) + i, rnd.Next(12 * i)) ;
}
bigChart.Size = new Size(10000, 8000);
bigChart.SaveImage(filename, ChartImageFormat.Png);
A few notes:
You may want to add a Legend as well, as the dynamically created Chart has none of the default elements..
If you want to you can use a chart you have, serialize its content to a file (or stream) and then load (deserialize) it into the big chart..
The automatic smartness of MSChart tends to hit some limit when the sizes grow a lot; so brace yourself for tweaking some settings for line width, Fonts etc..
You also may want to load the saved image and set the dpi to a bigger values than the default, which is the screen resolution..
Final remark: Sometimes it is better to save the chart as a vector format; these are also provided in the SaveImage method.
Update: Maybe you want to combine vector and pixel formats as in Jimi's last comments.
You may want to study this post which discusses a few general options.

How do you calculate pixel size of string?

I have a string. I know the font family and the font size it is going to be displayed in. I need to know how many pixels the text will take up in the ui. So that I can determine whether to show another element. How would I do that?
I found a couple of things, but none of them were available in my Windows universal project. For example:
Graphics.MeasureString
TextRenderer.MeasureText
Edit:
This is not a web project.
I want to calculate the size it will take in the ui before it is in the ui.
I think you need to create a textblock in code and assign the desired text to it. Then you can get the actual height and width from it. See the below code
TextBlock txt=new TextBlock();
//set additional properties of textblock here . Such as font size,font family, width etc.
txt.Text = "your text here";
var height = txt.ActualHeight;
var width = txt.ActualWidth;
You can do further operations based on this height and width
I am not saying this is the optimized solution .But this will work for you
Try checking the values of the Width and Height properties of the control you use to display your text (eg. your TextBox), after setting your string as text/content, to decide whether to show another element.

How to set a emgu imagebox size to a percentage of screen size

I have two side by side Emgu imagebox and I want their size to be relative to that of the users screen size (33% of the screens width). I tried to set it using something like this in the main form:
rectangle screen = Screen.PrimaryScreen.Bounds;
int screenHeight = (screen.Height/2);
int screenWidth = screenHeight;
and then setting the size to these in the designer form but that just led it to not load the imagebox at all. If anyone knows how to do it with a pictureBox, that may work too.
Here is a picture of my form to help get an idea of what I am trying to achieve. I would like to have these proportions on a screen of any size. Like the % feature in css.
http://oi59.tinypic.com/dwdf12.jpg (I have linked it due to lack of reputation)
Use a tableLayoutPannel and set its columns to 33%. Then just select full on the dock property to fill each cell.

WinForms: ImageList for ListView with different image sizes

I have a ListView with View property set to LargeIcon. And I have an ImageList as image source for ListView.
What I want is to display both vertically and horizontally oriented images in ListView but ImageList have only a single property ImageSize for all images in its collection so for example if I set that property to 150x100 and add a vertical image (100x150) to collection - ListView automatically stretches it to 150x100.
So as I understand I need some ImageList, in which every image is stored with its original size. Any thoughts about how to do that?
Thanks in advance.
I have had this problem myself, Here is what I did, I hope it will help you as well, first determine the biggest image size you would use ( for example 200x200) then use Png or Gif images (all 200x200) with transparent backgrounds.
have a look at these two images i have created as an example.
but I make it like this to avoid stretching:
To keep from stretching you can use cross-multiplication.
Image Width Desired Width
--------------- = ---------------
Image Height New Height
or
Image Width New Width
--------------- = ---------------
Image Height Desired Height
In code it looks something like this
int height = img.Width*newWidth/image.Height;
or
int height = img.Width/image.Height*newWidth;
and
int width = img.Height*newHeight/image.Width;
or
int width = img.Height/image.Width*newHeight;
Then when creating your new bitmap you can draw a scaled image on the new bitmap using the desired size and the derived size.

How can I programmatically determine the width of Y-axis section of a WPF chart?

I am developing a WPF application that has a stacked column chart (I'm using the Silverlight Toolkit charting features). I need to dynamically overlay a semi-transparent rectangle over a section of the chart - the size and location of the rectangle needs to adapt to the number of data points on the X axis. The X-axis values represent days, the number of which may vary, but the rectangle always needs to cover 30 days.
In any case, I've figured out most of it, but I need to find out how much width the Y-axis label section of the chart is taking up so that I can take it into account in my rectangle size and location calculations.
There is an "Actual Width" property for the chart available, but I don't know how to get the actual width for just the Y-axis label area. Does anyone know how to find this?
I was able to address this issue by waiting until the chart was loaded and then using the techniques described here http://www.scottlogic.co.uk/blog/colin/2009/03/adding-a-location-crosshair-to-silverlight-charts-again/.
The key thing here is to do the processing when the Loaded event is received:
MyChart.Loaded += (sender, e) =>
{
// MyChart is about to be rendered
// it's now safe to access the ActualWidth properties of the chart components
MyRectangle.Left = MyChart.ActualWidth/2;
}

Categories

Resources