VB.Net Box Plot Series Labels - vb.net

The following code creates a simple boxplot in VB.net. However, I cannot figure out how to change the x axis labels. Currently they are 1 and 2 but I need them to be text values. I also want all of the numeric lables removed:
Dim yVal As Double() = {55.62, 45.54, 73.45, 9.73, 88.42, 45.9, 63.6, 85.1, 67.2, 23.6}
Dim yVal2 As Double() = {35.62, 25.54, 43.45, 23.73, 43.42, 12.9, 23.6, 65.1, 54.2, 41.6}
Chart1.Series.Clear()
Chart1.Series.Add("BoxPlotSeries")
Chart1.Series.Add("1")
Chart1.Series("1").Points.DataBindY(yVal)
Chart1.Series.Add("2")
Chart1.Series("2").Points.DataBindY(yVal2)
Chart1.Series("1").Enabled = False
Chart1.Series("2").Enabled = False
Chart1.Series("BoxPlotSeries").ChartType = SeriesChartType.BoxPlot
Chart1.Series("BoxPlotSeries")("BoxPlotSeries") = "1;2"
Chart1.Series("BoxPlotSeries")("BoxPlotWhiskerPercentile") = "15"
Chart1.Series("BoxPlotSeries")("BoxPlotShowAverage") = "true"
Chart1.Series("BoxPlotSeries")("BoxPlotShowMedian") = "true"
Chart1.Series("BoxPlotSeries")("BoxPlotShowUnusualValues") = "true"

You have to add custom labels to a new ChartArea1, and then link the BoxPlotSeries to the ChartArea1 so that the axis can be controlled. There's actually more than this and I have added what will make it work. I am also including 2-point decimal precision ("N2") in the Y-axis value, which you can change to integer via using "= N0". I would also turn off both vertical and horizontal grids, since they look horrendous in mathematical plots. Place all the code below after your code:
'Create new chart area called "ChartArea1", link BoxPlotSeries to it
Dim ChartArea1 As New ChartArea
ChartArea1.Name = "ChartArea1"
Chart1.ChartAreas.Add(ChartArea1)
Chart1.Series("BoxPlotSeries").ChartArea = "ChartArea1"
'Add the custom labels "One" and "Two" to the X-axis
Chart1.ChartAreas("ChartArea1").AxisX.CustomLabels.Add((1) * 2D, 0.0, "One")
Chart1.ChartAreas("ChartArea1").AxisX.CustomLabels.Add((2) * 2D, 0.0, "Two")
'X-Axis
Chart1.ChartAreas("ChartArea1").AxisX.MajorGrid.Enabled = False
Chart1.ChartAreas("ChartArea1").AxisX.IntervalAutoMode = False
Chart1.ChartAreas("ChartArea1").AxisX.MajorTickMark.LineWidth = 3
Chart1.ChartAreas("ChartArea1").AxisX.LabelStyle.Angle = -90
Chart1.ChartAreas("ChartArea1").AxisX.IsLabelAutoFit = True
Chart1.ChartAreas("ChartArea1").AxisX.LabelStyle.IsEndLabelVisible = True
Chart1.ChartAreas("ChartArea1").AxisX.IsMarginVisible = True
'Y-Axis
Chart1.ChartAreas("ChartArea1").AxisY.MajorGrid.Enabled = False
Chart1.ChartAreas("ChartArea1").AxisY.LabelStyle.Format = "N2"
Chart1.ChartAreas("ChartArea1").AxisY.LabelStyle.Angle = 0
Chart1.ChartAreas("ChartArea1").AxisY.IntervalAutoMode = True
Chart1.ChartAreas("ChartArea1").AxisY.MajorTickMark.LineWidth = 3

Related

Unexpected TableLayoutPanel Behavour

