Create Bar Chart with negative value using C# and VB.Net in Windows Application

Last Reply 9 months ago By elvisidrizi1

Posted 9 months ago

Hello Everyone,

I want to create a chart that has negative values on it and that it takes values directly from the, I have been googling about these issues but there is nothing useful for this purpose.

I want to be a bar-chart with negative values on it and if the values are negative then to be red and if it's positive to be green.

Thank you for the help,


You are viewing reply posted by: elvisidrizi1 9 months ago.
Posted 9 months ago Modified on 9 months ago

Thank you for your reply and help, I take values directly from textbox.text so i treid this way :

    Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(1) {New DataColumn("ShipCity"), New DataColumn("Total", GetType(Integer))})
        dt.Rows.Add("Al", Convert.ToDecimal(TextBox1.Text.Trim().Replace(".", ",").Replace("€", "")))
        dt.Rows.Add("En", Convert.ToDecimal(TextBox2.Text.Trim().Replace(".", ",").Replace("€", "")))
        dt.Rows.Add("Els", Convert.ToDecimal(TextBox3.Text.Trim().Replace(".", ",").Replace("€", "")))
        dt.Rows.Add("Sao Paulo", 31)

        Dim x As String() = (From p In dt.AsEnumerable()
                             Order By p.Field(Of String)("ShipCity")
                             Select p.Field(Of String)("ShipCity")).ToArray()
        Dim y As Integer() = (From p In dt.AsEnumerable()
                              Order By p.Field(Of String)("ShipCity")
                              Select p.Field(Of Integer)("Total")).ToArray()

        Chart2.Series(0).IsVisibleInLegend = False
        Chart2.Series(0).ChartType = SeriesChartType.Column
        Chart2.Series(0).IsValueShownAsLabel = True
        Chart2.Series(0).Points.DataBindXY(x, y)
        Chart2.ChartAreas("ChartArea1").AxisY.Crossing = 0
        Chart2.ChartAreas("ChartArea1").AxisX.Crossing = 0
        For Each point As DataPoint In Chart1.Series(0).Points
            If point.YValues(0) >= 0 Then
                point.Color = Color.Green
                point.Color = Color.Red
            End If

THank you