Force all Data Label to display on the MS Chart using C# and VB.Net in Console Application

vijay9471
 
on Jun 21, 2021 06:12 AM
1308 Views

Hello,

I am using below link of code for chart binding. It was working fine for hard coded data. When I use my actual application data it is messing up with duplicate values if DataTable count is 10 from 10 to 6 rows are getting duplicated.

Can you please help me? If we take example in y axis as a b cd and 4 rows and there value for all is same 7.

If we give it as a b cd and their values as 7 then in chart it is coming as abc and d is missing please help me with this issue. It is very urgent for me.

Change column Chart bar color based on condition using C# and VB.Net in Console Application

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 22, 2021 12:06 AM

Hi vijay9471,

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

Namespaces

C#

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

VB.Net

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

Code

C#

static void Main(string[] args)
{
    DataSet dataSet = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.Add("Text", typeof(string));
    dt.Columns.Add("Value", typeof(int));
    dt.Rows.Add("First", 8);
    dt.Rows.Add("Second", 15);
    dt.Rows.Add("Third", 31);
    dt.Rows.Add("Four", 25);
    dt.Rows.Add("Five", 30);
    dt.Rows.Add("Six", 14);
    dt.Rows.Add("Seven", 22);
    dt.Rows.Add("Eight", 39);
    dt.Rows.Add("Nine", 18);
    dt.Rows.Add("Ten", 32);
    dataSet.Tables.Add(dt);

    Chart chart = new Chart();
    chart.DataSource = dataSet.Tables[0];
    chart.Width = 600;
    chart.Height = 350;

    Series serie1 = new Series();
    serie1.Name = "Serie1";
    serie1.BorderColor = Color.FromArgb(164, 164, 164);
    serie1.ChartType = SeriesChartType.Bar;
    serie1.BorderDashStyle = ChartDashStyle.Solid;
    serie1.BorderWidth = 1;
    serie1.ShadowColor = Color.FromArgb(128, 128, 128);
    serie1.ShadowOffset = 1;
    serie1.IsValueShownAsLabel = true;
    serie1.XValueMember = "Text";
    serie1.YValueMembers = "Value";
    serie1.Font = new Font("Tahoma", 8.0f);
    serie1.BackSecondaryColor = Color.FromArgb(0, 102, 153);
    serie1.LabelForeColor = Color.FromArgb(100, 100, 100);
    chart.Series.Add(serie1);

    ChartArea ca = new ChartArea();
    ca.Name = "ChartArea1";
    ca.BackColor = Color.White;
    ca.BorderColor = Color.FromArgb(26, 59, 105);
    ca.BorderWidth = 0;
    ca.BorderDashStyle = ChartDashStyle.Solid;
    ca.AxisX = new Axis();
    ca.AxisY = new Axis();
    chart.ChartAreas.Add(ca);
    chart.DataBind();

    chart.ChartAreas["ChartArea1"].AxisX.Interval = 1;

    foreach (DataPoint point in chart.Series[0].Points)
    {
        if (point.YValues[0] < 15)
        {
            point.Color = Color.Yellow;
        }
        if (point.YValues[0] > 15 && point.YValues[0] < 30)
        {
            point.Color = Color.Blue;
        }
        if (point.YValues[0] > 30)
        {
            point.Color = Color.Red;
        }
    }
    chart.SaveImage(@"C:\myChart.png", ChartImageFormat.Png);
}

VB.Net

Sub Main(ByVal args As String())
    Dim dataSet As DataSet = New DataSet()
    Dim dt As DataTable = New DataTable()
    dt.Columns.Add("Text", GetType(String))
    dt.Columns.Add("Value", GetType(Integer))
    dt.Rows.Add("First", 8)
    dt.Rows.Add("Second", 15)
    dt.Rows.Add("Third", 31)
    dt.Rows.Add("Four", 25)
    dt.Rows.Add("Five", 30)
    dt.Rows.Add("Six", 14)
    dt.Rows.Add("Seven", 22)
    dt.Rows.Add("Eight", 39)
    dt.Rows.Add("Nine", 18)
    dt.Rows.Add("Ten", 32)
    dataSet.Tables.Add(dt)
    Dim chart As Chart = New Chart()
    chart.DataSource = dataSet.Tables(0)
    chart.Width = 600
    chart.Height = 350
    Dim serie1 As Series = New Series()
    serie1.Name = "Serie1"
    serie1.BorderColor = Color.FromArgb(164, 164, 164)
    serie1.ChartType = SeriesChartType.Bar
    serie1.BorderDashStyle = ChartDashStyle.Solid
    serie1.BorderWidth = 1
    serie1.ShadowColor = Color.FromArgb(128, 128, 128)
    serie1.ShadowOffset = 1
    serie1.IsValueShownAsLabel = True
    serie1.XValueMember = "Text"
    serie1.YValueMembers = "Value"
    serie1.Font = New Font("Tahoma", 8.0F)
    serie1.BackSecondaryColor = Color.FromArgb(0, 102, 153)
    serie1.LabelForeColor = Color.FromArgb(100, 100, 100)
    chart.Series.Add(serie1)
    Dim ca As ChartArea = New ChartArea()
    ca.Name = "ChartArea1"
    ca.BackColor = Color.White
    ca.BorderColor = Color.FromArgb(26, 59, 105)
    ca.BorderWidth = 0
    ca.BorderDashStyle = ChartDashStyle.Solid
    ca.AxisX = New Axis()
    ca.AxisY = New Axis()
    chart.ChartAreas.Add(ca)
    chart.DataBind()

    chart.ChartAreas("ChartArea1").AxisX.Interval = 1

    For Each point As DataPoint In chart.Series(0).Points
        If point.YValues(0) < 15 Then
            point.Color = Color.Yellow
        End If
        If point.YValues(0) > 15 And point.YValues(0) < 30 Then
            point.Color = Color.Blue
        End If
        If point.YValues(0) > 30 Then
            point.Color = Color.Red
        End If
    Next
    chart.SaveImage("C:\myChart.png", ChartImageFormat.Png)
End Sub

Screenshot