Hi having some issues with some nested tablelayoutpanels in a custom control
I have a tablelayoutpanel in a tablelayoutpanel in a tablelayoutpanel Crazy i know but it keeps it uniform and ordered
The custom control consists of 2 tablelayoutpanels that is placed in a table layout panel on a form and in a preview area of my main form.
Having them set at design time works fine but dynamically adding / removing the topmost ones with 1 row 2 columns into the one below it that has 1 column and x rows only Seems to break the autosizing behavior I'm chasing.
So I want the cells & rows to resize automatically based on the contents in this case labels but still remain in a neat ordered layout
There's no docking anywhere in the hierarchy of controls just anchors here and there
Here's my code to add the tablelayoutpanels below
"https://i.stack.imgur.com/vRfhE.png"
Private Sub AddControl(ByRef Counter As Counter)
Dim Gpanel As New TableLayoutPanel
Dim tlabel As New Label
Dim clabel As New Label
Dim pad As Integer = Counter.Cpad
TLPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
Gpanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
Gpanel.AutoSize = True
Gpanel.GrowStyle = TableLayoutPanelGrowStyle.AddRows
Gpanel.BorderStyle = BorderStyle.FixedSingle
Gpanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
TLPanel.BorderStyle = BorderStyle.FixedSingle
Gpanel.Dock = DockStyle.None
Gpanel.Padding = New Padding(0)
Gpanel.Anchor = AnchorStyles.Top Or AnchorStyles.Left
Gpanel.BackColor = Color.Transparent
Gpanel.RowCount = 1
Gpanel.ColumnCount = 2
Gpanel.RowStyles.Add(New RowStyle(SizeType.AutoSize))
Gpanel.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
Gpanel.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
tlabel.Dock = DockStyle.None
clabel.Dock = DockStyle.None
tlabel.GetPreferredSize(Size.Empty)
clabel.GetPreferredSize(Size.Empty)
tlabel.Text = Counter.Clabel
clabel.Text = Counter.Ccount.ToString
tlabel.Padding = New Padding(pad)
clabel.Padding = New Padding(pad)
tlabel.Anchor = AnchorStyles.None
clabel.Anchor = AnchorStyles.None
tlabel.ForeColor = Color.FromName(Counter.Clcolor)
clabel.ForeColor = Color.FromName(Counter.Ccolor)
Dim fontstyle As New FontStyle
fontstyle = Counter.ClfontStyle
tlabel.Font = New Font(Counter.Clfont, Counter.Clfontsize, fontstyle)
fontstyle = Counter.CcfontStyle
clabel.Font = New Font(Counter.Ccfont, Counter.Ccfontsize, fontstyle)
Gpanel.Controls.Add(tlabel, 0, 0)
Gpanel.Controls.Add(clabel, 1, 0)
TLPanel.Controls.Add(Gpanel, 0, Counter.ID)
TLPanel.RowCount += 1
TLPanel.RowStyles.Add(New RowStyle(SizeType.AutoSize))
Dim Styles As TableLayoutRowStyleCollection = TLPanel.RowStyles
Dim Cstyles As TableLayoutColumnStyleCollection = Gpanel.ColumnStyles
Gpanel.RowStyles.Item(0) = New RowStyle(SizeType.AutoSize)
TLPanel.ColumnStyles.Item(0) = New ColumnStyle(SizeType.AutoSize)
For i = 0 To Cstyles.Count - 1
Cstyles.Item(i) = New ColumnStyle(SizeType.AutoSize)
Next
For i = 0 To Styles.Count - 1
Styles.Item(i) = New RowStyle(SizeType.AutoSize)
Next
TLPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
AutoSize = True
AutoSizeMode = AutoSizeMode.GrowAndShrink
End Sub
'

Custom colors on bar chart using Zedgraph library

