How to use System.Windows.Forms.DataVisualization.Charting.Chart inside console application to draw an graph and save it to file?
//populate dataset with some demo data..
DataSet dataSet = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Counter", typeof(int));
DataRow r1 = dt.NewRow();
r1[0] = "Demo";
r1[1] = 8;
dt.Rows.Add(r1);
DataRow r2 = dt.NewRow();
r2[0] = "Second";
r2[1] = 15;
dt.Rows.Add(r2);
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.Color = Color.FromArgb(112, 255, 200);
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 = "Name";
serie1.YValueMembers = "Counter";
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();
//save result...
chart.SaveImage(#"c:\myChart.png", ChartImageFormat.Png);
Add this declaration on top of your class:
using System.Windows.Forms.DataVisualization.Charting;
Plot sin(x) between 0 and 2pi.
You have to add "System.Windows.Forms.DataVisualization.dll" and "System.Drawing.dll" to your project.
Good Luck :)
//
// chart
//
System.Windows.Forms.DataVisualization.Charting.Chart chart = new System.Windows.Forms.DataVisualization.Charting.Chart();
chart.Size = new System.Drawing.Size(640, 320);
chart.ChartAreas.Add("ChartArea1");
chart.Legends.Add("legend1");
// Plot {sin(x), 0, 2pi}
chart.Series.Add("sin");
chart.Series["sin"].LegendText = "Sin(x)";
chart.Series["sin"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
for (double x = 0; x < 2 * Math.PI; x += 0.01)
{
chart.Series["sin"].Points.AddXY(x, Math.Sin(x));
}
// Save sin_0_2pi.png image file
chart.SaveImage("sin_0_2pi.png", System.Drawing.Imaging.ImageFormat.Png);
Related
I am using Spire.PDF library to create a PDF in server side and return it to mobile side. Everything is working great. But I wanna access to each cell's border in the grid. I wanna change the color of the border.
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Table/How-to-Change-the-Color-of-Grid-Border-in-PDF-in-C.html
Current code..
public HttpResponseMessage GET_MOTOR_RENWAL_PDF()
{
AgentDAL agntDAL = new AgentDAL();
//Create a pdf document.
PdfDocument doc = new PdfDocument();
PdfSection sec = doc.Sections.Add();
sec.PageSettings.Width = PdfPageSize.A4.Width;
sec.PageSettings.Height = PdfPageSize.A4.Height;
sec.PageSettings.Margins = new PdfMargins(20f, 5f, 20f, 5f);
PdfPageBase page = sec.Pages.Add();
//title
PdfBrush brush1 = PdfBrushes.Black;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Bold));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString("Monthly", font1, brush1, page.Canvas.ClientSize.Width / 2, 60, format1);
page.Canvas.DrawString("Report generated", font1, brush1, page.Canvas.ClientSize.Width / 2, 76, format1);
//grid
PdfGrid grid = new PdfGrid();
grid.Columns.Add(5);
float gridWidth = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
for (int j = 0; j < grid.Columns.Count; j++)
{
grid.Columns[j].Width = gridWidth * 0.20f;
}
PdfGridRow row0 = grid.Rows.Add();
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();
row0.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
row1.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
row2.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
row3.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
row0.Cells[2].ColumnSpan = 3;
row1.Cells[2].ColumnSpan = 3;
row2.Cells[2].ColumnSpan = 3;
row3.Cells[2].ColumnSpan = 3;
row0.Cells[2].RowSpan = 4;
row0.Cells[0].Value = "Policy";
row0.Cells[1].Value = "VM5115001510000009";
row1.Cells[0].Value = "Vehicle";
row1.Cells[1].Value = "BBP";
row2.Cells[0].Value = "Premium";
row2.Cells[1].Value = "7,366.10";
row3.Cells[0].Value = "Date";
row3.Cells[1].Value = "03-Jan-2020";
float gridHeight = 20.0f;
for (int i = 0; i < grid.Rows.Count; i++)
{
grid.Rows[i].Height = gridHeight;
}
grid.Draw(page, new PointF(0, 120));
MemoryStream stream = new MemoryStream();
doc.SaveToStream(stream);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(stream.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
doc.Close();
return result;
}
What I get from this...
What I want...
I tried to do this, but it didn't work.
row0.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
row1.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
row2.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
row3.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
Try the below code:
float gridHeight = 20.0f;
for (int i = 0; i < grid.Rows.Count; i++)
{
grid.Rows[i].Height = gridHeight;
grid.Rows[i].Cells[0].Style.Borders.Right = new PdfPen(PdfBrushes.DarkRed);
grid.Rows[i].Cells[1].Style.Borders.Left = new PdfPen(PdfBrushes.DarkRed);
}
I am using System.Web.UI.DataVisualization.Charting to generate line chart which I am populating at run-time. Y axis of the graph is a percent value that currently scales from -150% to +150%. I want to adjust the scale so that Y Axis label will always scale from -100% to +100%. Any help is appreciated. Thank you.
This is the sample code i have for the chart:
Chart c = new Chart();
c.AntiAliasing = AntiAliasingStyles.All;
c.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
c.Width = 800;
c.Height = 450;
ChartArea ca = new ChartArea();
ca.Position.X = 0;
ca.Position.Y = 10;
ca.Position.Width = 100;
ca.Position.Height = 80;
ca.BackColor = Color.FromArgb(248, 248, 248);
ca.BackSecondaryColor = Color.FromArgb(255, 255, 255);
ca.BackGradientStyle = GradientStyle.TopBottom;
ca.AxisY.IsMarksNextToAxis = true;
ca.AxisY.LineColor = Color.FromArgb(157, 157, 157);
ca.AxisY.MajorTickMark.Enabled = true;
ca.AxisY.MajorTickMark.LineColor = Color.FromArgb(157, 157, 157);
ca.AxisY.LabelStyle.ForeColor = Color.FromArgb(89, 89, 89);
ca.AxisY.LabelStyle.Format = "{0:p}";
ca.AxisY.LabelStyle.IsEndLabelVisible = true;
ca.AxisY.Interval = 1;
ca.AxisY.LabelStyle.Font = new Font("Calibri", 4, FontStyle.Regular);
ca.AxisY.MajorGrid.LineColor = Color.FromArgb(234, 234, 234);
ca.AxisX.IsMarksNextToAxis = true;
ca.AxisX.LineColor = Color.FromArgb(157, 157, 157);
ca.AxisX.MajorTickMark.Enabled = true;
ca.AxisX.MajorTickMark.LineColor = Color.FromArgb(157, 157, 157);
ca.AxisX.LabelStyle.IsEndLabelVisible = true;
ca.AxisX.LabelStyle.ForeColor = Color.FromArgb(89, 89, 89);
ca.AxisX.MajorGrid.LineWidth = 0;
ca.AxisX.IsMarginVisible = false;
ca.AxisX.Interval = 0;
c.ChartAreas.Add(ca);
List<RawData> rawData = (List<RawData>)rawDataDao.GetDataByDateRange(start.Value, end.Value);
if(rawData.Count > 0)
{
Series s = new Series();
s.Font = new Font("Lucida Sans Unicode", 6f);
s.Color = System.Drawing.ColorTranslator.FromHtml(GetCombinedColorByIndex(possibleSensors.IndexOf(sensor)));
s.BorderColor = Color.FromArgb(159, 27, 13);
s.BackSecondaryColor = Color.FromArgb(173, 32, 11);
s.BackGradientStyle = GradientStyle.LeftRight;
s.ChartType = SeriesChartType.Line;
s.XValueType = ChartValueType.DateTime;
s.Name = sensor.SensorName;
foreach (RawData d in workingRawData)
{
System.DateTime x = DateTime.Parse(d.Dt.Value.ToString("", CultureInfo.CreateSpecificCulture("en-US")));
DataPoint p = new DataPoint(x.ToOADate(), Convert.ToDouble(d.Reading));
p.AxisLabel = d.Dt.Value.ToString();
s.Points.Add(p);
}
c.Series.Add(s);
}
If the axis is zoomable, set the scaleview to the range you need
ca.AxisY.ScaleView.Zoom(-100, 100);
I am using System.Web.UI.DataVisualization.Charting library to generate column chart in c#. But I want to remove x and y axis. want only the bars. I remover grid lines using following code
chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
How can I remove axis lines and its values ?
var chart = new Chart
{
Width = 80,
Height = 125,
RenderType = RenderType.ImageTag,
AntiAliasing = AntiAliasingStyles.All,
TextAntiAliasingQuality = TextAntiAliasingQuality.High
};
chart.Titles.Add("50");
chart.Titles.Add("Strongly Disagree");
chart.Titles[0].Font = new Font("Arial", 8f,FontStyle.Bold);
chart.Titles[0].ForeColor = Color.DarkGray;
chart.Titles[1].Font = new Font("Arial", 7f);
chart.Titles[1].ForeColor = Color.DarkGray;
chart.Titles[0].Position.X = 50;
chart.Titles[0].Position.Y = 50;
chart.Titles[1].Position.X = 50;
chart.Titles[1].Position.Y = 90;
chart.ChartAreas.Add("");
chart.ChartAreas[0].AxisX.TitleFont = new Font("Arial", 12f);
chart.ChartAreas[0].AxisY.TitleFont = new Font("Arial", 12f);
chart.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Arial", 10f);
chart.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
chart.ChartAreas[0].BackColor = Color.White;
chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
chart.Series.Add("");
chart.Series[0].ChartType = SeriesChartType.Column;
int i = 0;
foreach (var q in myOrderList)
{
if (i == 0)
{
chart.Series[0].Points.AddXY("", Convert.ToDouble(q.NumberOfOrders));
chart.Series[0].Points[0].Color = Color.Orchid;
}
else
{
chart.Series[0].Points.AddXY("", Convert.ToDouble(q.NumberOfOrders));
chart.Series[0].Points[1].Color = Color.DarkViolet;
}
i++;
}
using (var chartimage = new MemoryStream())
{
chart.SaveImage(chartimage, ChartImageFormat.Png);
return chartimage.GetBuffer();
}
I found the solution..
chart.ChartAreas[0].AxisX.MinorGrid.Enabled = false;
chart.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;
chart.ChartAreas[0].AxisX.MinorTickMark.Enabled = false;
chart.ChartAreas[0].AxisX.Interval = 0;
chart.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
chart.ChartAreas[0].AxisY.LabelStyle.Enabled = false;
chart.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
chart.ChartAreas[0].AxisY.MinorTickMark.Enabled = false;
chart.ChartAreas[0].AxisX.LineWidth = 0;
chart.ChartAreas[0].AxisY.LineWidth = 0;
I'm trying to create chart programmatically. Here is code
static void Main(string[] args)
{
var xvals = new[]
{
new DateTime(2012, 4, 4),
new DateTime(2012, 4, 5),
new DateTime(2012, 4, 6),
new DateTime(2012, 4, 7)
};
var yvals = new[] { 1, 3, 7, 12 };
// create the chart
var chart = new Chart();
chart.Width = 600;
chart.Height = 350;
var chartArea = new ChartArea();
chartArea.AxisX.IsMarginVisible = false;
chartArea.AxisY.IsMarginVisible = false;
chartArea.AxisX.LabelStyle.Format = "dd";
chartArea.AxisX.MajorGrid.LineWidth = 0;
chartArea.AxisY.MajorGrid.LineWidth = 0;
chartArea.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chartArea.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chart.ChartAreas.Add(chartArea);
var series = new Series();
series.Name = "Series1";
series.ChartType = SeriesChartType.Line;
chart.Series.Add(series);
chart.Series["Series1"].Points.DataBindXY(xvals, yvals);
chart.SaveImage("chart.png", ChartImageFormat.Png);
}
I'm getting following output,
How can I remove Axis Lines both X and Y? So that my output will only the blue line.
chartArea.AxisY.LineWidth = 0;
chartArea.AxisX.LineWidth = 0;
chartArea.AxisX.LabelStyle.Enabled = false;
chartArea.AxisY.LabelStyle.Enabled = false;
chartArea.AxisX.MajorTickMark.Enabled = false;
chartArea.AxisY.MajorTickMark.Enabled = false;
Fixed my problem
I wonder if there is any way to show this serie as follows, the chartype is column.
I'm aware that I need an (X,Y) pair to show the datapoint in the graph, but the thing is that I need to show a Y value in a range of x, to clarify this, let's put an example:
The X axis shows the hours and Y axis production values, in this case the production started at 7:30, so from 7:30 to 8 the production was let's say 20 units; from 8-9, 60 units; 9-10, 45 units and from 10-10:35 18 units.
Can I accomplish that with MSChart, or perhaps any other one that you guys suggest me? This is a winform app.
Thanks
You should take a look at the MS Chart site. The Downloads tab has WinForms examples that you can download as a WinForms project, run in Visual Studio, and view the code for the chart you want. The Chart controls are included with .Net 4, but can be downloaded and added as a reference for .Net 3.5.
What you want is a Column chart. You set the values as a series on the chart, and then you can pick the width of the column to show the range of x-values you want.
The basic idea is you create a Chart, ChartArea, and Series, and optionally a Title and Legend.
You add one or more Series objects to the ChartArea, then add the ChartArea, Title, and Legend to the Chart.
You add DataPoints individually to the Series, as in the example below, or you set the Chart.DataSource to something like a DataTable and use DataBinding.
Here's a snippet from the Column Chart example:
System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint1 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36890, 32);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint2 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36891, 56);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint3 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36892, 35);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint4 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36893, 12);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint5 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36894, 35);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint6 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36895, 6);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint7 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36896, 23);
System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Title title1 = new System.Windows.Forms.DataVisualization.Charting.Title();
chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
chart1.BorderlineWidth = 2;
chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;
chartArea1.Area3DStyle.Inclination = 15;
chartArea1.Area3DStyle.IsClustered = true;
chartArea1.Area3DStyle.IsRightAngleAxes = false;
chartArea1.Area3DStyle.Perspective = 10;
chartArea1.Area3DStyle.Rotation = 10;
chartArea1.Area3DStyle.WallWidth = 0;
chartArea1.AxisX.LabelAutoFitMaxFontSize = 8;
chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
chartArea1.AxisX.LabelStyle.Format = "MM-dd";
chartArea1.AxisX.LabelStyle.IsEndLabelVisible = false;
chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisY.LabelAutoFitMaxFontSize = 8;
chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
chartArea1.AxisY.LabelStyle.Format = "C0";
chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.BackColor = System.Drawing.Color.OldLace;
chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
chartArea1.BackSecondaryColor = System.Drawing.Color.White;
chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.Name = "Default";
chartArea1.ShadowColor = System.Drawing.Color.Transparent;
chart1.ChartAreas.Add(chartArea1);
legend1.BackColor = System.Drawing.Color.Transparent;
legend1.Enabled = false;
legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
legend1.IsTextAutoFit = false;
legend1.Name = "Default";
chart1.Legends.Add(legend1);
chart1.Location = new System.Drawing.Point(16, 64);
chart1.Name = "chart1";
series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
series1.ChartArea = "Default";
series1.Legend = "Default";
series1.Name = "Series1";
series1.Points.Add(dataPoint1);
series1.Points.Add(dataPoint2);
series1.Points.Add(dataPoint3);
series1.Points.Add(dataPoint4);
series1.Points.Add(dataPoint5);
series1.Points.Add(dataPoint6);
series1.Points.Add(dataPoint7);
series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
chart1.Series.Add(series1);
chart1.Size = new System.Drawing.Size(412, 296);
chart1.TabIndex = 0;
title1.Font = new System.Drawing.Font("Trebuchet MS", 14.25F, System.Drawing.FontStyle.Bold);
title1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
title1.Name = "Title1";
title1.ShadowColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
title1.ShadowOffset = 3;
title1.Text = "Column Chart";
chart1.Titles.Add(title1);