Reputation: 53
I am new to c# programming and creating my first Windows forms application. I have a two 1-D Arrays, one represents the X Axis Values and the other represents Y axis.
I am trying to create a graph from them using the following code.
public void drawgraph()
{
chart1.Series["Series1"].Name = MemsName;
for (int i = 0; i < VmicArray.Length; i++)
{
chart1.Series[MemsName].Points.AddXY(VmicArray[i], SensitivityArray[i]);
}
chart1.ChartAreas[0].AxisX.Title = "Vmic Value";
chart1.ChartAreas[0].AxisY.Title = "Sensitivity";
}
I am getting the XAxis values which I have in Array(like -2.333333754 or 6.46870) with the interval of 5 which I have set. The range of X axis is have is from -4 to +8.
Is there anyone who can help me in getting a ouput like we get in Excel Graphs? I am trying for Long time always I mess up with the values of X axis.
I Need the graph with XAxis value from -10 to +10 with the interval of 1 and mapping the Y values to corresponding X values on graph.
Unfortunately I am not able to post any Images :(
Upvotes: 1
Views: 1985
Reputation: 6375
If you want to project a certain range of values to another range you need to use linear interpolation between the values. First determine the old min and max values (MinX
and MaxX
) and define the new limits (-10, 10).
Then use a simple formula to determine the new x value of an arbitrary old value.
double MinX = VmicArray.min;
double MaxX = VmicArray.Max;
double NewMin = -10;
double NewMax = 10;
for (i = 0; i <= VmicArray.Count - 1; i++) {
// Linear interpolation
double NewX = VmicArray(i) / (MaxX - MinX) * (NewMax - NewMin) + NewMin;
}
Recalculate each X value before you use AddXY
.
In order to just change the visible bounds of each axis you can use the XAxis.Minimum
and XAxis.Maximum
as well as the XAxis.Interval
properties:
chart1.ChartAreas[0].AxisX.Minimum = -10;
chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisX.Interval = 1;
Upvotes: 1