Chart control change axis color - c#

This is my Chart control Graph:
I want to remove axis black lines from the left side and add blue line as a frame for all the graph like at the button.
I have search for all the properties and try to change color but the default color still remained

I know this old post but to answer the OP question.
Changes the line color and line style
Chart1.ChartAreas(0).AxisY.LineColor = Color.Blue
Chart1.ChartAreas(0).AxisY.LineDashStyle =ChartDashStyle.Dot
If want to remove the tick marks you use this code
Chart1.ChartAreas(0).AxisY.MajorTickMark.Enabled = False
Chart1.ChartAreas(0).AxisY.MinorTickMark.Enabled = False

if you want to customize a chart you could draw your own dynamically:
Example in VB.net:
Const GraphHeight As Integer = 100
Const GraphWidth As Integer = 200
Dim cIMG As New Bitmap(GraphWidth, GraphHeight)
Dim G As Graphics = Graphics.FromImage(cIMG)
Dim red As Integer = 0
Dim pnts(4) As Point
pnts(0) = New Point(0, GraphHeight - 0)
pnts(1) = New Point(50, GraphHeight - 80)
pnts(2) = New Point(100, GraphHeight - 50)
pnts(3) = New Point(150, GraphHeight - 40)
pnts(4) = New Point(200, GraphHeight - 20)
G.DrawLines(Pens.Blue, pnts)
PictureBox1.Image = cIMG
Example in C#:
const int GraphHeight = 100;
const int GraphWidth = 200;
Bitmap cIMG = new Bitmap(GraphWidth, GraphHeight);
Graphics G = Graphics.FromImage(cIMG);
int red = 0;
Point[] pnts = new Point[5];
pnts(0) = new Point(0, GraphHeight - 0);
pnts(1) = new Point(50, GraphHeight - 80);
pnts(2) = new Point(100, GraphHeight - 50);
pnts(3) = new Point(150, GraphHeight - 40);
pnts(4) = new Point(200, GraphHeight - 20);
G.DrawLines(Pens.Blue, pnts);
PictureBox1.Image = cIMG;
you could use a loop to draw the grid
Example

Related

Dynamically Combining Rectangles

I'm trying to combine two rectangles that I need to create dynamically but I can't figure out how to draw them using .Data and I don't know how to convert from Windows.Shapes.Rectangle to Windows.Media.Geometry.
Rectangle Cross1 = new Rectangle();
Cross1.Margin = new Thickness(465, -140, 0, 0);
Cross1.Height = 110;
Cross1.Width = 15;
Cross1.RenderTransform = rotateTransform1;
Rectangle Cross2 = new Rectangle();
Cross2.HorizontalAlignment = HorizontalAlignment.Left;
Cross2.VerticalAlignment = VerticalAlignment.Top;
Cross2.Margin = new Thickness(362, -103, 0, 0);
Cross2.Height = 110;
Cross2.Width = 15;
Cross2.RenderTransform = rotateTransform2;
CombinedGeometry c1 = new CombinedGeometry(GeometryCombineMode.Union, Cross1, Cross2);
The CombinedGeometry class only works with other System.Windows.Media.Geometry objects, not System.Windows.Shapes. You need to use the equivalent RectangleGeometry class instead.
Something such as:
RectangleGeometry Cross1 = new RectangleGeometry(new Rect(0, 0, 15, 110));
Cross1.Transform = rotateTransform1;
RectangleGeometry Cross2 = new RectangleGeometry(new Rect(0, 0, 15, 110));
Cross2.Transform = rotateTransform2;
CombinedGeometry c1 = new CombinedGeometry(GeometryCombineMode.Union, Cross1, Cross2);

How to increase label box text Size in c# windows application?

