Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use system.web.ui.datavisualization.charting.chart to make a chart? [closed]

Tags:

c#

charts

Does anyone have a good link for instructions on how to make a basic chart with Microsoft's built-in chart control?

I'd like to make a stacked bar chart, if I could. But, failing that, a regular bar chart would suffice. All the data for the chart is the result of a single SQL call (one result set, 1 label column and 3 data columns, if that makes any difference.)

My google-fu is failing me. Thanks in advance.

like image 985
BoltBait Avatar asked Mar 15 '11 00:03

BoltBait


1 Answers

Something that the article kesun left out was generating the chart in code:

Here's a quick example that covers the majority of the options.

Chart c = new Chart();
c.AntiAliasing = AntiAliasingStyles.All;
c.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
c.Width = 640; //SET HEIGHT
c.Height = 480; //SET WIDTH

ChartArea ca = new ChartArea();
ca.BackColor = Color.FromArgb(248, 248, 248);
ca.BackSecondaryColor = Color.FromArgb(255, 255, 255);
ca.BackGradientStyle = GradientStyle.TopBottom;

ca.AxisY.IsMarksNextToAxis = true;
ca.AxisY.Title = "Gigabytes Used";
ca.AxisY.LineColor = Color.FromArgb(157, 157, 157);
ca.AxisY.MajorTickMark.Enabled = true;
ca.AxisY.MinorTickMark.Enabled = true;
ca.AxisY.MajorTickMark.LineColor = Color.FromArgb(157, 157, 157);
ca.AxisY.MinorTickMark.LineColor = Color.FromArgb(200, 200, 200);
ca.AxisY.LabelStyle.ForeColor = Color.FromArgb(89, 89, 89);
ca.AxisY.LabelStyle.Format = "{0:0.0}";
ca.AxisY.LabelStyle.IsEndLabelVisible = false;
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.LabelStyle.Enabled = false;
ca.AxisX.LineColor = Color.FromArgb(157, 157, 157);
ca.AxisX.MajorGrid.LineWidth = 0;
ca.AxisX.MajorTickMark.Enabled = true;
ca.AxisX.MinorTickMark.Enabled = true;
ca.AxisX.MajorTickMark.LineColor = Color.FromArgb(157, 157, 157);
ca.AxisX.MinorTickMark.LineColor = Color.FromArgb(200, 200, 200);

c.ChartAreas.Add(ca);

Series s = new Series();
s.Font = new Font("Lucida Sans Unicode", 6f);
s.Color = Color.FromArgb(215, 47, 6);
s.BorderColor = Color.FromArgb(159, 27, 13);
s.BackSecondaryColor = Color.FromArgb(173, 32, 11);
s.BackGradientStyle = GradientStyle.LeftRight;

int i = 0;
foreach (DataRow dr in sourceData.Rows)
{
    DataPoint p = new DataPoint();
    p.XValue = i;
    p.YValues = new Double[] { Convert.ToDouble(dr[0]) };
    s.Points.Add(p);
    i++;
}

c.Series.Add(s);

c.SaveImage(Server.MapPath("~/output.png"), ChartImageFormat.Png);

This outputs to a file, but you could write this in an HttpHandler and write directly to the response stream.

like image 87
lukiffer Avatar answered Oct 18 '22 21:10

lukiffer