visual studio c# chart with only one axis - c#

I'm trying to create in c# in Visual studio a nice way to show the minimum, maximum and actual value of a "variable" (Variable is a class). I was trying to use the charts to do that but I have two problems.
1) It shows in 2D and I only need 1 dimension.
2) I can't write tags on the values, in this case to show which is the minimum, the maximum and the current value.
Is there a SeriesChartType that does that?
I would appreciate ideas. Thanks!

It is not so much the chart type but the various styling options you need to play with.
Here is an example using ChartType.Point:
// no legend:
chart.Legends.Clear();
// a couple of short references:
ChartArea ca = chart.ChartAreas[0];
Series S1 = chart.Series[0];
// no y-axis:
ca.AxisY.Enabled = AxisEnabled.False;
ca.AxisY.Minimum = 0;
ca.AxisY.Maximum = 1;
// use your own values:
ca.AxisX.Minimum = 0;
ca.AxisX.Maximum = 100;
// style the ticks, use your own values:
ca.AxisX.MajorTickMark.Size = 7;
ca.AxisX.MajorTickMark.Interval = 10;
ca.AxisX.MinorTickMark.Enabled = true;
ca.AxisX.MinorTickMark.Size = 3;
ca.AxisX.MinorTickMark.Interval = 2;
// I turn the axis labels off.
ca.AxisX.LabelStyle.Enabled = false;
// If you want to show them pick a reasonable Interval!
ca.AxisX.Interval = 1;
// no gridlines
ca.AxisY.MajorGrid.Enabled = false;
ca.AxisX.MajorGrid.Enabled = false;
// the most logical type
// note that you can change colors, sizes, shaps and markerstyles..
S1.ChartType = SeriesChartType.Point;
// display x-value above; make sure you have enough room!
S1.Label = "#VALX";
// a few test data:
S1.Points.AddXY(1, 0.1);
S1.Points.AddXY(11, 0.1);
S1.Points.AddXY(17, 0.1);
S1.Points.AddXY(81, 0.1);
Note that you can play with the Series.SmartLabelStyle to display values when they are too dense!
I add the DataPoints at an y-value of 0.1. You can change it to move the points up or down a little..

Related

Display marks in contour points