Am using Zedgraph chart library but I can't seem to be able to color a bar graph depending on a specific condition. Am following along with the example found on this tutorial.
In my case, if the value isn't above 50 -which is the student.pass_mark variable-, I want to color the bar red and if its above 50 I want to color it green. Below is my code. Which so far only gives me red even though I have values of 100, 80, 110 etc.
Dim subject As String
Dim grade As Decimal
Dim colors As Color() = {}
Dim subject_names As String() = {}
For i = 0 To student.no_of_subjects
ReDim Preserve colors(i)
ReDim Preserve subject_names(i)
subject = student.subject_name
grade = student.grade
Dim x As Double = CDbl(i) + 1
Dim y As Double = grade
Dim z As Double = 0
list.Add(x, y, z)
If grade < student.pass_mark Then
colors(i) = Color.Red
Else
colors(i) = Color.Green
End If
subject_names(i) = subject
Next
Dim myCurve As BarItem = myPane.AddBar("Student Subject", list, Color.Blue)
'Dim colors As Color() = {Color.Red, Color.Yellow, Color.Green, Color.Blue, Color.Purple}
myCurve.Bar.Fill = New Fill(colors)
myCurve.Bar.Fill.Type = FillType.Solid
myCurve.Bar.Fill.RangeMin = 0
myCurve.Bar.Fill.RangeMax = 4
myPane.Chart.Fill = New Fill(Color.White, Color.FromArgb(220, 220, 255), 45)
myPane.Fill = New Fill(Color.White, Color.FromArgb(255, 255, 225), 45)
' Tell ZedGraph to calculate the axis ranges
' Set the XAxis labels
myPane.XAxis.Scale.TextLabels = subject_names
' Set the XAxis to Text type
myPane.XAxis.Type = ZedGraph.AxisType.Text
ZedChart.IsShowPointValues = True
ZedChart.AxisChange()
ZedChart.Refresh()
Also, I want to draw a line across the whole chart that shows the pass_mark so that it is quickly visible that a student' has or hasn't passed a certain subject in comparison to the passmark
I don't know whether it is still relative but you can do multi-colored bar plots in ZedGraph.
You can find the tutorial here:
http://zedgraph.dariowiz.com/indexb806.html?title=Multi-Colored_Bar_Demo
In your case you can use FillType.GradientByColorValue I assume.
For the line you can simply add a LineObj to the myPane.GraphObjList

Aligning zeros in two Y Axis of DataVisualization.Charting.Chart

I have two series in a DataVisualization.Charting.Chart and two AxisY (AxisY1, and AxisY2). I was able to draw both series on a chart but the zeros for the left axis and right axis are not aligned. How can I align the zeros for the two axis?
NetChart.Series.Clear()
NetChart.Series.Add("Net")
NetChart.Series.Add("Net Cum.")
NetChart.Series(0).Points.Clear()
NetChart.Series(1).Points.Clear()
Dim netSaleDT As DataTable = SomeDataTable
netSaleDT.Columns.Add("CumulativeNet", GetType(Decimal))
Dim index As Integer = 0
Dim cumulSum As Decimal = 0
For Each drow As DataRow In netSaleDT.Rows
cumulSum += Convert.ToDecimal(drow("Net"))
drow("CumulativeNet") = cumulSum
NetChart.Series(0).Points.AddXY(drow("myMonth"), Convert.ToDecimal(drow("Net")))
NetChart.Series(1).Points.AddXY(drow("myMonth"), Convert.ToDecimal(drow("CumulativeNet")))
NetChart.Series(0).Points(index).AxisLabel = drow("myMonth")
index += 1
Next
NetChart.Series(0).YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Primary
NetChart.Series(1).YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary
NetChart.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
NetChart.ChartAreas(0).AxisX.Interval = 2
NetChart.ChartAreas(0).AxisY.LabelStyle.Format = "C"
NetChart.ChartAreas(0).AxisY2.LabelStyle.Format = "C"
NetChart.Series(1).BorderWidth = 2
NetChart.Series(1).Color = Color.Blue
NetChart.ChartAreas(0).AxisY2.Enabled = DataVisualization.Charting.AxisEnabled.True
NetChart.ChartAreas(0).AxisY2.LabelStyle.Enabled = True
Is the axis properties set to auto?
If they are you need to manually set each axis programatically.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.axis.minimum%28v=vs.110%29.aspx
When I set the min/max I usually do something like this:
Dim myChart As Excel.ChartObject = CType(xlCharts.Item(y), ChartObject)
Dim chartPage As Excel.Chart = myChart.Chart
chartPage.Axes(Excel.XlAxisType.xlCategory).minimumscale = minimum
chartPage.Axes(Excel.XlAxisType.xlCategory).maximumscale = maximum
This changes the x axis scale, to change the Y scale:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chartarea.axisy%28v=vs.110%29.aspx
and to change the secondary y scale:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chartarea.axisy2%28v=vs.110%29.aspx
Hope this helps
*Edit
Well i was trying to not give you the whole answer
But I suppose:
Dim myChart As Excel.ChartObject = CType(QoE.xLSheet1.ChartObjects(0), ChartObject)
Dim chartPage As Excel.Chart = myChart.Chart
chartPage.Axes(Excel.XlAxisGroup.xlPrimary).minimumscale = 0
chartPage.Axes(Excel.XlAxisGroup.xlSecondary).minimumscale = 0
Set the chart object to whatever chart you are trying to change, then set both the primary and secondary y axis to 0

