Hi vijay9471,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.Drawing;
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Windows.Forms.DataVisualization.Charting;
VB.Net
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Net
Imports System.Net.Mail
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)
{
if (point.YValues[0] > 8)
{
point.Color = Color.Green;
}
else
{
point.Color = Color.FromArgb(112, 255, 200);
}
}
byte[] bytes = null;
using (MemoryStream memoryStream = new MemoryStream())
{
chart.SaveImage(memoryStream, ChartImageFormat.Png);
bytes = memoryStream.ToArray();
}
string image = "data:image/png;base64," + Convert.ToBase64String(bytes);
MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com");
mm.Subject = "Bar graph";
mm.Body = "<img src = '" + image + "' />";
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
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
If point.YValues(0) > 8 Then
point.Color = Color.Green
Else
point.Color = Color.FromArgb(112, 255, 200)
End If
Next
Dim bytes As Byte() = Nothing
Using memoryStream As MemoryStream = New MemoryStream()
chart.SaveImage(memoryStream, ChartImageFormat.Png)
bytes = memoryStream.ToArray()
End Using
Dim image As String = "data:image/png;base64," & Convert.ToBase64String(bytes)
Dim mm As MailMessage = New MailMessage("sender@gmail.com", "receiver@gmail.com")
mm.Subject = "Bar graph"
mm.Body = "<img src = '" & image & "' />"
mm.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "<password>"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
End Sub