Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smaller margin on VB.NET Chart

Tags:

vb.net

charts

Is there a way to customize the margin between the first bar chart and the Y-Axis?

I'm aware that there is the possibility to set IsMarginVisible to False:

.AxisX.IsMarginVisible = False

But I don't want to remove the margin completely, I just would like to adjust it a bit. In âddition, I would like to adjust the margin between the "ticks" and the label text. Here is an example:

Here is how the chart looks right now

Old

And how it should look like (space in front and after the "ticks") New

Do you have an idea how to solve this problem?

like image 343
yannickpulver Avatar asked May 04 '16 07:05

yannickpulver


1 Answers

Unfortunately, it looks like that there is no margin property in the way I would have liked one. But I stumbled upon this article today: http://support2.dundas.com/Default.aspx?article=869

My workaround was to set the MajorTickMark to the size of the ticks I wanted to have + the margin. Then I set the color to transparent.

Chart1.ChartAreas(0).AxisY.MajorTickMark.Size = size
Chart1.ChartAreas(0).AxisY.MajorTickMark.LineColor = Color.FromArgb(0, 0, 0, 0)

After that, I just added a HorizontalLineAnnotation for every row in the size and place I wanted.

Dim minValue As Double = Chart1.ChartAreas("ChartArea").AxisY.Minimum
Dim maxValue As Double = Chart1.ChartAreas("ChartArea").AxisY.Maximum
Dim iteration As Integer = CInt((Math.Abs(minValue) + Math.Abs(maxValue )) / interval)

For i As Integer = 0 To iteration
    Dim line As New HorizontalLineAnnotation()
    With line
        .AxisX = Chart1.ChartAreas("ChartArea").AxisX
        .AxisY = Chart1.ChartAreas("ChartArea").AxisY
        .AnchorX = 0
        .Y = i * interval - Math.Abs(minValue)
        .AnchorOffsetX = offset
        .Height = 0
        .LineWidth = 1
        .Width = (5 / Chart1.Width.Value * 1240)
        .LineColor = Color.FromArgb(128, 128, 128)
        End With
    Chart1.Annotations.Add(line)
Next

With this workaround, I got the result I wanted.

like image 119
yannickpulver Avatar answered Oct 20 '22 16:10

yannickpulver