MSCharts Last Axis Label Hiding

I'm using MSCharts and am using dates for the axes and couldn't help but notice that the axis label is hidden for the last grid line. The image below shows this.
The code that I am using for this is:
Public Function buildXAxis(ByVal chartArea As ChartArea, ByVal min As DateTime, ByVal max As DateTime) As Axis
Dim xAxis As New Axis(chartArea, AxisName.X) 'Chart Area is passed into the function
With xAxis
.Interval = 3 'This is the interval, so the next visible label should be 7/1/2013
.IntervalType = DateTimeIntervalType.Months
.IntervalAutoMode = IntervalAutoMode.FixedCount
.Maximum = max.ToOADate 'In this instance, it is 7/29/2013
.Minimum = min.ToOADate 'In this instance, it is 1/29/2013
.TitleAlignment = Drawing.StringAlignment.Center
.TitleForeColor = Drawing.Color.FromArgb(129, 127, 124)
.TextOrientation = TextOrientation.Auto
.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep45
Dim xLabelStyle As New LabelStyle
xLabelStyle.TruncatedLabels = False
xLabelStyle.IsStaggered = False
xLabelStyle.Format = "d"
.LabelStyle = xLabelStyle
.MajorGrid.LineColor = Drawing.Color.FromArgb(129, 127, 124)
.MinorGrid.LineColor = Drawing.Color.FromArgb(129, 127, 124)
.MajorTickMark.LineColor = Drawing.Color.FromArgb(129, 127, 124)
.MinorTickMark.LineColor = Drawing.Color.FromArgb(129, 127, 124)
End With
Return xAxis
End Function
Does anybody have any idea why those labels are hidden? According to the code, the gridlines are in the right place, (Every 3 months) but the axis labels simply not showing, and this is in fact the only place modifying this code
LabelStyle.IsEndLabelVisible controls whether the first and last label are displayed.
Here is the MSDN entry.
I'm guessing that somewhere in the code you didn't post is something along these lines:
Dim chartingArea As New ChartArea
chartingArea.AxisX.LabelStyle.IsEndLabelVisible = False
You'll want to change that to true or remove it entirely because it defaults to true.

vb.net bubble chart with gradient style in each buble to make it look like a ball

I have a bubble chart in vb.net with the code below and I would like to make the bubbles appear with a gradient stlyle to look like a ball (rather than a circle)
Dim xValues As Double() = {10.62, 75.54, 60.45}
Dim yValues As Double() = {650.62, 50.54, 600.45}
Dim size As Integer() = {10, 20, 30}
Dim names As String() = {"a", "b", "c"}
Chart5.Series("Series1").ChartType = SeriesChartType.Bubble
Chart5.Series("Series1").Points.DataBindXY(xValues, yValues, size, names)
Chart5.Series("Series1").MarkerStyle = MarkerStyle.Circle
For i = 0 To 2
Chart5.Series("Series1").Points(i).Label = names(i)
Next
I tried:
Chart_Analysis.Series("Series1").BackSecondaryColor = Drawing.Color.Green
Chart_Analysis.Series("Series1").MarkerColor = Drawing.Color.Blue
Chart_Analysis.Series("Series1").BackGradientStyle = GradientStyle.Center
... and
For i = Region_From To Region_To
Chart_Analysis.Series("Series1").Points(i).Label = names(i)
Chart_Analysis.Series("Series1").Points(i).BackGradientStyle = GradientStyle.Center
Chart_Analysis.Series("Series1").Points(i).Color = Drawing.Color.Aqua
Chart_Analysis.Series("Series1").Points(i).BackSecondaryColor = Drawing.Color.Green
Next
... but with no success
Any ideas on how to achive this 3d/ball effect?
Thanks
Try changing your 1st option to
Chart_Analysis.Series("Series1").BackSecondaryColor = Drawing.Color.Green
Chart_Analysis.Series("Series1").MarkerColor = Drawing.Color.Blue
Chart_Analysis.ChartAreas("Series1").BackGradientStyle = GradientStyle.Center
Note the change to ChartAreas on the third line. I have found that sometimes you think you should be coding against the series, but you should be using ChartAreas.