ErrorBar with min and max values

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
Oli
Newbie
Newbie
Posts: 42
Joined: Fri Jan 13, 2012 12:00 am

ErrorBar with min and max values

Post by Oli » Fri Apr 13, 2012 10:33 am

Hi,

I am trying to add error bars to bar graphs presenting minimum and maximum. I understand that the upper and lower errorbar are always in equal size, and that makes sense charting errors. The distance between mean to minimum, and between mean and maximum are not always equal, in most cases unequal. So I declare two errorbars, upper and lower to chart maximum and minimum respectively. Unfortunetely, the two errorbars are displayed offset from each other. Is there a way to bring both in line?

Cheers
Oli

Here my code:

Dim ErrorbarMin As New Steema.TeeChart.Styles.Error()
Dim ErrorbarMax As New Steema.TeeChart.Styles.Error()


ErrorbarMax.Add(i, GraphData_Mean(i), GraphData_Max(i) - GraphData_Mean(i), labels(i))
ErrorbarMin.Add(i, GraphData_Mean(i), GraphData_Mean(i) - GraphData_Min(i), labels(i))

ErrorbarMax.Color = Color.Black
ErrorbarMax.HorizAxis = HorizontalAxis.Bottom
ErrorbarMax.ErrorPen.Visible = True
ErrorbarMax.ErrorPen.Width = 2
ErrorbarMax.ErrorPen.Color = Color.Black
ErrorbarMax.ErrorPen.Style = Drawing2D.DashStyle.Solid
ErrorbarMax.ErrorWidthUnits = ErrorWidthUnits.Percent
ErrorbarMax.ErrorWidth = 65
ErrorbarMax.ErrorStyle = ErrorStyles.Top
ErrorbarMax.Title = "Error bar, upper"

ErrorbarMin.Color = Color.Black
ErrorbarMin.HorizAxis = HorizontalAxis.Bottom
ErrorbarMin.ErrorPen.Visible = True
ErrorbarMin.ErrorPen.Width = 2
ErrorbarMin.ErrorPen.Color = Color.Black
ErrorbarMin.ErrorPen.Style = Drawing2D.DashStyle.Solid
ErrorbarMin.ErrorWidthUnits = ErrorWidthUnits.Percent
ErrorbarMin.ErrorWidth = 65
ErrorbarMin.ErrorStyle = ErrorStyles.Bottom
ErrorbarMin.Title = "Error bar, lower"

tChart.Series.Add(ErrorbarMax)
tChart.Series.Add(ErrorbarMin)
MinMaxErrorBar.PNG
MinMaxErrorBar.PNG (13.81 KiB) Viewed 10117 times

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: ErrorBar with min and max values

Post by Sandra » Fri Apr 13, 2012 1:58 pm

Hello Oli,

Can you please, send us a complete example where we can help you to achieve as you want?

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Oli
Newbie
Newbie
Posts: 42
Joined: Fri Jan 13, 2012 12:00 am

Re: ErrorBar with min and max values

Post by Oli » Sun Apr 15, 2012 10:22 pm

Hi Sandra,

I have attached a small example project to demonstrate the offset of two errorbars added to one bar. I would like to have the two errorbars aligned.

I am also wondering why the font size of labels in bottom and left axis is different, although I set both to a size of 11. :?:

Please let me know if you require further details on what I am trying to achieve. Thanks for your support :)

Cheers
Oli
Attachments
MinMaxErrorBar.zip
(77.7 KiB) Downloaded 494 times

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: ErrorBar with min and max values

Post by Sandra » Mon Apr 16, 2012 12:29 pm

Hello Oli,

Ok. I have made a simple code using a ErrorBar Series and Error Series where the errorbars are displayed in line

