WinForms Charting: X Axis Labels - where are they? - c#

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!

Related

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;

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

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

How do I add additional series to a Excel Chart using C#

I am trying to add an additional data series to the chart this shows CPU threshold, I can get the range and create the graph with out the threshold on it, but I don't know how to add the threshold value to the chart.
do I need to create another chart object? can I use the existing and just add teh new range in?
How are you creating the chart? -- see code below.
Is this chart already created in the excel file, and you want to modify the chart in the excel file? yes the chart is already in a Excel file.
Excel.ChartObjects sCPUChart;
Excel.ChartObject sCPUChartObjects;
sCPUChart = sDBSheet.ChartObjects(Type.Missing);
sCPUChartObjects = sCPUChart.Add(49, 15, 360, 215);
Excel.Chart sChartCPU;
sChartCPU = sCPUChartObjects.Chart;
sChartCPU.SetSourceData(cpuChartRange, Missing.Value);
sChartCPU.ChartWizard(Source: cpuChartRange, Gallery: Excel.XlChartType.xlLine, Format: 2, HasLegend: true);
sChartCPU.Location(Excel.XlChartLocation.xlLocationAsObject, sDBSheet.Name);
//CPU Chart Axis
Excel.Axis xSChartCPUAxis;
xSChartCPUAxis = sChartCPU.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
Excel.Axis ySChartCPUAxis;
ySChartCPUAxis = syChartCPU.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
ySChartCPUAxis.HasMajorGridlines = true;
ySChartCPUAxis.MaximumScaleIsAuto = true;
//Set Summary CPU Series
Excel.Series sCPUSeries = sChartCPU.SeriesCollection(1);
sCPUSeries.Name = "CPU";
//-------
// this is where I am having my issue
//I don't know how to add the threshold line to the graph with the existing graph being displayed
//sChartCPU.set_HasAxis(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlSecondary, true);
//summaryChartCPU.SetSourceData(summaryMemThreshold, Type.Missing); -- things break
//-------
I have now done the following:
Excel.SeriesCollection threshold = sChartCPU.sseriesCollection();
Excel.Series line = threshold.NewSeries();
line.Formula = "=SERIES(Summ!$D$54,Summ!$C$55:$C$56,Summ!$D$55:$D$56)";
line.ChartType = Excel.XlChartType.xLScatterLinesNoMarkers;
when the threshold line is created I have the following
the values I have in cells D54 - threshold
C55 = 0
C56 = 1
D55 = 75
D56 = 75
I don't know how to remove the 2 additional axis that appear in chart
If I comment out the line.ChartType, then the axis is correct but I only get one threshold data point ?? I don't understand why.
var series = (SeriesCollection) yourChart.SeriesCollection();
var line = series.NewSeries();
line.Name = "CPU Threshhold";
//line.Values = ...;
//line.XValues = ...;
//formatting
Here's the solution I found to the OP's question of how to remove the secondary axes:
Excel.SeriesCollection threshold = sChartCPU.sseriesCollection();
Excel.Series line = threshold.NewSeries();
// line.Formula = "=SERIES(Summ!$D$54,Summ!$C$55:$C$56,Summ!$D$55:$D$56)";
// instead of setting the Formula, I set the series values
line.Values = "='Summ!'$D$55:$D$56";
line.XValues = "='Summ!'$C$55:$C$56";
line.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;
// creates Axis objects for the secondary axes
Excel.Axis YAxis2 = (Excel.Axis)sChartCPU.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary);
Excel.Axis XAxis2 = (Excel.Axis)sChartCPU.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlSecondary);
// remove the major and minor tick marks from the secondary (top) x-axis
XAxis2.MajorTickMark = Excel.XlTickMark.xlTickMarkNone;
XAxis2.MinorTickMark = Excel.XlTickMark.xlTickMarkNone;
// change the secondary x-axis max range to 1.0 so the bar will go all the way to the right side
XAxis2.MaximumScale = 1.0;
// delete the secondary x-axis labels
XAxis2.TickLabels.Delete();
// I chose to delete the secondary y-axis so the line would graph on the primary axis scale
YAxis2.Delete();

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