Linear Axis Scale

Helpmeh

Member
Joined
Nov 10, 2014
Messages
13
Programming Experience
1-3
Hi,

First off, apologies if this is meant to be in the graphics parts of the forum- let me know and i will move it.

Anyway, i am creating a VB chart in a windows form, and basically no matter how hard i look, i cant find any code anywhere that allows me to force the x-axis scale to be linear.

Basically at the moment it is automatically choosing the x-axis scale based on the data. What i want it to do is in fact create the axes and then plot the data on the axis provided, or for it to just let me set the axis manually.

i have tried using the .intervals, and the constant i set it to is just displaying the corresponding number of x-axis values as a lebel on the axis rather than fixing the interval between each point.

Please help me fast! This is driving me mad! :hopelessness:
 
You can configure each axis in designer, click into ChartAreas collection, then its Axes collection. Axis.IsLogarithmic (in category Scale) is False by default.
 
You can configure each axis in designer, click into ChartAreas collection, then its Axes collection. Axis.IsLogarithmic (in category Scale) is False by default.

Unless i am missing something, why does the scale have to be logarithmic? if i want an x axis that goes 0 1 2 3 4 5 , but data points with an x value of 2.35 and 3.11, then why shouldnt i be able to do that?

Also it prints a giant red cross on the chart if i enable that option.
 
Unless i am missing something, why does the scale have to be logarithmic?
You asked for "Linear Axis Scale", and since a scale can only be linear or logarithmic I suggested where to look if you somehow didn't get a linear scale.
if i want an x axis that goes 0 1 2 3 4 5 , but data points with an x value of 2.35 and 3.11, then why shouldnt i be able to do that?
Setting Axis.Interval to 1 should do that (considering min/start by 0 and max 5). It is Auto by default.
i have tried using the .intervals, and the constant i set it to is just displaying the corresponding number of x-axis values as a lebel on the axis rather than fixing the interval between each point.
What?
Also it prints a giant red cross on the chart if i enable that option.
"red cross on the chart" usually mean an invalid setting causes chart painting to crash.
 
You asked for "Linear Axis Scale", and since a scale can only be linear or logarithmic I suggested where to look if you somehow didn't get a linear scale.

Setting Axis.Interval to 1 should do that (considering min/start by 0 and max 5). It is Auto by default.

What?

Okay well it definitely is possible apparently to create a non linear axis... which is what is frustrating me... http://i.imgur.com/tNWPFLT.png

Here is the code:

Private Sub CreateChart(ByRef XAxisValues() As String, YAxisValues() As String, iarrayindex As Integer)
Dim i As Integer, x As Integer, Y As Integer


Me.Size = New Size(1500, Me.Size.Height)


Chart1.ChartAreas(0).AxisX.Minimum = 0
Chart1.ChartAreas(0).AxisX.Maximum = 5.0
Chart1.ChartAreas(0).AxisX.Interval = 0.5




Chart1.ChartAreas(0).AxisY.Minimum = -5.0
Chart1.ChartAreas(0).AxisY.Maximum = 2
Chart1.ChartAreas(0).AxisY.Interval = 1




Me.Chart1.Series.Add("Series1")
Me.Chart1.Series("Series1").ChartType = DataVisualization.Charting.SeriesChartType.Point




For i = 1 To iarrayindex - 1
Chart1.Series("Series1").Points.AddXY(XAxisValues(i), YAxisValues(i))
Debug.Print(XAxisValues(i))
Next i


End Sub


FYI, there are 50 values stored in each array, and this is what the data should look like (ignoring the trendline):
http://i.imgur.com/S780BMG.png
 
Last edited:
With the first few data points the output with that code, with otherwise default Chart control, will be like this:
output.png
The first of your images must have other configuration (probably the Series IsXValueIndexed is turned on).

To get something like your second image you must set AxisX.Interval to 1 and configure AxisX/AxisY.MinorGrid.
Where x axis crosses y axis is set with AxisY.Crossing, and x axis labels angle with AxisX.LabelStyle.Angle.

All chart configuration that don't rely on runtime values is easiest set in designer, but if you must write code the With statement can be useful, here especially useful with nested objects:
        With Chart1.ChartAreas(0).AxisX
            .Minimum = 0
            .Maximum = 4.0
            .Interval = 1
            .LabelStyle.Angle = 30
            With .MinorGrid
                .Enabled = True
                .Interval = 0.5
                .LineColor = Color.LightGray
            End With
        End With
 
I just dont understand it, using that code or any other code to the same effect, the chart just seems to flat out ignore any parameters i tell it to set for the xaxis. This is such a simple concept i have no idea why its being such a pain. Also set .isvalueshownaslabel = false and .isxvalueindexed = false. Neither made any difference at all. Using your code i get this: http://i.imgur.com/XAMGess.png

For some reason vb, with the x axis, is treating the .maximum as the maximum number of elements it displays rather than maximum integer xvalue for the axis (as you can see in the image).

I was thinking that maybe it was using the for loop incorrectly, but then why are the Y values being plotted absolutely fine, and the x value arent. The x values are also correct, its just the axis being a pain. Also, i have tried using the .ChartAreas(0).RecalculateAxesScale() to no avail. This seems to be a really ridiculous problem.

BTW John i really appreciate the time you are taking to debug this problem.

Thanks
 
This seems to be a really ridiculous problem.
Have you tried deleting the Chart control and adding a new one that surely has default configuration?
 
Yes, no luck.
Doing that should give you trouble with this line:
Me.Chart1.Series.Add("Series1")
Because a new Chart control will already have a "Series1" object. How did you handle that problem?
 
Doing that should give you trouble with this line:

Because a new Chart control will already have a "Series1" object. How did you handle that problem?

Of course, because it already existed. I simply commented that line out so that it was adding points to the already existing 'series1'
 
Of course, because it already existed. I simply commented that line out so that it was adding points to the already existing 'series1'
That's what I did too. So with a "fresh" Chart control and your code (post 6) there is no reason you shouldn't get the same output as my image (post 7).
 
Okay in that case, i guess we are agreed that there is no reason why this shouldnt work. I will have a double check that no other process it affecting during runtime.

EDIT: Have checked and there simply isnt anything related to any kind of chart control at all in my code except the code above.

EDIT 2: The problem is using the .addXY control with the arrays in, this is what is causing the problem. Please can someone advise what i am doing wrong or a better method to add the datapoints based on these arrays?
 
Last edited:
So the answer was that the arrays need to be converted from string arrays to double, and the points need to be added via DatabindingXY.

I am so glad that this problem is finally dealt with. And i hope this helps someone in the future.

Thanks,

#Hero
 
Back
Top