Code: Select all

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Initialize chart
        InitializeChart()

        Dim GraphXAxisTitle(2) As String
        GraphXAxisTitle = New String() {"A", "B", "C"}

        Dim GraphData(4, 2) As Double
        GraphData = New Double(,) {{1, 2, 3, 4, 5}, {8, 9, 11, 12, 15}, {22, 19, 21, 23, 17}}

        Dim GraphData_Mean(2) As Double
        GraphData_Mean(0) = 3
        GraphData_Mean(1) = 11
        GraphData_Mean(2) = 34

        Dim GraphData_Max(2) As Double
        GraphData_Max(0) = 5
        GraphData_Max(1) = 15
        GraphData_Max(2) = 23

        Dim GraphData_Min(2) As Double
        GraphData_Min(0) = 1
        GraphData_Min(1) = 8
        GraphData_Min(2) = 17

        ' Clearing  and Setting tchart1 properties
        tChart.Series.Clear()
        tChart.Axes.Bottom.Labels.Items.Clear()

        ' The labels for vertical axis
        Dim Ylabel(0) As String
        Ylabel(0) = "Y Title"
        tChart.Axes.Left.Title.Lines = Ylabel
        tChart.Axes.Left.Title.Text = Ylabel(0)
        tChart.Axes.Left.Title.Visible = True

        ' The labels for the chart
        Dim labels(GraphXAxisTitle.Length - 1) As String
        For i = 0 To GraphXAxisTitle.Length - 1
            labels(i) = GraphXAxisTitle(i)
        Next

        ' Set titles of graph
        tChart.Header.Text = "MinMax ErrorBar"
        tChart.Header.Visible = True
        tChart.Legend.Visible = False
        ' Set Axes
        tChart.Axes.Left.MinorTickCount = 0
        tChart.Axes.Left.Automatic = True
        tChart.Axes.Left.Labels.Font.Size = 11
        tChart.Axes.Bottom.MinorTickCount = 0
        tChart.Axes.Bottom.Automatic = True
        tChart.Axes.Bottom.Labels.Font.Size = 11
        tChart.Axes.Bottom.MinimumOffset = 50
        tChart.Axes.Bottom.MaximumOffset = 50
        tChart.Axes.Left.MinimumOffset = 0
        tChart.Axes.Top.Automatic = True
        tChart.Axes.Top.Visible = False
        tChart.Axes.Top.MinimumOffset = 50
        tChart.Axes.Top.MaximumOffset = 50

        For i = 0 To 2

            Dim errorBar As New Steema.TeeChart.Styles.ErrorBar(tChart.Chart)
            Dim error1 As New Steema.TeeChart.Styles.Error(tChart.Chart)
            'Error Bar Series
            errorBar.Pen.Color = Color.Black
            errorBar.ErrorPen.Width = 2
            errorBar.Brush.Solid = True
            errorBar.Brush.Color = Color.White
            errorBar.Title = labels(i)
            errorBar.Marks.Visible = False
            errorBar.ErrorStyle = ErrorStyles.Top
            errorBar.HorizAxis = HorizontalAxis.Bottom
            errorBar.MultiBar = MultiBars.None
            errorBar.Add(i, GraphData_Mean(i), GraphData_Max(i) - GraphData_Mean(i), labels(i))
            errorBar.BarWidthPercent = 40
            tChart.Axes.Bottom.Labels.Items.Add(i, errorBar.Title)
            ' Error Series 
            error1.ErrorPen.Width = 2
            error1.Brush.Solid = True
            error1.Color = Color.Black
            error1.Marks.Visible = False
            error1.Title = labels(i)
            error1.ErrorStyle = ErrorStyles.Bottom
            error1.HorizAxis = HorizontalAxis.Top
            error1.MultiBar = MultiBars.None
            error1.ShowInLegend = False
            error1.Add(i, GraphData_Mean(i), GraphData_Mean(i) - GraphData_Min(i), labels(i))
            error1.BarWidthPercent = 40
        Next
        tChart.Chart.Tag = "MeanBarError"
        Me.Controls.Add(tChart)
    End Sub
Can you tell us if previous code works as you want?

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Oli
Newbie
Newbie
Posts: 42
Joined: Fri Jan 13, 2012 12:00 am

Re: ErrorBar with min and max values

Post by Oli » Wed Apr 18, 2012 11:04 pm

Hi Sandra,

your code works as long as I am using bar charts. Another type of charts I do in my application is a simple line with errorbars. I create the line with box plot, and set the box and whisker invisible, and the meanline to visible. Then I add two errorss, one for minimum and the other for maximum value. See attachment.

Is the behavior to offsetting the errors expected, or a bug which could be fixed in the near future?

And in your code, the font size of left and bottom axis seems to be different too, even so both are set to a size of 11. Any idea why?

Thanks again

Oli
Attachments
LineChartwithMinMax.PNG
LineChartwithMinMax.PNG (13.73 KiB) Viewed 10042 times

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: ErrorBar with min and max values

Post by Sandra » Thu Apr 19, 2012 11:53 am

Hello Oli,

I suggest do something as next code to achieve as you want:

