I'm working with the ASP.NET Charting Library and I've got it generating a pie chart but I'm having a problem configuring it to generate the pie chart with semi-transparent slices. If you look at the image you'll see what I'm talking about. Of the 4 pie charts the top 2 and the bottom left chart have the pie slice transparency I'm talking about.
(source: scottgu.com)
What settings of the chart do I tweak to render the slices with a certain % of transparency?
Thanks!
Try assigning the color of the series to a color with alpha transparency, like so:
Chart1.Series(0).Color = Color.FromArgb(128, 255, 0, 0) //transparent red
Taken from this thread.
This the ultimate solution for both cases - one color per series or palette charts:
myChart.ApplyPaletteColors();
foreach (var series in myChart.Series)
{
foreach (var point in series.Points)
{
point.Color = Color.FromArgb(220, point.Color);
}
}
Try this:
Series["SeriesName"].Color = Color.FromArgb(180, Color.Blue);
Where 180 defines the "transparency level", which must be between 0 to 255.
You can use semi transparent palettes.
Refer to:
https://blogs.msdn.microsoft.com/alexgor/2009/10/06/setting-microsoft-chart-series-colors/
Solution for color per serie and even for transparent palette:
Chart1.Series(0).Color = Color.FromArgb(200, Chart1.Series(0).Color)
Related
I've got C# code to draw a pie chart using the conventional code for each slice:
gr.FillPie(brFill, rect, start_angle, sweep_angle);
gr.DrawPie(penOutline, rect, start_angle, sweep_angle);
This works fine: the trouble is it looks very boring. I'd like to get the effect as below; that is, keeping it 2-D but rounding off the edges.
I presume I'd need to use a path gradient brush somehow, but none of the examples I've looked at have helped.
Grateful for any help or tips.
Tony Reynolds
I posted this 11 days ago hoping that someone would do my job for me. I now realise I was wrong and am a reformed character.
I tried drawing shading and highlights using ControlPaint.Dark() & Light() but couldn't get it to look good. Finally I decided that as most pie charts have a limited range of colours I should set up a series of complete discs with colouring and shading and clip sectors out as required.
A few sample discs are below:
I then set up an equivalent of the FillPie method as below.
private static void FillImagePie(Graphics gr, Bitmap bm, Rectangle rect,
float startAngle, float sweepAngle)
// Display a pie slice of a background image.
{
// Create a clipping Region
GraphicsPath pathClip = new GraphicsPath();
pathClip.AddPie(rect, startAngle, sweepAngle);
Region regionClip = new Region(pathClip);
// Use region to clip image to the pie sector
gr.Clip = regionClip;
// Draw the image portion
gr.DrawImage(bm, rect);
// Clean up
gr.ResetClip();
regionClip.Dispose();
pathClip.Dispose();
}
This takes a disc bitmap, bm, sets up a pie-shaped clip region and draws the image.
I then needed a control routine that can be called from the Paint overload of the form.
In the special case of the pie chart only having one category, the whole disc is displayed. Otherwise each sector is taken in turn.
I found in practice that the outline looked ragged, so I first flood the sector with a black brush, then call FillImagePie, then draw a black outline.
A sample result is here:
I'm happy enough with this effect.
All comments and tweaks welcome, and if anyone else has a way to jazz up pie charts please share!
Tony Reynolds
I am working with charts. I have a simple chart with 2 columns, one green and one red:
I would like to have the green column to turn yellow when its value exceeds 60.
How could I achieve that?
I have made the chart by manually add 2 datapoint (one for each serie) so there is not code that is generating this chart so far.
You can set custom palette colors on the fly. Because ms charts don't have a great way to change the color of the column chart, this is a little work around you can use.
Color[] colorSet = new Color[]
{
Color.Yellow,
Color.Red
};
chart1.PaletteCustomColors = colorSet;
chart1.Palette = ChartColorPalette.None;
I have to generate dynamic graphs on the fly, save it as a PNG image format (Transparent background) to be embedded on to a PDF.
The problem is, the graph will not be displayed on a control in the page (without a chart control in the WebForm) and should be generated from an underlying information and should be embedded while creating the PDF.
The libraries for the PDFs and PDF generation is all in place.
I would like to know how to generate a dynamic graph (Bar graph, Pie Graph) 3D or Simple graph without a chart control in place and be able to save it as an image with transparent background (.PNG).
The reason being, the PDF template has numerous layer shading, hence the background around the graph image should be transparent, something similar to the below image.
I have tried generating a dynamic bar graph similar to the example in the below link but the Image doesn't look promising
https://2leggedspider.wordpress.com/2004/11/21/generating-a-dynamic-bar-graph-using-aspnet-and-c/
Any help regarding the functionality is deeply appreciated. Thank you in advance.
Update: Wednesday 16th March 2016
With TaW's answer and updates I have been able to draw the required graph and below is the update with answer to the same question I asked
Add references to System.Windows.Forms and System.Windows.Forms.DataVisualization
Add below namespaces
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
And below method would give you the Pie Chart with smooth edges as shown in the picture
void Create3DPieChart()
{
/* Utilize the Chart class available in System.Windows.Forms.DataVisualization.Charting */
Chart chart = new Chart();
/* Add a chart area to the chart */
ChartArea CA = chart.ChartAreas.Add("A1");
/* Add data series to the chart and specify the type of Series */
Series S1 = chart.Series.Add("S1");
S1.ChartType = SeriesChartType.Pie;
/* Assign points for the Series */
S1.Points.AddXY(1, 17);
S1.Points.AddXY(2, 27);
S1.Points.AddXY(3, 7);
S1.Points.AddXY(4, 49);
/* Set chart color and other settings as required */
chart.BackColor = Color.Transparent;
CA.BackColor = chart.BackColor;
CA.Area3DStyle.Enable3D = true;
S1.Points[2]["Exploded"] = "true";
/*Assign AntiAliasing to Graphics style for smooth edges*/
chart.AntiAliasing = AntiAliasingStyles.Graphics;
/* Set the image path and save the image as PNG format*/
string imageNameAndPath = string.Concat(Application.StartupPath.Remove(Application.StartupPath.IndexOf("\\bin\\Debug")),
"/TempImages/Image", DateTime.Now.ToString("ddMMyyyyhhmmss") + ".png");
chart.SaveImage(imageNameAndPath, ChartImageFormat.Png);
}
TaW. Please correct me if I'm wrong.
Unfortunately the chart won't draw any transparency directly, so we need to workaround..
If you want to create a PNG with transparent background here is what you can do:
chart1.BackColor = Color.Fuchsia;
chart1.ChartAreas[0].BackColor = chart1.BackColor;
Bitmap bmp = new Bitmap( chart1.ClientSize.Width, chart1.ClientSize.Height);
chart1.AntiAliasing = AntiAliasingStyles.Graphics;
chart1.DrawToBitmap(bmp, chart1.ClientRectangle);
bmp.MakeTransparent(chart1.BackColor);
bmp.Save(yourFileName, ImageFormat.Png);
First we set some areas in the chart to a unique color; if you use fuchsia in your chart (yuck!), pick some other color.
Then we create a Bitmap to hold the image.
Now we turn anti-aliasing off for the text; this help to avoid funny colors when we turn the background pixels to tranparent.
Now we tell the chart control to draw itself into our bitmap.
Finally we make all pixels transparent that have our special color.
Now we can save to disk..
You may want to look into setting the dpi resolution and the chart size to make the result look nice in your pdf!
Update:
If you want to create the image without acutally showing a Chart control, there is no real problem, either. In fact you can even create a Chart image in a console application, if you include all necessary namespaces etc..
All you need to do is create the Chart in code and set up all necessary properties.
Here is a minimal example:
Chart chart = new Chart();
ChartArea CA = chart.ChartAreas.Add("A1");
Series S1 = chart.Series.Add("S1");
S1.ChartType = SeriesChartType.Pie;
S1.Points.AddXY(1, 17);
S1.Points.AddXY(2, 27);
S1.Points.AddXY(3, 7);
S1.Points.AddXY(4, 49);
chart.BackColor = Color.Fuchsia;
CA.BackColor = chart.BackColor;
CA.Area3DStyle.Enable3D = true;
S1.Points[2]["Exploded"] = "true";
Bitmap bmp = new Bitmap(chart.Width, chart.Height);
chart.AntiAliasing = AntiAliasingStyles.None;
chart.DrawToBitmap(bmp, chart.ClientRectangle);
bmp.MakeTransparent(chart.BackColor);
bmp.Save(yourFileName, ImageFormat.Png);
I'm wondering if there is a way to differentiate multiple lines in a chart (using Windows Forms Chart) through means other than different colors. In Excel you can make small triangles or other shapes appear on different lines in a graph, and I was wondering if you could do something similar in C# for sets of 3 or more lines.
You can change the shape of the marker (http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.markerstyle(v=vs.110).aspx), for point-type charts:
chart1.Series["MySeries"].MarkerStyle = MarkerStyle.Square;
You can also set background colors and gradients / hatching (http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chart.backgradientstyle(v=vs.110).aspx) for bar-type charts:
chart1.Series["MySeries"].BackGradientStyle = GradientStyle.DiagonalLeft;
You can also specify line thickness and/or style for line-type charts:
chart1.Series["MySeries"].BorderWidth = 4;
chart1.Series["MySeries"].BorderDashStyle = ChartDashStyle.Dash;
How can I create a background for a chart that looks like this?
It's got different background colors at certain % levels that correspond to the status. All I see in ChartArea is BackColor, BackSecondaryColor, and BackGradientStyle which won't do this. This image was taken from an SSRS report and I am trying to recreate it using C# and the Microsoft charting library. I am pretty sure SSRS uses the same library so it should be possible - I just don't know how.
You can achieve this by using StripLine.
// Create the stripline for 'gold status'
StripLine sline = new StripLine();
sline.IntervalOffset = 0.6;
sline.StripWidth = 0.25;
sline.Text = "Gold Status";
sline.Interval = 0.0; // this ensures it will not repeat like normal strip lines
sline.BackColor = Color.Yellow;
myChart.AxisY.StripLines.Add(sline);
repeat the process for your silver and platinum strip lines.