I create a single contour and add it to the chart, add points with label text and also subscribe to the GetSeriesMark event, but the text is not displayed, and the event never gets fired
Contour contour1 = new Contour();
contour1.IrregularGrid = true;
//
// contour1
contour1.Brush.Color = Color.FromArgb(68, 102, 163);
contour1.ColorEach = false;
contour1.EndColor = Color.FromArgb(192, 0, 0);
contour1.FillLevels = checkEditFillLevels.Checked;
//
//
contour1.Marks.Style = MarksStyles.Label;
contour1.Marks.Visible = true;
//
//
contour1.NumLevels = 8;
contour1.PaletteMin = 0;
contour1.PaletteStep = 0;
contour1.PaletteStyle = PaletteStyles.Pale;
//
//
contour1.Pen.Color = Color.FromArgb(192, 192, 192);
contour1.Pen.Style = DashStyle.Dot;
//
//
contour1.Pointer.HorizSize = 2;
//
//
contour1.Pointer.Pen.Visible = false;
contour1.Pointer.Style = PointerStyles.Rectangle;
contour1.Pointer.VertSize = 2;
contour1.Pointer.Visible = true;
contour1.StartColor = Color.FromArgb(255, 255, 192);
contour1.Title = "contour1";
Adding points is done with this
contour1.Add(x, y, z, "My Point 1");
Is there a way to display marks on the exact points in the Contour, and moreover is there a way to display marks only on specific points in the Contour (some points are actual data, others are made using interpolation to be able to show the contour)?
I'm afraid not, Contour series calculates and displays isolines from a custom array of X, Y and Z points. Levels are calculated automatically from users data. What would you like to get exactly? You might be interested in using Annotation tools. Here you can find an example about custom annotation tool positioning.
Since it is not possible to mark single points in contour (see #NarcĂ­s Calvet's answer), I ended up adding one Points series with marks on them.
However, I still wanted only the Contour levels to be shown in the legend, and X axis to display it's values instead of Marks of the Points, so I needed to add following lines.
tChart1.Legend.LegendStyle = LegendStyles.Values;
tChart1.Legend.Series = _currentContour;
tChart1.Axes.Bottom.Labels.Style = AxisLabelStyle.Value;

C# WinForms Charts configuration

I am trying to plot a file's byte count over a C# WinForms bar graph. As such, the X-axis will have values 0-255 (if greater than zero) and the Y-axis varies upon the length of the file and the byte distribution. The code is as follows:
for (int i = 0; i < byteDistribution.Count; i++)
{
if (byteDistribution[i] > 0)
{
Series series = new Series(i.ToString());
series.Points.AddXY(i, byteDistribution[i]);
// PointWidth has no affect?
series.SetCustomProperty("PointWidth", "1");
this.crtBytes.Series.Add(series);
}
Questions:
This works well but the way the chart is shown is not to my liking.
I would like each bar to fill in as much space as possible (ie. no
margin / border). From what I've read elsewhere it was suggested to
use PointWidth or PixelPointWidth but none of these approaches is
working.
Is there a way to remove the inner black grid lines from
showing? Ideally, I would like the bottom X-axis numbering to remain just the same, but remove the grid lines.
For removing the gaps:
series["PointWidth"] = "1";
For removing the gridlines:
chartArea.AxisX.MajorGrid = new FChart.Grid {Enabled = false};
chartArea.AxisY.MajorGrid = new FChart.Grid { Enabled = false };
UPDATE:
I think your problem is that you create a new series for each data point. So you also get the "color effect". Just create ONE series, add it to the chart area and add all data points to this series.
Series series = new Series();
this.crtBytes.Series.Add(series);
series.SetCustomProperty("PointWidth", "1");
for (int i = 0; i < byteDistribution.Count; i++)
{
if (byteDistribution[i] > 0)
{
series.Points.AddXY(i, byteDistribution[i]);
// PointWidth has no affect?
}
}
PointWidth property is a relative amount, try something like series["PointWidth"] = 1.25.
The black lines are called MajorGrid, use chartArea.MajorGrid.Enabled = False.

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:

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

Visually formatting chart issue

This is an incredibly localized problem but I've spent a while on it now and just can't get the formatting to work. Basically what I want to do is show a column chart for a value but no bells and whistles (i.e. no labeling, no title, no legend, nothing!) And I want it to look like this:
But instead it looks like this:
The axis extends out and to the right and I can't figure out how to make it go away. Here is my code:
charts[i].Series.Clear();
charts[i].Series.Add("Block " + i + 1);
charts[i].Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
charts[i].Series[0].Points.Add(liveData[i]);
charts[i].ChartAreas[0].AxisY.Maximum = 4;
charts[i].ChartAreas[0].AxisY.Minimum = 0;
charts[i].ChartAreas[0].AxisX.Maximum = 1;
charts[i].ChartAreas[0].AxisX.Minimum = 1;
charts[i].ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
charts[i].ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
charts[i].ChartAreas[0].AxisX.LabelStyle.Enabled = false;
charts[i].ChartAreas[0].AxisY.LabelStyle.Enabled = false;
charts[i].ChartAreas[0].AxisY.LineWidth = 0;
charts[i].ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
charts[i].ChartAreas[0].AxisX.MajorTickMark.Enabled = false;
charts[i].Series[0].IsVisibleInLegend = false;
Now I am guessing this is a min/max thing but for the life of me I just can't get it to work. Can someone see my mistake? Or suggest another method besides charts?
Also note the 'Block 1 (V)' label you see underneath is not generated by the chart, it is a textbox label that just happened to get cut into the screenshot.
Thanks!
Your data has the x-value of 1. By default the bar is centered on the 1 mark. by shifting the limits you are actually only seeing half of it. Two things to do: Tell the bar exactly how wide it shall be and limit the x-axes accordingly.
charts[i].Series[0].CustomProperties = "PointWidth = 1"; // One bar takes a width of 1 unit on the x-axis
charts[i].ChartAreas[0].AxisX.Minimum = 0.5; // change!
charts[i].ChartAreas[0].AxisX.Maximum = 1.5; // change!

Categories

Resources