Hi vijay9471,
Check this example. Now please take its reference and correct your code.
Add references of System.Drawing, System.Windows.Forms.DataVisualization and System.Windows.Forms in the project.
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)
{
//populate dataset with some demo data
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", 5);
dataSet.Tables.Add(dt);
//prepare chart control.
Chart chart = new Chart();
chart.DataSource = dataSet.Tables[0];
chart.Width = 600;
chart.Height = 350;
//create serie.
Series serie1 = new Series();
serie1.Name = "Serie1";
serie1.BorderColor = Color.FromArgb(164, 164, 164);
serie1.ChartType = SeriesChartType.Column;
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);
//create chartareas.
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);
//databind.
chart.DataBind();
foreach (DataPoint point in chart.Series[0].Points)
{
// Set Color on condition.
if (point.YValues[0] > 8)
{
point.Color = Color.Green;
}
else
{
point.Color = Color.FromArgb(112, 255, 200);
}
}
//save result.
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", 5)
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.Column
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()
For Each point As DataPoint In chart.Series(0).Points
' Set Color on condition.
If point.YValues(0) > 8 Then
point.Color = Color.Green
Else
point.Color = Color.FromArgb(112, 255, 200)
End If
Next
chart.SaveImage("C:\myChart.png", ChartImageFormat.Png)
End Sub
Screenshot