how to update values on axes of point chart in c# - c#

In my project im having a point chart .For each and every 1 second time interval im updating the values in the points chart that's works fine.But while updating the values it is automatically splitting 4 values on x axis like 1,2,3,4 (1,2,3,4 are x axis values).But i want 10 fixed values to be plot on x axis.How to do it?
Please refer my code below ,
series.Name = "Series";
series.Color = System.Drawing.Color.Green;
series.IsVisibleInLegend = false;
series.IsXValueIndexed = true;
series.YAxisType=AxisType.Primary;
series.ChartType = SeriesChartType.Point;
this.chart.Series.Add(series);
chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisY2.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisX.Title = "X Axis value";
chart.ChartAreas[0].AxisY.Title = "Y Axis value1";
chart.ChartAreas[0].AxisY2.Title = "Y Axis value2";
chart.ChartAreas[0].AxisX.ScrollBar.Size = 15;
chart.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
chart.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;
chart.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
chart.ChartAreas[0].AxisX.ScaleView.Zoom(2,3);
chart.ChartAreas[0].CursorX.IsUserEnabled = true;
chart.ChartAreas[0].CursorY.IsUserEnabled = true;
chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
chart.ChartAreas[0].AxisY2.ScaleView.Zoomable = true;
chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;

I am assuming you want your x axis shows 0-10 range:
Try add follow codeļ¼š
chart.ChartAreas[0].AxisX.Maximum = 10;
chart.ChartAreas[0].AxisX.Minimum = 0;

If you use horizontal scroll bar for your chart,use the following code to display 10 intervals on x-axis.
chart.ChartAreas[0].AxisX.ScaleView.Zoom(2, 9);

Related

C# Chart issue with space between columns and font size Y axis