Code: Select all

   Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        InitializeChart()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Private boxSeries1 As Steema.TeeChart.Styles.Box, boxSeries2 As Steema.TeeChart.Styles.Box, boxSeries3 As Steema.TeeChart.Styles.Box
    Private error1 As Steema.TeeChart.Styles.Error, error2 As Steema.TeeChart.Styles.Error
    Private Sub InitializeChart()

        TChart1.Aspect.View3D = False
        TChart1.Panel.MarginBottom = 10
        boxSeries1 = New Box(TChart1.Chart)
        boxSeries2 = New Box(TChart1.Chart)
        boxSeries3 = New Box(TChart1.Chart)
        error1 = New [Error](TChart1.Chart)
        error2 = New [Error](TChart1.Chart)

        'BoxSeries1
        boxSeries1.Add(New Double(5) {321, 625, 810, 150, 190, 210})
        boxSeries1.Position = 0
        CustomizeBoxSeries(boxSeries1)
        boxSeries1.Title = "A"
        boxSeries2.Add(New Double(3) {256, 456, 120, 78})
        boxSeries2.Position = 1
        CustomizeBoxSeries(boxSeries2)
        boxSeries2.Title = "B"
        boxSeries3.Add(New Double(4) {306, 100, 201, 789, 861})
        boxSeries3.Position = 2
        CustomizeBoxSeries(boxSeries3)
        boxSeries3.Title = "C"

        'Error1

        error1.Add(boxSeries1.Position, CalculateMedian(boxSeries1), 50)
        error1.Add(boxSeries2.Position, CalculateMedian(boxSeries2), 30)
        error1.Add(boxSeries3.Position, CalculateMedian(boxSeries3), 63)
        error1.ErrorPen.Width = 2
        error1.Color = Color.Black
        error1.ErrorWidth = boxSeries1.Box.HorizSize / 2
        error1.ErrorStyle = ErrorStyles.Top
        error1.HorizAxis = HorizontalAxis.Top
        error1.MultiBar = MultiBars.None
        error1.ShowInLegend = False
        'Error2
        error2.Add(boxSeries1.Position, CalculateMedian(boxSeries1), -10)
        error2.Add(boxSeries2.Position, CalculateMedian(boxSeries2), -50)
        error2.Add(boxSeries3.Position, CalculateMedian(boxSeries3), -33)
        error2.ErrorPen.Width = 2
        error2.Color = Color.Black
        error2.ErrorWidth = boxSeries1.Box.HorizSize / 2
        error2.ErrorStyle = ErrorStyles.Bottom
        error2.HorizAxis = HorizontalAxis.Bottom
        error2.MultiBar = MultiBars.None
        error2.ShowInLegend = False

        'Axes
        TChart1.Axes.Top.Visible = False

        TChart1.Axes.Bottom.Labels.Items.Add(0, boxSeries1.Title)
        TChart1.Axes.Bottom.Labels.Items.Add(1, boxSeries2.Title)
        TChart1.Axes.Bottom.Labels.Items.Add(2, boxSeries3.Title)
        TChart1.Axes.Bottom.Labels.Font.Size = 15
        TChart1.Draw()
        TChart1.Draw()
        TChart1.Draw()
    End Sub

    Private Function CalculateMedian(ByVal series As Box) As Double
        Dim median As Double
        median = 0.0
        Dim N As Integer = series.SampleValues.Count
        If N > 0 Then
            Dim InvN As Double = 1.0 / N
            ' calculate median 

            Dim med As Integer = N \ 2
            If (N Mod 2) = 0 Then
                median = 0.5 * (series.SampleValues(med - 1) + series.SampleValues(med))
            Else
                median = series.SampleValues(med)
            End If
        End If
        Return median
    End Function
    Private Sub CustomizeBoxSeries(ByVal boxSeries As Box)
        boxSeries.MedianPen.Color = Color.Black
        boxSeries.MedianPen.Width = 2
        boxSeries.MedianPen.Style = System.Drawing.Drawing2D.DashStyle.Solid
        boxSeries.WhiskerPen.Visible = False
        boxSeries.ExtrOut.Visible = False
        boxSeries.MildOut.Visible = False
        boxSeries.Box.HorizSize = 50
        boxSeries.Box.Visible = False
    End Sub
And in your code, the font size of left and bottom axis seems to be different too, even so both are set to a size of 11. Any idea why?
Sorry I forgot answer it. When the labels are custom you need change the size for each label as do in next lines of code:

Code: Select all

     tChart1.Axes.Bottom.Labels.Items(0).Font.Size = 11;
            tChart1.Axes.Bottom.Labels.Items(1).Font.Size = 11;
            tChart1.Axes.Bottom.Labels.Items(2).Font.Size = 11;
I hope will helps.
Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Oli
Newbie
Newbie
Posts: 42
Joined: Fri Jan 13, 2012 12:00 am

Re: ErrorBar with min and max values

Post by Oli » Sun Apr 22, 2012 11:34 am

Thanks Sandra. Errorbar with minimum and maximum (unsymmetric errorbars) works fine now on bars and boxplots. Many thanks for your help.

Oli

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: ErrorBar with min and max values

Post by Sandra » Tue Apr 24, 2012 7:44 am

Hello Oli,

I am glad that my suggestion has been helpful for you. :)

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply