Chart without Axis Lines - c#

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

Related

Change the border color in individual cells in Spire.PDF grid C#

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);
}

ImageMagick create circle count on top of image

Hey all I have the following Magick.net code:
List<string> lFiles = new List<string>();
bool isBlankImage = false;
lFiles.Add(#"C:\Users\David\Pictures\1.jpg");
lFiles.Add(#"C:\Users\David\Pictures\blank.png");
lFiles.Add(#"C:\Users\David\Pictures\blank.png");
lFiles.Add(#"C:\Users\David\Pictures\blank.png");
lFiles.Add(#"C:\Users\David\Pictures\blank.png");
IMagickImage roundImg = new MagickImage();
IMagickImage mask = new MagickImage();
IMagickImage shadow = new MagickImage();
IMagickImage result = new MagickImage();
foreach (string tempFBProfileImg in lFiles)
{
roundImg = new MagickImage(tempFBProfileImg);
if (Regex.IsMatch(tempFBProfileImg.ToLower(), #"\bblank.png\b"))
{
result.Extent(360, 100, Gravity.West);
images.Add(result);
isBlankImage = true;
break;
}
else
{
mask = new MagickImage("xc:black", 100, 100);
mask.Settings.FillColor = MagickColors.White;
mask.Draw(new DrawableCircle(50, 50, 50, 90));
mask.HasAlpha = false;
roundImg.Resize(100, 100);
roundImg.Composite(mask, CompositeOperator.CopyAlpha);
roundImg.Draw(new DrawableStrokeColor(MagickColors.Black),
new DrawableStrokeWidth(1),
new DrawableFillColor(MagickColors.None),
new DrawableCircle(50, 50, 50, 90));
shadow = new MagickImage("xc:none", 100, 100);
shadow.Settings.FillColor = MagickColors.Black;
shadow.Draw(new DrawableCircle(50, 50, 50, 90));
shadow.Blur(0, 5);
roundImg.Composite(shadow, CompositeOperator.DstOver);
images.Add(roundImg);
images.First().BackgroundColor = MagickColors.None;
result = images.SmushHorizontal(-35);
result.Resize(100, 100);
result.Write(#"C:\Users\David\Pictures\final2.png");
}
}
var imgText = new MagickImage(MagickColors.Blue, 0, 0);
imgText.Settings.FontPointsize = 24;
imgText.BackgroundColor = MagickColors.White;
imgText.Settings.FillColor = MagickColors.Black;
imgText.Settings.TextAntiAlias = true;
imgText.Settings.FontFamily = "Arial";
imgText.Read("label: 10+", 45, 25);
string caption = "label:10+";
//imgText = new MagickImage(caption, settings);
//imgText.Draw(new DrawableText(100, 20, "10+"));
imgText.Page = new MagickGeometry(300, 100, 0, 0);
result.Composite(imgText, CompositeOperator.Over);
result.Write(#"C:\Users\David\Pictures\final2.png");
And this comes out looking like this:
But what I am looking to want it to look like is this:
I'm having issues with just making a red circle with a black outline and shadow with the text XX+ inside it. Also, I've looked high and low to find out how i can position it where I need it but I am unable to find it. I thought that MagickGeometry would be it but it turns out that its not.
Update 1
Changing some code I now get this:
Changed code:
roundImg.Composite(mask, CompositeOperator.CopyAlpha);
roundImg.Draw(new DrawableStrokeColor(MagickColors.Black),
new DrawableStrokeWidth(1),
new DrawableFillColor(MagickColors.None),
new DrawableCircle(50, 50, 50, 90));
imgText.Composite(roundImg, CompositeOperator.Over);
imgText.Settings.FontPointsize = 24;
imgText.BackgroundColor = MagickColors.Red;
imgText.Settings.FillColor = MagickColors.White;
imgText.Settings.TextAntiAlias = true;
imgText.Settings.FontFamily = "Arial";
imgText.Read("label: 10+", 45, 25);
//string caption = "label:This is a very long caption line";
//imgText = new MagickImage(caption, settings);
//imgText.Draw(new DrawableText(100, 20, "10+"));
imgText.Page = new MagickGeometry(360, 100, 0, 0);
result.Composite(imgText, CompositeOperator.Over);
result.Write(#"C:\Users\David\Pictures\final2.png");
update 2
Ok I have the code correct for the round circle with number in it:
MagickImage circleText = new MagickImage(MagickColors.Transparent, 29, 29);
MagickImage circle = new MagickImage(MagickColors.Transparent, 29, 29);
circleText.Settings.BorderColor = MagickColors.Black;
circleText.Settings.FontPointsize = 16;
circleText.Settings.FontWeight = ImageMagick.FontWeight.Bold;
circleText.BackgroundColor = MagickColors.Transparent;
circleText.Settings.FillColor = MagickColors.White;
circleText.Settings.TextGravity = Gravity.Center;
circleText.Settings.TextAntiAlias = true;
circleText.Settings.FontFamily = "Arial";
circleText.Border(2);
circleText.Read("label: 10+", 29, 29);
circle.Draw(new DrawableStrokeColor(MagickColors.Black),
new DrawableStrokeWidth(1),
new DrawableFillColor(MagickColors.Red),
new DrawableCircle(14, 14, 10, 1));
circleText.Composite(circle, CompositeOperator.DstOver);
result.Composite(circleText, CompositeOperator.Over);
result.Write(#"C:\Users\David\Pictures\final2.png");
Which produces the image:
But now I can not seem to get the border for the text to show up and I'm still stuck on how to move the circle to its intended spot?
update 3
I finally got it to move over where I need it to be using this code:
MagickImage circleText = new MagickImage(MagickColors.Transparent, 29, 29);
MagickImage circle = new MagickImage(MagickColors.Transparent, 29, 29);
MagickImage _shadow = new MagickImage();
circleText.Settings.BorderColor = MagickColors.Black;
circleText.Settings.FontPointsize = 16;
circleText.Settings.FontWeight = ImageMagick.FontWeight.Bold;
circleText.BackgroundColor = MagickColors.Transparent;
circleText.Settings.FillColor = MagickColors.White;
circleText.Settings.TextGravity = Gravity.Center;
circleText.Settings.TextAntiAlias = true;
circleText.Settings.FontFamily = "Arial";
circleText.Border(2);
circleText.Read("label: 10+", 29, 29);
circle.Draw(new DrawableStrokeColor(MagickColors.Black),
new DrawableStrokeWidth(1),
new DrawableFillColor(MagickColors.Red),
new DrawableCircle(14, 14, 10, 1));
_shadow = new MagickImage("xc:none", 29, 29);
_shadow.Settings.FillColor = MagickColors.Black;
_shadow.Draw(new DrawableCircle(14, 14, 10, 1));
_shadow.Blur(0, 15);
circle.Composite(_shadow, CompositeOperator.DstOver);
circleText.Composite(circle, CompositeOperator.DstOver);
result.Composite(circleText, 68, 9, CompositeOperator.Over);
result.Write(#"C:\Users\David\Pictures\final2.png");
BUT when I try to add the shadow to the circle it comes out looking like this:
Not sure why the shadow is coming out being a square when its using the exact circle x,y,w,h as the original red circle...

How do i adjust the Y Axis scale for the line chart using c#

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);

Remove x and y axis from Column chart

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;

How to draw Chart based on DataTable from console application?

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);

Categories