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

Last Reply 10 months ago By elvisidrizi1

Posted 10 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: dharmendr 10 months ago.
Posted 10 months ago

Hi elvisidrizi1,

Check this example. Now please take its reference and correct your code.



using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms.DataVisualization.Charting;


Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Windows.Forms.DataVisualization.Charting



public partial class Form1 : Form
    public Form1()

    private void Form1_Load(object sender, EventArgs e)
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] {
            new DataColumn("ShipCity"), new DataColumn("Total", typeof(int)) });
        dt.Rows.Add("Campinas", 9);
        dt.Rows.Add("Resende", -9);
        dt.Rows.Add("Rio de Janeiro", 34);
        dt.Rows.Add("Sao Paulo", 31);

        string[] x = (from p in dt.AsEnumerable()
                        orderby p.Field<string>("ShipCity") ascending
                        select p.Field<string>("ShipCity")).ToArray();
        int[] y = (from p in dt.AsEnumerable()
                    orderby p.Field<string>("ShipCity") ascending
                    select p.Field<int>("Total")).ToArray();

        Chart1.Series[0].IsVisibleInLegend = false;
        Chart1.Series[0].ChartType = SeriesChartType.Column;
        Chart1.Series[0].IsValueShownAsLabel = true;
        Chart1.Series[0].Points.DataBindXY(x, y);
        Chart1.ChartAreas["ChartArea1"].AxisY.Crossing = 0;
        Chart1.ChartAreas["ChartArea1"].AxisX.Crossing = 0;

        foreach (DataPoint point in Chart1.Series[0].Points)
            if (point.YValues[0] >= 0)
                point.Color = Color.Green;
                point.Color = Color.Red;


Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(1) {New DataColumn("ShipCity"), New DataColumn("Total", GetType(Integer))})
        dt.Rows.Add("Campinas", 9)
        dt.Rows.Add("Resende", -9)
        dt.Rows.Add("Rio de Janeiro", 34)
        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()

        Chart1.Series(0).IsVisibleInLegend = False
        Chart1.Series(0).ChartType = SeriesChartType.Column
        Chart1.Series(0).IsValueShownAsLabel = True
        Chart1.Series(0).Points.DataBindXY(x, y)
        Chart1.ChartAreas("ChartArea1").AxisY.Crossing = 0
        Chart1.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
    End Sub
End Class