Custom X/Y grid line in MSChart control - c#

I have a C# windows form with a simple 2D line chart that I want to add custom X or Y axis markers to, and draw a custom grid line (in a highlighted color, dotted line for example). I have looked at the customLabels property, but this seems to override the default grid, which I still want to display. This is to illustrate something like a threshold or a cutoff. How can I do this with the MSChart control?
Many thanks

Could you achieve what you want with striplines?
In the ms chart samples (get it here http://archive.msdn.microsoft.com/mschart), inside the "Using Custom Labels" section, they use striplines on the Y axis which are quite effective at highlighting ranges of values. They also do not affect the grid ... I checked that by changing the sample code a little so I could easily move the boundaries of the striplines around (see below).
double low_med = 17; // was 30
double med_hi = 92; // was 70
// Set Y axis custom labels
axisY.CustomLabels.Add(0, low_med, "Low");
axisY.CustomLabels.Add(low_med, med_hi, "Medium");
axisY.CustomLabels.Add(med_hi, 100, "High");
StripLine stripLow = new StripLine();
stripLow.IntervalOffset = 0;
stripLow.StripWidth = low_med;
stripLow.BackColor = Color.FromArgb(64, Color.Green);
StripLine stripMed = new StripLine();
stripMed.IntervalOffset = low_med;
stripMed.StripWidth = med_hi - low_med;
stripMed.BackColor = Color.FromArgb(64, Color.Orange);
StripLine stripHigh = new StripLine();
stripHigh.IntervalOffset = med_hi;
stripHigh.StripWidth = 100 - med_hi;
stripHigh.BackColor = Color.FromArgb(64, Color.Red);
axisY.StripLines.Add(stripLow);
axisY.StripLines.Add(stripMed);
axisY.StripLines.Add(stripHigh);

Related

Datapoint to show X and Y value on chart

This should be easy, I just can't find it:
I find a datapoint in a series given some rules, I then want to show that point on the chart, at the moment it shows only the Y value. I need it to show both X and Y values, something like -1506;409
(X axis value = -1506, Y axis value = 409)
To display the datapoint I have at the moment:
datapoint.Font = new System.Drawing.Font("Serif", 7);
datapoint.LabelFormat = "#,#";
datapoint.IsValueShownAsLabel = true;
Any ideas?
Edit:
datapoint, of course is:
Datapoint datapoint
A nice overview of Labels in Chart controls is here on MSDN.
Here is an example at work; the first line labels each point in a whole series, the second line only one point in another series:
chart1.Series[3].Label = "Y = #VALY\nX = #VALX";
chart1.Series[1].Points[5].Label = "Y = #VALY\nX = #VALX";
A less crowded altenative may be setting tooltips, which only show up when the mouse is over the datapoint:
chart1.Series[2].ToolTip = "Y = #VALY\nX = #VALX";
For more ways to include data values do look into the chart Keywords!!
With the help of #Taw's code, its easy, just append the format to your specific datapoint:
datapoint.Label = "Y = #VALY\nX = #VALX";

Align two charts-areas one on top of the other [duplicate]

I have one Chart and three ChartArea that are aligned in view, zoom, cursor:
this is my related previous post. All things works well except that the three ChartArea are not aligned at the beginning. Following an image of the problem:
I think it depends from the digit's number of Y values axis. From some research I try the following configuration:
// selezione e zoom
dlChart.ChartAreas[VOLTAGE_AREA].CursorX.Interval = 1;
dlChart.ChartAreas[VOLTAGE_AREA].CursorX.IsUserEnabled = true;
dlChart.ChartAreas[VOLTAGE_AREA].CursorX.IsUserSelectionEnabled = true;
// generale
dlChart.ChartAreas[VOLTAGE_AREA].AxisX.LabelStyle.Format = "dd/MM/yy - HH:mm:ss.fff";
dlChart.ChartAreas[VOLTAGE_AREA].AxisX.ScaleView.Zoomable = true;
dlChart.ChartAreas[VOLTAGE_AREA].AxisY.LabelStyle.Format = "D5";
In witch the last row:
dlChart.ChartAreas[VOLTAGE_AREA].AxisY.LabelStyle.Format = "D5";
should specifies always five digits. This mitigate in some way the problem but it doesn't desappers. Furthermore with this row the program starts to throws very lots exceptions of form below any time I scroll the graph:
Generate exception: 'System.FormatException' in mscorlib.dll
Does anyone knows the solution for this problem? Thanks in advance.
You may want to take control of the size of the InnerPlotPosition.
(But Baddack's solution is simpler and more flexible!)
Here is an example:
After setting up a Chart with three CharAreas, setting Minima and Maxima as well as adding one DataPoint to each we get this :
Your issue is showing clearly.
After setting the InnerPlotPosition to a fixed percentage it looks like this:
Here is how to set the InnerPlotPosition size:
ca1.InnerPlotPosition = new ElementPosition(10, 5, 80, 90);
ca2.InnerPlotPosition = new ElementPosition(10, 5, 80, 90);
ca3.InnerPlotPosition = new ElementPosition(10, 5, 80, 90);
Note that both ChartArea.Position and ChartArea.InnerPlotPosition are called 'Position' but really are areas of percentages referring to the respective containers!
So my example has a Left distance of 10%, a Top space of 5% and Width of 80% and Height of 90%. Which leaves 10% space at the Bottom and 5% at the Right. Note: All are referring to the ChartAreas not the ClientArea of the Chart! (Which are still at Auto, which maximizes the size.)
This was my initial setup:
ChartArea ca1 = chart.ChartAreas[0];
ChartArea ca2 = chart.ChartAreas[1];
ChartArea ca3 = chart.ChartAreas[2];
Series s1 = chart.Series[0];
Series s2 = chart.Series.Add("Series2");
Series s3 = chart.Series.Add("Series3");
s2.ChartArea = ca2.Name;
s3.ChartArea = ca3.Name;
s1.Points.AddXY(1, 7);
s2.Points.AddXY(1, 777);
s3.Points.AddXY(1, Math.PI);
Have you tried using the chart area alignment options? I would try something like:
//define inner plot position of the chart areas
dlChart.ChartAreas[0].InnerPlotPosition.Auto = true;
dlChart.ChartAreas[1].InnerPlotPosition.Auto = true;
dlChart.ChartAreas[2].InnerPlotPosition.Auto = true;
//set our second chart area's alignments to match our first chart area
dlChart.ChartAreas[1].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
dlChart.ChartAreas[1].AlignmentStyle = AreaAlignmentStyles.All;
dlChart.ChartAreas[1].AlignWithChartArea = dlChart.ChartAreas[0].Name;
//set our third chart area's alignments to match our first chart area
dlChart.ChartAreas[2].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
dlChart.ChartAreas[2].AlignmentStyle = AreaAlignmentStyles.All;
dlChart.ChartAreas[2].AlignWithChartArea = dlChart.ChartAreas[0].Name;

Add axis name into chart c#

I'm working with winforms using C#.
I use chart and I want to set the titles of the X- and Y-axis in code. I tried
chart1.chartarea(0).axisX.title = "xxx"
but it does't work and I don't know why.
I am using the charts control on the web and setting the X and Y axis titles are done in the following way.
I assume the API would be the same for winforms.
var chartArea = new ChartArea("MyChart");
...
chartArea.AxisX.Title = "xxx";
chartArea.AxisY.Title = "yyy";
None of the solutions worked for me. I used the following code which helped me to add Axis title on windows form chart. I am adding a couple of useful properties so anyone who is working on it can have an idea how to use it. I searched a lot to find out all those properties. There are very few examples of this types.
chartESTOr.Titles.Add("Est OR Date " + " (" + Year + ")").Font = new Font("Arial", 10, FontStyle.Bold); // Chart Title
chartESTOr.ChartAreas["ChartArea1"].AxisX.Title = "Month"; // Chart X Axis Title
chartESTOr.ChartAreas["ChartArea1"].AxisX.TitleAlignment = StringAlignment.Center; // Chart X axis Text Alignment
chartESTOr.ChartAreas["ChartArea1"].AxisX.TextOrientation = TextOrientation.Rotated270; // Chart X Axis Text Orientation
chartESTOr.ChartAreas["ChartArea1"].AxisX.TitleFont = new Font("Arial", 8, FontStyle.Bold); // Chart X axis Title Font
chartESTOr.ChartAreas["ChartArea1"].AxisX.Interval = 1; // Chart X Axis Interval
chartESTOr.ChartAreas["ChartArea1"].AxisY.Title = "Quote Value (USD)"; // Chart Y Axis Title
chartESTOr.ChartAreas["ChartArea1"].AxisY.TitleAlignment = StringAlignment.Center; // Chart Y axis Text Alignment
chartESTOr.ChartAreas["ChartArea1"].AxisY.TextOrientation = TextOrientation.Horizontal; // Chart Y Axis Text Orientation
chartESTOr.ChartAreas["ChartArea1"].AxisY.TitleFont = new Font("Arial", 8, FontStyle.Bold); // Chart Y axis Title Font
chartESTOr.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format = "{0:0,}K"; // Chart Y Axis lable format
As suggested by #TaW in comments this code works as: chart1.ChartAreas[0].AxisX.Title = "xxx";
for dynamic addition the code by #Mytroy2050 is what works

VS2010 Chart control, how to display a blank chart?

I'm trying to use the chart control on a windows form and have it working, plotting some real time data, however before the data arrives nothing is displayed. I would like to show an empty graph with an X Y of 10 30 but still have the graph auto range if values go above this.
I cannot find a property to show the "blank" graph it this possible and if so how?
thanks
You can hide all data of a Series by making its line color Transparent. If you also set its LegendText to be " " all you can see are the Axis ticks. you can control them by adding a few Points and by setting the Minimum and Maximum values:
// short reference for our dummy:
Series S0 = chart1.Series[0];
// a simple type
S0.ChartType = SeriesChartType.Line;
// set 10 point with x-values going from 0-100 and y-values going from 1-10:
for (int i = 0; i < 100; i +=10) S0.Points.AddXY(i , i / 10);
// or add only a few, e.g. the first and last points:
//S0.Points.AddXY(100, 10);
//S0.Points.AddXY(0, 10);
// hide the line:
S0.Color = Color.Transparent;
// hide the legend text (it will still take up a little space, though)
S0.LegendText = " ";
// limit the axis to the target values
chart1.ChartAreas[0].AxisX.Maximum = 100;
chart1.ChartAreas[0].AxisX.Minimum = 0;
The result looks like an empty chart:

Labelling and circle a specific point in zedgraph

I am currently doing a project in which I've managed to identify the peak I want. However, I wanted to do more like circling the particular point with a label attached to it. Is it possible to do that in Zedgraph?
I've attached a snippet of my code which only include a text label to that point, and I wanted to do more so people will identify the point more easily.
PointPair pt = myCurve.Points[i-1];
const double offset = 0.8;
TextObj text = new TextObj("P", pt.X, pt.Y + offset,
CoordType.AxisXYScale, AlignH.Left, AlignV.Center);
text.ZOrder = ZOrder.A_InFront;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
text.FontSpec.Fill = new Fill( Color.FromArgb( 100, Color.White ) );
myPane.GraphObjList.Add(text);
Any help is appreciated! Thanks!
Make a LineItem as follows
LineItem line = new LineItem("Point", new double[] {pt.x}, new double[] {pt.y}, Color.Black, SymbolType.Circle);
line.Symbol.Size = 20;
line.Symbol.Fill = new Fill(Color.Transparent);
myPane.CurveList.Add(line);
This should create a large empty circle centered around your point. Obviously, you can adjust color and size as you see fit, and the ZOrder if you need to. You might want to adjust your legend so it doesn't include this point. Alternatively, you can name this line with your label and leave it in the legend as a way of tagging it. The only other way for a label is to do what you're doing, as I'm not sure of a way to associate labels directly to a line.

Categories

Resources