I using following code, When I run below code, Half of the text only displayed in label box. I want display full line in label box text? How to do it?
Label dynamiclabel1 = new Label();
dynamiclabel1.Location = new Point(280, 90);
dynamiclabel1.Name = "lblid";
dynamiclabel1.Size = new Size(150, 14);
dynamiclabel1.Text="Smith had omitted the paragraph in question (an omission which had escaped notice for twenty years) on the ground that it was unnecessary and misplaced; but Magee suspected him of having been influenced by deeper reasons.";
dynamiclabel1.AutoSize = true;
dynamiclabel1.Font = new Font("Arial", 10, FontStyle.Regular);
panel1.Controls.Add(dynamiclabel1);
Label dynamiclabel = new Label();
dynamiclabel.Location = new Point(38, 30);
dynamiclabel.Name = "lbl_ques";
dynamiclabel.Text = question;
//dynamiclabel.AutoSize = true;
dynamiclabel.Size = new System.Drawing.Size(900, 26);
dynamiclabel.Font = new Font("Arial", 9, FontStyle.Regular);
You have used AutoSize and explicit label size setting at the same time, this makes no sense.
If you want to enable word wrapping in your label text - you have to set dynamiclabel1.AutoSize = false; first and then increase its Height. Currently it has only 14 pixels of height so it is impossible to make text multiline. Increase it to something about 200 pixels and all the text will be placed inside the label in a few lines.
If you increase the height, and want less rows, set the Width property to something bigger;
dynamiclabel1.Width = 150;
If you want to select the number of lines depending on font/text size, you can do something like this:
Label dynamiclabel1 = new Label();
dynamiclabel1.Location = new Point(280, 90);
dynamiclabel1.Name = "lblid";
dynamiclabel1.Size = new Size(150, 100);
dynamiclabel1.Text = "Smith had omitted the paragraph in question (an omission which had escaped notice for twenty years) on the ground that it was unnecessary and misplaced; but Magee suspected him of having been influenced by deeper reasons.";
dynamiclabel1.Font = new Font("Arial", 10, FontStyle.Regular);
int lines = 3; //number of lines you want your text to display in
using (Graphics g = CreateGraphics())
{
SizeF size = g.MeasureString(dynamiclabel1.Text, dynamiclabel1.Font);
float w=size.Width / lines;
dynamiclabel1.Height = (int)Math.Ceiling(size.Height*lines);
dynamiclabel1.Width = (int)Math.Ceiling(w);
}
panel1.Controls.Add(dynamiclabel1);
Edit
If what you know is the width you have available and you want your label to adjust height to show all the content, can do this:
Label dynamiclabel1 = new Label();
dynamiclabel1.Location = new Point(280, 90);
dynamiclabel1.Name = "lblid";
dynamiclabel1.Size = new Size(150, 100);
dynamiclabel1.Text = "Smith had omitted the paragraph in question (an omission which had escaped notice for twenty years) on the ground that it was unnecessary and misplaced; but Magee suspected him of having been influenced by deeper reasons.";
dynamiclabel1.Font = new Font("Arial", 10, FontStyle.Regular);
int width = 600; //Width available to your label
using (Graphics g = CreateGraphics())
{
SizeF size = g.MeasureString(dynamiclabel1.Text, dynamiclabel1.Font);
lines = (int)Math.Ceiling(size.Width / width);
dynamiclabel1.Height = (int)Math.Ceiling(size.Height * lines);
dynamiclabel1.Width = width;
}
panel1.Controls.Add(dynamiclabel1);

Plot Area Gradient in MS Chart

I'm currently working with MS Chart for the first Time, and i want my C# Programm to draw a grey to white gradient under my graph, just like this (on the left is the current version, on the right what I want to do):
EDIT: Thank you Dmitry for including the Image from my Link :)
The only gradient Option I found for MS Chart so far is the Background.
This is my basic configuration of the chart so far:
chart.Series.Add(MsChartHelper.CreateSeries(chartData, SeriesChartType.Line));
chart.ChartAreas.Add(MsChartHelper.CreateChartArea());
chart.Series[0].BorderWidth = 1;
chart.Series[0].BorderColor = fnBlue;
chart.Series[0].Font = new System.Drawing.Font("Verdana", 7F, System.Drawing.FontStyle.Regular);
chart.ChartAreas[0].AxisY.Minimum = 92;
chart.ChartAreas[0].AxisY.Maximum = 96;
chart.ChartAreas[0].AxisY.Interval = 1;
chart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Verdana", 7F, System.Drawing.FontStyle.Regular);
chart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Verdana", 7F, System.Drawing.FontStyle.Regular);
chart.ChartAreas[0].AxisX.Interval = 36;
chart.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;
chart.ChartAreas[0].AxisX.IsInterlaced = true;
chart.ChartAreas[0].AxisX.InterlacedColor = fnGray;
chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
DataPoint min = chart.Series[0].Points.FindMinByValue();
min.MarkerStyle = MarkerStyle.None;
min.IsValueShownAsLabel = true;
DataPoint max = chart.Series[0].Points.FindMaxByValue();
max.MarkerStyle = MarkerStyle.None;
max.IsValueShownAsLabel = true;
Ok, finally I figured out, how to achieve this. So if someone has the same Issue for some reason, this is how you do it:
Your Charttype hast to be Area
chart.Series.Add(MsChartHelper.CreateSeries(chartPlotData, SeriesChartType.Area));
After this, you can add a gradient and set a border color:
chart.Series[0].BackGradientStyle = GradientStyle.TopBottom;
chart.Series[0].Color = Color.FromArgb(125, 110, 110, 110);
chart.Series[0].BackSecondaryColor = Color.FromArgb(0, 255, 255, 255);
chart.Series[0].BorderColor = System.Drawing.ColorTranslator.FromHtml("#AA3131");
chart.Series[0].BorderDashStyle = ChartDashStyle.Solid;
chart.Series[0].BorderWidth = 1;
I hope this helps :)

List of labels does not display in Picture Box

