Add axis name into chart c# - 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

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";

WinForms Charting: X Axis Labels - where are they?

Ok.... I have a WinForms Chart that is successfully plotting my series. HOWEVER, the X Axis numeric labels (not the Title, the "number line") do NOT display at all. I have not altered or touched anything! I have the EXACT same setup in another VSExpress project, same plot setup, and those Axis numeric labels display just fine. What the hell is going on???
** The Y Axis numbers show up just fine, with no issue. It's only the X Axis that is being like this.
* The Y axis values are Pressures values
* The X Axis values are Volumes values
I have tried:
Forcing the "Interval" = 1
Forcing the Axis "Max" / "Min" to specific values
Setting the "IntervalType"
Setting the "LabelStyle"
Nothing has changed. Zero effect. There is still NO indication to the user what the values of the X Axis are displaying. There is only ONE plot, ONE chart, ONE chartArea and a single series with 3 data points. WHERE. ARE. THE. AXIS. NUMBERS!??
I was under the impression, that unless the user has tampered with something, those axis labels should just show up automatically (and then it's up to the user to format, etc.).... am I wrong about that?
Here is a snippet from the Designer:
chartArea2.AxisX.MinorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea2.AxisX.MinorTickMark.Enabled = true;
chartArea2.AxisX.Title = "Cum. Adjusted Inventory (Mcf)";
chartArea2.AxisX.TitleFont = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
chartArea2.AxisX.LabelStyle.Interval = 1;
chartArea2.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Auto;
chartArea2.AxisY.MinorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea2.AxisY.MinorTickMark.Enabled = true;
chartArea2.AxisY.Title = "Pressure (psia)";
chartArea2.AxisY.TitleFont = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
~Sincerely frustrated,
A
I figured out the answer to my question.
I had this code written in a .cs file:
System.Windows.Forms.DataVisualization.Charting.Series newObservationSeries = new System.Windows.Forms.DataVisualization.Charting.Series()
{
ChartType = SeriesChartType.Line,
MarkerStyle = MarkerStyle.Diamond,
MarkerSize = 5,
BorderWidth = 2,
BorderDashStyle = ChartDashStyle.Dash,
AxisLabel = "Pressure"
};
This last line "AxisLabel = "Pressure"" was screwing up the plots ability to discern the correct Axis Labels. I simply REMOVED that line, and the labels appeared. It was from a prior attempt to create an Axis Title, before I knew how to do so. I simply forgot to remove it.
The correct code is:
System.Windows.Forms.DataVisualization.Charting.Series newObservationSeries = new System.Windows.Forms.DataVisualization.Charting.Series()
{
ChartType = SeriesChartType.Line,
MarkerStyle = MarkerStyle.Diamond,
MarkerSize = 5,
BorderWidth = 2,
BorderDashStyle = ChartDashStyle.Dash
};
Cheers!

How to separate two series by displaying Line between them in Column chart C#?

I have 2 series (2016 and 2017) in column chart and all datapoints values are showing fine. but I need to differentiate two series values by showing thick border line between two series.
because , now it seems to combining the 2017 values with 2016 series values since no separator line not there.
FYI.
EDIT:
After used vertical line in my column chart the output as like below,
But i need only one Line that should present between the two series .
how do i remove other lines.
Finally , Got the expected Output.
Thanks in advance.
var series = Mainchart.Series[0]; //series object
var chartArea = Mainchart.ChartAreas[series.ChartArea];
chartArea.AxisX.StripLines.Add(new StripLine
{
BorderDashStyle = ChartDashStyle.Solid,
BorderColor = Color.Black,
Interval = 0, // to show only one vertical line
IntervalOffset = 1.5, // for showing Vertical line between 2 series
IntervalType = DateTimeIntervalType.Years // for me years
});
You may use StripLine:
StripLine limit_lower_strip = new StripLine();
limit_lower_strip.Interval = 0;
limit_lower_strip.IntervalOffset = v1_lower;
limit_lower_strip.StripWidth = 0.0;
limit_lower_strip.BorderColor = Color.FromArgb(100, Color.Red);
limit_lower_strip.BorderDashStyle = ChartDashStyle.Solid;
limit_lower_strip.BorderWidth = 5;
chart1.ChartAreas[0].AxisX.StripLines.Add(limit_lower_strip);

Creating multiple charts and the relation between Chart, Series, ChartArea

I have been generating charts using MSChart for some time now, but I have never created multiple charts within one chart object. Thinking about this task has revealed a gap in my knowledge.
How I think about creating a chart
Create Chart object
Add ChartArea object to Chart object
Create Series and add data
Add Series to Chart
The object structure ends up looking like this
Chart
/ \
ChartArea Series
As far as I have been concerned in the past, ChartArea is simply the area I set up the labels and that sort of thing. To add another, I will be wanting to add another ChartArea and one or more series.
___________________ Chart ___________________
/ / \ \
ChartArea0 ChartArea1 Series0 Series1
How do I associate Series0 to ChartArea0? It would make sense to add a Series to a ChartArea, but that is not possible. For what reason is it beneficial to associate a Series with a Chart, rather than a ChartArea?
Series are associated with chart areas like so
Chart Chart0 = new Chart();
ChartArea ChartArea0 = new ChartArea("name");
Chart0.ChartAreas.Add(ChartArea0);
Series Series0 = new Series();
Chart0.Series.Add(Series0);
// link series to area here
Series0.ChartArea = "name";
A Chart can be divided into multiple Areas where one area can be a Bar chart other can be Pie chart.
System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartarea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartarea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
chart1.ChartAreas.Clear();
chart1.ChartAreas.Add(chartarea1);
chart1.ChartAreas.Add(chartarea2);
Then you create some series; each series will be associated with a chart area. if you create 5 series and associate series1, series2 and series3 to chartarea1 then those series must be same or compatible chart type. otherwise Runtime error will occur. Multiple series In same Chart Area may have different x axis component in some cases. for example in the following code: series1 has 3 data points and series2 has 5, in this case chartarea will show first three x values from series1 and next two x values from series2.
chart1.Series.Clear();
chart1.Series.Add("Series1");
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
chart1.Series[0].ChartArea = chart1.ChartAreas[0].Name;
chart1.Series[0].Points.AddXY("Point1", 20);
chart1.Series[0].Points.AddXY("Point2", 50);
chart1.Series[0].Points.AddXY("Point3",30);
chart1.Series.Add("Series2");
chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
chart1.Series[1].ChartArea = chart1.ChartAreas[0].Name;
chart1.Series[1].Points.AddXY("newname1", 10);
chart1.Series[1].Points.AddXY("newname2", 20);
chart1.Series[1].Points.AddXY("newname3", 30);
chart1.Series[1].Points.AddXY("newname4", 40);
chart1.Series[1].Points.AddXY("newname5", 50);
this.tabPage3.Controls.Add(chart1);
chart1.Dock = System.Windows.Forms.DockStyle.Fill;
Previous answer breaks width of chart, this example uses elementposition objects, specifically set with to 100% (all nr's are %()
This example: "Two Chart Areas, vertically divided 80/20":
ElementPosition ePos = new ElementPosition();
ePos.Width = 100; ePos.Y = 0; ePos.X = 2; ePos.Height = 80;
ElementPosition ePos2 = new ElementPosition();
ePos2.Width = 100; ePos2.Y = 80; ePos2.X = 2; ePos2.Height = 20;
chartCandleStick.ChartAreas[0].Position = ePos;
chartCandleStick.ChartAreas[1].Position = ePos2;

Custom X/Y grid line in MSChart control

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);

Categories

Resources