Link to home
Start Free TrialLog in
Avatar of printmedia
printmedia

asked on

Chart being cut off at the top in Visual Studio VB.NET

Hi all.

I have a Chart object in my Visual Studio windows application, I'm using VB.NET.

The problem I'm running into sometimes is when some of the columns have really low values compared to other columns with really high values, for example, one stacked column will have a Y value of 50 and another stacked column will have a Y value of 1,000 then the 1,000 stacked column will be cut off at the top. I have 2 series, the first series is a stacked chart that sums up the sales for each product line and the second series is a line chart type which is a sum of all the stacked columns for each year. From my screen shot you can see that it's cutting of the second series (line chart). Any help would be appreciated!

Here's my code to load the chart:

Private Sub LoadChartProductLine()

        Chart1.Series.Dispose()
        Chart1.Series.Clear()

Dim query As String = "SELECT ProductLine, SalesYear, Sales, TotalYearSales"
        query += " FROM CRMProductLineSales WHERE AccountID = '" & txtPMID.Text & "'ORDER BY ProductLine"
Dim dt As DataTable = GetData(query)

        If dt.Rows.Count = 0 Then
            Chart1.Visible = False
            Exit Sub
        Else
            Chart1.Visible = True
        End If

        'Get the DISTINCT product lines
        Dim productlines As List(Of String) = (From p In dt.AsEnumerable() Select p.Field(Of String)("ProductLine")).Distinct().ToList()
        Dim years As List(Of Integer) = (From p In dt.AsEnumerable() Select p.Field(Of Integer)("SalesYear")).Distinct().ToList()

        'Remove the Default Series
        If Chart1.Series.Count() = 1 Then
            Chart1.Series.Remove(Chart1.Series(0))
        End If

        'Loop through the product lines
        For Each productline As String In productlines
Dim x As Integer() = (From p In dt.AsEnumerable() Where p.Field(Of String)("ProductLine") = productline Order By p.Field(Of Integer)("SalesYear") Select p.Field(Of Integer)("SalesYear")).ToArray()

Dim y As Double() = (From p In dt.AsEnumerable() Where p.Field(Of String)("ProductLine") = productline Order By p.Field(Of Integer)("SalesYear") Select p.Field(Of Double)("Sales")).ToArray()

            'Add Series to the Chart.
            Chart1.Series.Add(New Series(productline))

Chart1.Series(productline).ChartType = SeriesChartType.StackedColumn
            Chart1.Series(productline).Points.DataBindXY(x, y)
            Chart1.Series(productline).ToolTip = "#VALY{C2}"
            Chart1.Series(productline).IsVisibleInLegend = True
            Chart1.Series(productline).CustomProperties = "DrawingStyle = Cylinder"
        Next

For Each year As String In years
Dim x As Integer() = (From p In dt.AsEnumerable() Where p.Field(Of Integer)("SalesYear") = year Order By p.Field(Of Integer)("SalesYear") Select p.Field(Of Integer)("SalesYear")).ToArray()

Dim y As Double() = (From p In dt.AsEnumerable() Where p.Field(Of Integer)("SalesYear") = year Order By p.Field(Of Integer)("SalesYear") Select p.Field(Of Double)("TotalYearSales")).ToArray()
            'Add Series to the Chart.
            Chart1.Series.Add(New Series(year))
            Chart1.Series(year).IsValueShownAsLabel = True

            Chart1.Series(year).SmartLabelStyle.Enabled = False
            Chart1.Series(year).Label = "#VALY{C2}"
            Chart1.Series(year).ChartType = SeriesChartType.Line
            Chart1.Series(year).Points.DataBindXY(x, y)
            Chart1.Series(year).IsVisibleInLegend = False
        Next

Chart1.ChartAreas(0).AxisX.MajorGrid.Enabled = False
        Chart1.ChartAreas(0).AxisY.MajorGrid.Enabled = False
        Chart1.ChartAreas(0).AxisY.LabelStyle.Enabled = False
        Chart1.ChartAreas(0).AxisY.LineWidth = 0
        Chart1.ChartAreas(0).AxisY.MajorTickMark.Enabled = False
        Chart1.Legends(0).Title = "Product Line"

Open in new window

Chart-cut-off.png
ASKER CERTIFIED SOLUTION
Avatar of Kelvin McDaniel
Kelvin McDaniel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial