I am getting this error
Invalid temp directory in chart handler configuration [c:\\TempImageFiles\\].
While running my code.
Intially I was getting
Time still progresses and people still have recommendations. I came across this issue during a migration, so I thought I'd add my two cents.
Why store it in the file system and not just keep it in memory?
<add key="ChartImageHandler" value="storage=memory;deleteAfterServicing=true;"/>
If the file system is an issue in Azure, then don't use it.
You can circumvent temporary image caching by using BinaryStreaming.
Google should do the rest.
It worked for me on Linux, where it threw an InvalidDirectory Exception for Linux paths.
(RenderType="BinaryStreaming")
<asp:Chart ID="ChartDIN277" runat="server" Width="500" Height="200" RenderType="BinaryStreaming">
<Series>
<asp:Series ChartArea="ChartArea1" ChartType="Pie"
Name="Area"
XValueMember="Label"
YValueMembers="Area"
IsVisibleInLegend="false">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
<Area3DStyle Enable3D="True" LightStyle="Realistic" />
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
FillChartFromDataBase();
}
public void FillChartFromDataBase()
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("Area", typeof(double));
table.Columns.Add("Label", typeof(string));
System.Data.DataRow row = table.NewRow();
row["Area"] = 791;
row["Label"] = "HNF 1";
table.Rows.Add(row);
row = table.NewRow();
row["Area"] = 978;
row["Label"] = "HNF 2";
table.Rows.Add(row);
row = table.NewRow();
row["Area"] = 1262;
row["Label"] = "HNF 3";
table.Rows.Add(row);
row = table.NewRow();
row["Area"] = 1650;
row["Label"] = "HNF 4";
table.Rows.Add(row);
row = table.NewRow();
row["Area"] = 2519;
row["Label"] = "HNF 5";
table.Rows.Add(row);
row = table.NewRow();
row["Area"] = 6071;
row["Label"] = "HNF 6";
table.Rows.Add(row);
// Set chart custom palette
ChartDIN277.Palette = System.Web.UI.DataVisualization.Charting.ChartColorPalette.None;
//ChartDIN277.PaletteCustomColors = New System.Drawing.Color() {System.Drawing.Color.Red, System.Drawing.Color.Blue}
ChartDIN277.PaletteCustomColors = COR.Design.ColorPalette.Generate(System.Drawing.Color.ForestGreen, table.Rows.Count - 1);
// http://student.csdn.net/space.php?uid=383581&do=blog&id=32768
//ChartDIN277.Palette = System.Web.UI.DataVisualization.Charting.ChartColorPalette.None;
//ChartDIN277.PaletteCustomColors = new Color[] { System.Drawing.Color.Red, System.Drawing.Color.Blue};
//// Hide all series empty data point by making them transparent
//Chart.Series[0].EmptyPointStyle.Color = Color.Transparent;
//// Set color for the whole series
//Chart.Series[0].Color = Color.Green;
//// Set color of a single data point
//Chart.Series[0].Points[5].Color = Color.Red;
this.ChartDIN277.DataSource = table;
this.ChartDIN277.DataBind();
}
or create an ashx handler, like this
Imports System.Web
Imports System.Web.Services
Public Class ChartCreator
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "image/jpeg"
Dim yValues As Double() = {10, 27.5, 7, 12, 45.5}
Dim xNames As String() = {"Mike", "John", "William", "George", "Alex"}
Dim mychart As System.Web.UI.DataVisualization.Charting.Chart
mychart = New System.Web.UI.DataVisualization.Charting.Chart
Dim mychartarea As New System.Web.UI.DataVisualization.Charting.ChartArea()
mychartarea.Name = "ChartArea1"
mychartarea.BackColor = Drawing.Color.Transparent
mychart.ChartAreas.Add(mychartarea)
Dim myseries As New System.Web.UI.DataVisualization.Charting.Series()
myseries.ChartArea = mychartarea.Name
myseries.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie
myseries.Name = "Series1"
mychart.Series.Add(myseries)
mychart.BackColor = Drawing.Color.Transparent
mychart.Series(0).Points.DataBindXY(xNames, yValues)
mychart.Series(0).Points.DataBindXY(xNames, yValues)
' http://msdn.microsoft.com/en-us/library/system.web.ui.datavisualization.charting.rendertype.aspx
' ImageTag, BinaryStreaming, ImageMap
' mychart.RenderType = System.Web.UI.DataVisualization.Charting.RenderType.BinaryStreaming
mychart.ImageType = System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png
'mychart.SaveImage(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "mychart.png"))
mychart.SaveImage(context.Response.OutputStream)
'getResizedImage(context.Request.PhysicalPath,Width,Height);
'context.Response.OutputStream
context.Response.End()
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Do the followings
1) Add or Edit the following key in web.config.
<add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;" /
2) Give writing privilages to IIS_User to this directory, for that do the followings:
a) Go to Windows Explorer.
b) Right click on c:\TempImageFiles Folder.
c) click Properties.
d) Select security tab , click Edit, click Add , Click Advanced, search for IIS_User.
e) Add this IIS_User , give write permission to this user.
f) Save and Close.
Now you have set the directory and given write permission to IIS to write temporary image files to this folder.
It should work now.
If changing the following line
<add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;" />
to
<add key="ChartImageHandler" value="storage=file;timeout=20;" />
not works, then change the value of attribute "storage=file;" to "storage=memory;" . It will surely work because now you are using memory, instead of file.