I am struggling (I am a beginner in C#) to: 1 put some space between the columns, define a size for the Y axis and display Ancillary in the label. I post my code, any help is largely appreciate.
Overall.SelectCommand = ToChart1;
Overall.Fill(ds, "TblChart1");
chart1.DataSource = ds.Tables["TblChart1"];
chart1.Series.Add("Series2");
chart1.Series.Add("Series3");
//chart1.Series["Series1"].XValueMember = "Ancillary";
chart1.Series["Series1"].YValueMembers = "Ancillary";
//chart1.Series["Series2"].XValueMember = "NonCommodity";
chart1.Series["Series2"].YValueMembers = "NonCommodity";
//chart1.Series["Series3"].XValueMember = "Totale";
chart1.Series["Series3"].YValueMembers = "Totale";
chart1.DataBind();
chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Calibri", 5);
chart1.ChartAreas[0].AxisX.Interval = 1;
//chart1.ChartAreas[0].AxisY.Maximum = 800000;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.Series[0].LegendText = "Ancillary";
chart1.Series[1].LegendText = "NonCommodity";
chart1.Series[2].LegendText = "Total";

WinForms: highlighting X and Y axes

I'm making a program to graph parabolas, and I want to make the X and Y axes (the ones from (0,0)) a different color. I haven't found any options to do so, and the only solution I've found is to make a large grid and set its increment to half the graph's size. Is there an alternative?
I used the default chart control. I'd expect something like:
You can set Crossing for axis to move the axis to center of chart. Also you can set LineWidth for axis to make it thicker. Also you can set ArrowStyle to have an arrow at the end of axis.
For example, to have a chart like this:
Use such code:
private void Form1_Load(object sender, EventArgs e)
{
//Set Chart Margins
this.chart1.ChartAreas[0].Position.Auto = false;
this.chart1.ChartAreas[0].Position.X = 10;
this.chart1.ChartAreas[0].Position.Y = 10;
this.chart1.ChartAreas[0].Position.Width = 80;
this.chart1.ChartAreas[0].Position.Height = 80;
//Configure X Axis
this.chart1.ChartAreas[0].AxisX.Crossing = 0;
this.chart1.ChartAreas[0].AxisX.Interval = 1;
this.chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
this.chart1.ChartAreas[0].AxisX.LineWidth = 2;
this.chart1.ChartAreas[0].AxisX.ArrowStyle =
System.Windows.Forms.DataVisualization.Charting.AxisArrowStyle.Lines;
//Configure Y Axis
this.chart1.ChartAreas[0].AxisY.Crossing = 0;
this.chart1.ChartAreas[0].AxisY.Interval = 5;
this.chart1.ChartAreas[0].AxisY.LineWidth = 2;
this.chart1.ChartAreas[0].AxisY.LabelStyle.Enabled = false;
this.chart1.ChartAreas[0].AxisY.ArrowStyle =
System.Windows.Forms.DataVisualization.Charting.AxisArrowStyle.Lines;
//Set Chart Type
this.chart1.Series[0].ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
//Set Data
var p = new List<PointF>();
for (int i = -5; i <= 5; i++)
{
p.Add(new PointF(i, i * Math.Abs(i)));
}
this.chart1.DataSource = p;
this.chart1.Series[0].XValueMember = "X";
this.chart1.Series[0].YValueMembers = "Y";
this.chart1.Series[0].IsVisibleInLegend = false;
}

Asp.net chart, how can I set the X axis label position to left aligned instead of centered?

I've spent hours trying to solve this silly problem. I create an histogram with asp chart control. All I want to do is have the xaxis label on the left of the column instead of centered on it. Xaxis lable doesn't seem to have a position property like series do, so I can't figure it out and it's frustrating.
Here's a sample code of the type of graphic I'm talking about to show you what I get approximately:
private void Graphique()
{
// Creating the series
Series series2 = new Series("Series2");
// Setting the Chart Types
series2.ChartType = SeriesChartType.Column;
// Adding some points
series2.Points.AddXY(1492, 12);
series2.Points.AddXY(2984, 0);
series2.Points.AddXY(4476, 1);
series2.Points.AddXY(5968, 2);
series2.Points.AddXY(7460, 2);
series2.Points.AddXY(8952, 12);
series2.Points.AddXY(10444, 4);
series2.Points.AddXY(11936, 3);
series2.Points.AddXY(13428, 3);
series2.Points.AddXY(14920, 5);
series2.Points.AddXY(16412, 1);
Chart3.Series.Add(series2);
Chart3.Width = 600;
Chart3.Height = 600;
// Series visual
series2.YValueMembers = "Frequency";
series2.XValueMember = "RoundedValue";
series2.BorderWidth = 1;
series2.ShadowOffset = 0;
series2.IsXValueIndexed = true;
// Setting the X Axis
Chart3.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;
Chart3.ChartAreas["ChartArea1"].AxisX.Interval = 1;
Chart3.ChartAreas["ChartArea1"].AxisX.Maximum = Double.NaN;
Chart3.ChartAreas["ChartArea1"].AxisX.Title = "kbps";
// Setting the Y Axis
Chart3.ChartAreas["ChartArea1"].AxisY.Interval = 2;
Chart3.ChartAreas["ChartArea1"].AxisY.Maximum = Double.NaN;
Chart3.ChartAreas["ChartArea1"].AxisY.Title = "Frequency";
}
Now my real chart looks like this, Actual result
I would like something similar to this website :
Desired layout chart
You see, the x label is on the left, which makes way more sense considering that each column of an histogram represents the frequency of a range of values.....
Any help would be appreciated...
Did you try to add CustomLabels to replace the default ones? For example:
for (int i = 0; i <= 10; i++) {
area.AxisX.CustomLabels.Add(i + 0.5, i + 1.5, i, 0, LabelMarkStyle.None);
}
The first two are for positioning and the third would be the text value of the label.

mschart horizontal scroll causing unwanted chart area width change

When I scroll horizontally a zoomed mschart I see an ugly chart area flicker (right border), caused by unwanted change of its width (this is due to variable scaling of the chart during horizontal scrolling).
Any ideas how to improve that?
Code example:
DateTime zeroTime = new DateTime(1, 1, 1, 0, 0, 0);
int k = 0;
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Dock = DockStyle.Fill;
chart1.BackColor = Color.Salmon;
this.chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
this.chart1.ChartAreas["ChartArea1"].CursorY.IsUserEnabled = true;
this.chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
this.chart1.ChartAreas["ChartArea1"].CursorY.IsUserSelectionEnabled = true;
this.chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true;
this.chart1.ChartAreas["ChartArea1"].AxisY.ScaleView.Zoomable = true;
this.chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.IsPositionedInside = true;
this.chart1.ChartAreas["ChartArea1"].AxisY.ScrollBar.IsPositionedInside = true;
this.chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.SmallScrollMinSizeType =DateTimeIntervalType.Seconds;
this.chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.SmallScrollSizeType = DateTimeIntervalType.Seconds;
chart1.ChartAreas["ChartArea1"].CursorY.Interval = 0.1;
chart1.ChartAreas["ChartArea1"].CursorX.Interval = 5.0;
chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "HH:mm:ss";
chart1.ChartAreas["ChartArea1"].CursorX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Seconds;
for (int i = 0; i < 600; i++)
{if (i < 200 ){k=i/10;} else if(i<400){k=20;}else{k=(600-i)/10;}; chart1.Series[0].Points.AddXY(zeroTime.AddSeconds(i * 5), 0 - k);}
The resizing of the chart can be influenced by the InnerPlotPosition property of the chart area. The default here is InnerPlotPosition.Auto set to true and this leads to the width change.
The chart width change can be stopped by switching to manual mode:
this.chart1.ChartAreas["ChartArea1"].InnerPlotPosition.Auto = false;
// optional:
this.chart1.ChartAreas["ChartArea1"].InnerPlotPosition.X = 0; // all values are percentage
this.chart1.ChartAreas["ChartArea1"].InnerPlotPosition.Y = 0;
this.chart1.ChartAreas["ChartArea1"].InnerPlotPosition.Height = 100;
this.chart1.ChartAreas["ChartArea1"].InnerPlotPosition.Width = 100;

Y axis Zooming properly but X axis is not Zooming

I am trying to make a graph in which Y Axis contains memory of an executable and X axis contains time.
I have made the following code
minValue = DateTime.Now;
maxValue = DateTime.Now.AddMinutes(5);
chartArea1.AxisX.Minimum = minValue.ToOADate();
chartArea1.AxisX.Maximum = maxValue.ToOADate();
chartArea1.CursorX.IsUserEnabled = true;
chartArea1.CursorX.IsUserSelectionEnabled = true;
chartArea1.CursorY.IsUserEnabled = true;
chartArea1.CursorY.IsUserSelectionEnabled = true;
chartArea1.Name = "ChartArea1";
chartArea1.CursorY.AutoScroll = true;
chartArea1.CursorX.AutoScroll = true;
chartArea1.AxisX.ScrollBar.Enabled = true;
chartArea1.AxisY.ScrollBar.Enabled = true;
chart1.ChartAreas.Add(chartArea1)
Series newSeries = new Series("Series1");
newSeries.XValueType = ChartValueType.Time;
chart1.Series.Add(newSeries);
Now when after adding points when i am trying to zoom the selection line comes only for y axis and not for the x axis.Hence i am unable to zoom the x axis.Can anyone help on what can be the problem
It was not zooming due to the reason i was using XValueType as ChartValueType.Time.When Time is converted in double the integer part remains the same whereas only decimal part changes.Hence there was only one integer point on the X Axis an hence zooming was disabled

Categories