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

elvisidrizi1
 
on Aug 05, 2020 11:38 PM
1304 Views

Hello Everyone,

I want to create a chart that has negative values on it and that it takes values directly from the text.box, 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,

Blessings

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 06, 2020 03:14 AM

Hi elvisidrizi1,

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

Namespaces

C#

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

VB.Net

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

Code

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    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;
            }
            else
            {
                point.Color = Color.Red;
            }
        }
    }
}

VB.Net

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
            Else
                point.Color = Color.Red
            End If
        Next
    End Sub
End Class

Screenshot

elvisidrizi1
 
on Aug 06, 2020 04:18 AM
on Aug 06, 2020 04:19 AM

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
            Else
                point.Color = Color.Red
            End If
        Next

THank you