|
// svgPie.cs
// Copyright by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a Creative Commons Attribution 2.0 Germany License.
// See http://creativecommons.org/licenses/by/2.0/de/
// -----
// 31.01.2006 created by Matthias Hertel
using System;
using System.Text;
namespace WebReports8 {
/// <summary>This class renders some svg code that displays a pie graph and can be embedded into html.</summary>
public abstract class svgPie {
/// <summary>Return a string containing svg tags that render as a pie chart.</summary>
/// <param name="data">The data values.</param>
/// <param name="legend">The labelsof the corresponding data elements.</param>
/// <returns>Xml text containing svg tags.</returns>
public static string RenderPie(double[] data, string[] legend) {
if (data != null) {
byte r = 0, g=0, b=0xFF;
byte shiftValue = 0x11;
string[] colors = new string[data.Length];
if (data.Length < 9)
shiftValue = 0x55;
else if (data.Length < 15)
shiftValue = 0x33;
for (int n = 0; n < data.Length; n++) {
colors[n] = String.Format("#{0:X2}{1:X2}{2:X2}", r, g, b);
// shift 0x11 value
if ((r != 0) & (b == 0)) {
r -= shiftValue;
g += shiftValue;
} else if (g != 0) {
g -= shiftValue;
b += shiftValue;
} else {
b -= shiftValue;
r += shiftValue;
} // if
} // for
return(RenderPie(data, legend, colors));
} // if
return(String.Empty);
}
/// <summary>Return a string containing svg tags that render as a pie chart.</summary>
/// <param name="data">The data values.</param>
/// <param name="legend">The labelsof the corresponding data elements.</param>
/// <param name="colors">The colors of the corresponding data elements.</param>
/// <returns>Xml text containing svg tags.</returns>
public static string RenderPie(double[] data, string[] legend, string[] colors) {
StringBuilder sb = new StringBuilder();
int mx = 200, my = 200;
int r = 100;
double sum, curSum;
int curX, curY;
int txtX, txtY;
string txt;
sb.AppendFormat("<svg:svg width='{0}' height='{1}'>\n", 2*mx, 2*my);
sum = 0;
for (int n = 0; n < data.Length; n++)
sum += data[n];
// draw the pie
curSum = 0;
curX = mx - r;
curY = my;
for (int n = 0; n < data.Length; n++) {
// draw the pie part
if (data[n] >= sum / 2)
sb.AppendFormat("<svg:path d='M {0},{1} L {2},{3} A {4} {4} 0 1 1 ",
mx, my, curX, curY, r );
else
sb.AppendFormat("<svg:path d='M {0},{1} L {2},{3} A {4} {4} 0 0 1 ",
mx, my, curX, curY, r );
curSum += data[n];
curX = mx - (int)(r*Math.Cos(2 * curSum * Math.PI / sum));
curY = my - (int)(r*Math.Sin(2 * curSum * Math.PI / sum));
sb.AppendFormat("{0},{1} Z' ",curX, curY);
sb.AppendFormat(" fill='{0}' stroke='black' stroke-width='1' />\n", colors[n]);
// draw the text
if ((legend != null) && (legend.Length > n) && (legend[n] != null) && (legend[n].Length > 0)) {
txt = legend[n];
} else {
txt = data[n].ToString();
} // if
txtX = mx - (int)(1.3 * r*Math.Cos(2 * (curSum - data[n]/2) * Math.PI / sum));
txtY = my - (int)(1.3 * r*Math.Sin(2 * (curSum - data[n]/2) * Math.PI / sum));
sb.AppendFormat("<svg:text x='{0}' y='{1}' text-anchor='middle' style='fill:black'>{2}</svg:text>\n",
txtX, txtY, txt);
} // for
sb.Append("</svg:svg>\n");
return(sb.ToString());
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.