I have a problem with Labels in VisulaStudio.
The version of VisualStudio I use is 2012.
The problem is, I need to show a grid and label the lines. The code I wrote seams identical to the solution of a similar problem here. It doesn't give me any compiler errors, but the labels still do not display in the pictureBox.
private void aResize()
{
Size clientSize = this.ClientSize;
int hToDraw, wToDraw;
hToDraw = clientSize.Height - 2 * marginOfTab;
wToDraw = clientSize.Width - 2 * marginOfTab;
tabControl1.Size = new Size(wToDraw, hToDraw);
piB1.Size = new Size(wToDraw, hToDraw);
piB1.Image = new Bitmap(piB1.Size.Width, piB1.Size.Height);
using (Graphics g = Graphics.FromImage(piB1.Image))
{
g.FillRectangle(new SolidBrush(Color.LightGray), 0, 0, W, H);
Pen gridPen = new Pen(Color.White, 1f);
int hDrawingStep = hToDraw / 10 -1;
int wDrawingStep = wToDraw / 10 -1;
for (int local = 1; local < 11; local++)
{
g.DrawLine(gridPen, 0, hDrawingStep*local, wToDraw, hDrawingStep*local); //horizontal axix
g.DrawLine(gridPen, wDrawingStep*local, 0, wDrawingStep*local , hToDraw); //vertical axis
Label localLabel = new Label();
localLabel.Name = "la" + local;
localLabel.Visible = true;
localLabel.Text = (local*100).ToString();
localLabel.Location = new Point((int)local*hDrawingStep, (int)10);
labelList.Add(localLabel);
}
}
}
All variables which are not declared in the code above are declared earlier. I didn't want to paste in too much. Thanks for any suggestion.
You don't set any parent for your localLabel, so how could it be rendered? Try this right before adding your localLabel to your labelList:
//...
localLabel.Parent = piB1;
labelList.Add(localLabel);

C# Windows Phone - Creating a round mask

I need to mask dynamically created images, so that they will be shown as circles.
Pictures can be square, but are usually rectangles... so the circle that will be shown can be taken from the center of it...so the shown circle must be inscribed in the picture and centered in the center of it.
This is the code I'm using right now:
//Setting up the image
Image image = new Image();
image.Height = 70;
image.Width = 70;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri("http://url-of-the-image", UriKind.Absolute);
image.CacheMode = new BitmapCache();
image.Source = bitmapImage;
image.Stretch = Stretch.UniformToFill;
image.VerticalAlignment = System.Windows.VerticalAlignment.Center;
//Setting up the mask
RadialGradientBrush opacityMask = new RadialGradientBrush();
GradientStop gs1 = new GradientStop();
GradientStop gs2 = new GradientStop();
GradientStop gs3 = new GradientStop();
gs1.Color = Color.FromArgb(255, 0, 0, 0);
gs1.Offset = 0.0;
gs2.Color = Color.FromArgb(255, 0, 0, 0);
gs2.Offset = 0.999;
gs3.Color = Color.FromArgb(0, 0, 0, 0);
gs3.Offset = 1.0;
opacityMask.GradientStops.Add(gs1);
opacityMask.GradientStops.Add(gs2);
opacityMask.GradientStops.Add(gs3);
image.OpacityMask = opacityMask;
//Showing the image
panel.Children.Add(image);
This all works fine, but when the pictures are rectangular and not square, this creates an ellipse instead of a circle... any idea on how can I force it to create a circle?
I also tried to specify some more parameters, but doesn't seem to help:
opacityMask.Center = new Point(0.5, 0.5);
opacityMask.RadiusX = 0.5;
opacityMask.RadiusY = 0.5;
Okay, today i tried again to fix this, and i came out with a solution.
It's not the best, more clean solution ever...but works :)
I basically wrapped the picture (not masked) into a StackPanel and then applied the mask to the StackPanel instead ;)
This is how it looks like (the only lines that change from the original are the the last few ones):
//Setting up the image
Image image = new Image();
image.Height = 70;
image.Width = 70;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri("http://url-of-the-image", UriKind.Absolute);
image.CacheMode = new BitmapCache();
image.Source = bitmapImage;
image.Stretch = Stretch.UniformToFill;
image.VerticalAlignment = System.Windows.VerticalAlignment.Center;
//Setting up the mask
RadialGradientBrush opacityMask = new RadialGradientBrush();
GradientStop gs1 = new GradientStop();
GradientStop gs2 = new GradientStop();
GradientStop gs3 = new GradientStop();
gs1.Color = Color.FromArgb(255, 0, 0, 0);
gs1.Offset = 0.0;
gs2.Color = Color.FromArgb(255, 0, 0, 0);
gs2.Offset = 0.999;
gs3.Color = Color.FromArgb(0, 0, 0, 0);
gs3.Offset = 1.0;
opacityMask.GradientStops.Add(gs1);
opacityMask.GradientStops.Add(gs2);
opacityMask.GradientStops.Add(gs3);
//Setting up the StackPanel
StackPanel sp = new StackPanel();
sp.OpacityMask = opacityMask;
//Showing the image
sp.Children.Add(image);
panel.Children.Add(sp);

Categories

Resources