using
System.Web.UI.DataVisualization.Charting;
private
Chart chart;
protected
override
void
OnInit(EventArgs e)
{
base
.OnInit(e);
chart =
new
Chart();
chart.Visible =
false
;
chart.RenderType = RenderType.ImageTag;
chart.ImageType = ChartImageType.Png;
}
CreateChildControls()
EnsureChildControls();
Controls.Add(chart);
OnPreRender(EventArgs e)
.OnPreRender(e);
PopulateChart(SPContext.Current.Web.Lists[
"crazythingslist"
]);
PopulateChart(SPList dataList)
try
chart.Width = 315;
chart.Height = 150;
chart.AntiAliasing = AntiAliasingStyles.All;
chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
Series s = GetCrazyThingsData(dataList);
ChartArea ca = ChartArea();
chart.ChartAreas.Add(ca);
chart.Series.Add(s);
true
catch
static
ChartArea ChartArea()
ChartArea chartArea =
ChartArea();
chartArea.BackColor = Color.Gray;
chartArea.BackSecondaryColor = Color.DarkGray;
chartArea.BackGradientStyle = GradientStyle.TopBottom;
chartArea.AxisY.Title =
"Craziness Count"
chartArea.AxisX.Title =
"Crazy things, Recent Years"
chartArea.AxisX.Interval = 1;
return
chartArea;
Series GetCrazyThingsData(SPList dataList)
var series =
Series();
series.Color = Color.ForestGreen;
series.BackSecondaryColor = Color.GreenYellow;
series.BorderColor = Color.Firebrick;
series.BackGradientStyle = GradientStyle.TopBottom;
var query =
SPQuery();
query.Query =
"<OrderBy><FieldRef Name=\"Year\"/></OrderBy>"
query.RowLimit = 50;
var items = dataList.GetItems(query);
foreach
(SPItem i
in
items)
var yearCountObj = Int32.Parse(i[
"NumberOfCrazyThingIveDone"
].ToString());
var yearObj = Int32.Parse(i[
"Year"
var p =
DataPoint
XValue = yearObj,
YValues =
[] {Convert.ToDouble(yearCountObj)}
};
series.Points.Add(p);
series;
<
handlers
>
add
name
=
"ChartImageHandler"
verb
"*"
path
"ChartImg.axd"
type
"System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
/>
</
appSettings
key
value
"storage=file;timeout=20;dir=c:\Temp\;"
<!-- Other Assemblies, Registers, etc -->
<%@ Import Namespace="Microsoft.SharePoint" %>
div
charts:Chart
ID
"chart"
runat
"server"
RenderType
"ImageTag"
ImageType
"Png"
></
System;
System.ComponentModel;
System.Drawing;
System.Web.UI.WebControls.WebParts;
Microsoft.SharePoint;
namespace
ChartWebPart.CoolChartVisualWebpart
[ToolboxItemAttribute(
)]
public
partial
class
CoolChartVisualWebpart : WebPart
CoolChartVisualWebpart()
InitializeControl();
Page_Load(
object
sender, EventArgs e)
"mylistwithdatainit"
[] { Convert.ToDouble(yearCountObj) }
Ed Price - MSFT edited Revision 13. Comment: Making the technology more obvious in the title.
Ed Price - MSFT edited Revision 11. Comment: HTML bug
Ed Price - MSFT edited Revision 9. Comment: Going to try just removing the period from the title
Ed Price - MSFT edited Revision 8. Comment: Reverting. I ruined the great code coloring. =^(
Ed Price - MSFT edited Revision 6. Comment: Removed period from title per guidelines. Great article!
Matthew Yarlett edited Revision 3. Comment: Fixed code formatting
Matthew Yarlett edited Revision 2. Comment: Minor changes to wording
Matthew Yarlett edited Revision 1. Comment: Added an example for adding a chart to a visual webpart
Matthew Yarlett edited Original. Comment: Minor change to gramma