Aligning zeros in two Y Axis of DataVisualization.Charting.Chart - vb.net

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

Related

How do I set the X Axis to equal a predetermined range in excel using vb.net

I have been able to create a graph using vb.net. The data is being displayed correctly however the X axis is incrementing 1, 2, 3... How do I specify a range for the X axis.
My code is:
Dim chartPage As excel.Chart
Dim xlCharts As excel.ChartObjects
Dim myChart As excel.ChartObject
Dim chartRange As excel.Range
oSheet.Cells(2).columnwidth = 4.29
oSheet.Cells(3).ColumnWidth = 5.71
oSheet.Range("D1", "P1").ColumnWidth = 6.14
oSheet.Cells(17).ColumnWidth = 9.29
oSheet.Cells(18).ColumnWidth = 12
oSheet.Cells(19).ColumnWidth = 7.86
xlCharts = oSheet.ChartObjects
myChart = xlCharts.Add(715, 30, 672, 360)
chartPage = myChart.Chart
chartRange = oSheet.Range("D3", "S" + CStr(RowCount))
chartPage.SetSourceData(Source:=chartRange)
chartPage.ChartType = excel.XlChartType.xlLine
Here is the spreadsheet that is being created:
Spreadsheet and chart created
Rather than the bottom axis of the chart being 1 threw 23 I need it to be the values in the Weeks column. (C3:C26)

VB.Net Box Plot Series Labels

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

Creating a chart in VisualBasic.net which has a Y axis comprising of string values

I'd like to create a chart in VB.net which has X and Y values in the form of a string.
Example below:
Sorry for the bad drawing, but this is what I'd like it to do
Can anybody help me find the settings to do this?
I can transfer data to a graph but it ends up looking like this:
This is what my current graph looks like. As you can see, I can't get the Y axis working.
(also dont worry about the X axis containing the grades, that's just something I need to fix. The grades string() still contains the right data)
This is a sample of my code: (don't worry about delimitegrades(), that just formats data for the grades into 'A', 'B', etc)
Subjects is a string() list.
Grades is an array which contains the data I need to insert. ChrtSubgrade is the chart itself.
Public Sub CreateGraph(ByVal name As String, subjects() As String)
MsgBox("Generating graph for " & name)
chrtSubgrade.Series.Clear()
chrtSubgrade.Series.Add("Data")
chrtSubgrade.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Column
chrtSubgrade.Series(0).YValueType = DataVisualization.Charting.ChartValueType.String
chrtSubgrade.Series(0).IsValueShownAsLabel = True
delimitgrades(subjects)
For i = 0 To subjects.Count - 2
chrtSubgrade.Series(0).Points.AddXY(subjects(i), grades(i))
Next
I've breakpointed all the code and the arrays and data transfer is fine so I believe it's just down to how I'm creating the graph.
I can't link the chart to a database since I'm drawing the source data from an XML file.
Thanks very much for any help you might be able to give.
I figured out a new method. Instead of using the VB charting tools I made my own. Here's the code:
Public Sub CreateGraph(ByVal name As String, subjects() As String)
delimitgrades(subjects) 'collects the info from subjects which contains the grade data and make a new array from it containing only grades
MsgBox("Generating graph for " & name)
Dim mygraphics As Graphics = Graphics.FromHwnd(hwnd:=ActiveForm.Handle) 'defines a new graphics set on the Grades window
Dim axespen As New Pen(Color.Black, 5) 'makes a pen so I can draw the axes
Dim Xaxisleft As New Point(30, 350) 'defines the left point of the X axis
Dim Xaxisright As New Point(400, 350) 'defines the right point of the X axis
Dim Yaxisbottom As New Point(30, 350) 'defines the bottom point of the Y axis
Dim yaxistop As New Point(30, 80) 'defines the top point of the Y axis
For i = 0 To 4 'for each possible grade - A* through D
Dim labelgrade As New Label 'makes a label
With labelgrade 'with said label
.BackColor = Color.Transparent 'makes its background colourless
.AutoSize = True 'resizes the bounding box
Select Case i 'examines I - the counting variable
Case Is = 0 ' if its 0
.Text = "A*" ' sets the labels text to A*
.Location = New Point(2, 100) 'moves it to the right place
Case Is = 1 'etc
.Text = "A"
.Location = New Point(2, 140)
Case Is = 2
.Text = "B"
.Location = New Point(2, 180)
Case Is = 3
.Text = "C"
.Location = New Point(2, 220)
Case Is = 4
.Text = "D"
.Location = New Point(2, 260)
End Select '/etc
End With
Controls.Add(labelgrade) 'inserts the label into the form
Next
For i = 0 To subjects.Count - 2 'the last part of the subjects array is empty so it counts to the last position containing data
Dim labelsubject As New Label 'makes a new label
Dim labelxoffset As Integer = 30 'defines the variable xoffset which is used to determine where all labels should be placed
With labelsubject 'with this label
.BackColor = Color.Transparent 'make the background colourless
.AutoSize = True 'resize the bounding box
Select Case i 'examine i
Case Is = 0 'if this is the first label placed onto the form
.Text = subjects(i) 'take the first entry in the subjects array
.Location = New Point(30, 355) 'place the label on the X axis in the first position
Case Else 'otherwise
.Text = subjects(i) 'take the right name from the array and make the label reflect this name
.Location = New Point(labelxoffset + (i * 70), 365) 'set its location depending on which place it is in the array using xoffset
End Select
End With
Controls.Add(labelsubject) 'add the label to the form
Next
'Axes
mygraphics.DrawLine(axespen, Xaxisleft, Xaxisright) 'create
mygraphics.DrawLine(axespen, Yaxisbottom, yaxistop) 'the axes
'bars
For i = 0 To grades.Count - 1 'from 0 to the second to last entry in the grades array (this is because of how I formed the grades array. It still works.
Dim grade As String = grades(i) 'create a temp variable to store the correct entry from the grades array
Dim gradeindex As Integer = Nothing ' create the index integer, this is used to determine how high the bar should go
Dim gradepen As New Pen(Color.Green, 50) 'make the pen for the bars
Dim p1 As New Point(30, 350) 'define point 1 as above the first label
Dim p2 As New Point 'create point 2
Dim x As Integer = (58 + (70 * i)) 'create a new xoffset integer
Dim yoffset As Integer = 348 ' create the yoffset integer. This is to place the bottom point of the bar correctly
Select Case grade 'examine grade
Case Is = "A*" 'if its A*
gradeindex = 100 'the top y coord is 100
p1 = New Point(x, yoffset) 'p1 is now x, yoffset
p2 = New Point(x, gradeindex) 'p2 is now x, gradeindex
Case Is = "A" 'etc
gradeindex = 140
p1 = New Point(x, yoffset)
p2 = New Point(x, gradeindex)
Case Is = "B"
gradeindex = 180
p1 = New Point(x, yoffset)
p2 = New Point(x, gradeindex)
Case Is = "C"
gradeindex = 220
p1 = New Point(x, yoffset)
p2 = New Point(x, gradeindex)
gradepen = New Pen(Color.Orange, 50) 'make the grade pen orange
Case Is = "D"
gradeindex = 260
p1 = New Point(x, yoffset)
p2 = New Point(x, gradeindex)
gradepen = New Pen(Color.Red, 50) 'make the grade pen red
End Select '/etc
mygraphics.DrawLine(gradepen, p1, p2) 'draw the line from p1 to p2
Next
End Sub
Here's the output for someone who has the following data: German;A* , Maths;A , French;A* , Bio;C , Bus(business for example);B
Output
Hope this helps anyone else having this issue. Thanks!

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

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.