View Code
private bool DrawChartImage(ChartData chartData, string chartPath)
{
if (chartData == null || chartData.isHasData == false)
return false;
int axisXInterval = 1;
bool multiY = false;
#region 图表横轴interval
if (chartData.categories.Length > 0)
{
string category = chartData.categories[0];
if (Regex.IsMatch(category, "^\\d{4}$"))//年统计图表
axisXInterval = 1;
else
if (chartData.categories.Length.Equals(12) || chartData.categories.Length.Equals(15))
axisXInterval = 1;
else
if (chartData.categories.Length >= 28 && chartData.categories.Length <= 31 || chartData.categories.Length.Equals(34))
axisXInterval = 1;
else
axisXInterval = 24;
}
#endregion
string chartName = chartData.name;
multiY = chartData.units.Length > 1 && chartName.Contains("&");
string unit = string.Empty;
unit = chartData.units[0];
JavaScriptSerializer jserializer = new JavaScriptSerializer();
int seriesCount = 0;
Chart Chart = new Chart();
Chart.Width = 1000;
Chart.Height = 500;
Title title = new Title(chartName, Docking.Top, new System.Drawing.Font("Trebuchet MS", 12, System.Drawing.FontStyle.Regular), System.Drawing.Color.FromArgb(26, 59, 105));
Chart.Titles.Add(title);
Chart.ChartAreas.Add("ChartAreas 1");
Chart.ChartAreas[0].AxisX.Interval = axisXInterval;
Chart.ChartAreas[0].AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
Chart.ChartAreas[0].AxisY2.IntervalAutoMode = IntervalAutoMode.VariableCount;
//Chart.ChartAreas[0].AxisX.MajorTickMark.Interval = 2;//次要辅助线间隔
Chart.ChartAreas[0].AxisX.IntervalOffset = 0;
//Chart.ChartAreas[0].AxisX.MinorGrid.LineWidth = 0;
Chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
Chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 1;
Chart.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(209, 208, 206);
Chart.ChartAreas[0].AxisY2.MajorGrid.LineColor = System.Drawing.Color.FromArgb(209, 208, 206);
Chart.ChartAreas[0].AxisY.Title = string.Format("[{0}]", unit);
Chart.ChartAreas[0].AxisX.LabelStyle.Angle = -90;//横坐标字符倾斜角度
#region 图例配置
Legend legend = new Legend();
legend.Alignment = StringAlignment.Center;
legend.Docking = Docking.Bottom;
legend.Name = string.Format("Legends {0}", 0);
Chart.Legends.Add(legend);
#endregion
Chart.SuppressExceptions = true;
while (seriesCount++ < chartData.series.Length)
{
#region 设置图表类型
SeriesChartType seriesChartType = SeriesChartType.Column;
string chartType = chartData.series[seriesCount - 1].type;
if (chartType.ToLower().Equals("area"))
seriesChartType = SeriesChartType.Area;
if (chartType.ToLower().Equals("line"))
seriesChartType = SeriesChartType.Line;
if (chartType.ToLower().Equals("column"))
seriesChartType = SeriesChartType.Column;
#endregion
#region 设置图表颜色
string color = chartData.series[seriesCount - 1].color;
string r = color.Substring(1, 2);
string g = color.Substring(3, 2);
string b = color.Substring(5, 2);
#endregion
IList<KeyValuePair<string, float?>> outputData = new List<KeyValuePair<string, float?>>();
for (int i = 0; i < chartData.categories.Length; i++)
outputData.Add(new KeyValuePair<string, float?>(chartData.categories[i], chartData.series[seriesCount - 1].data[i]));
Chart.Series.Add(string.Format("Series {0}", seriesCount - 1));
Chart.Series[seriesCount - 1].Points.DataBind(outputData, "key", "value", "");
Chart.Series[seriesCount - 1].Color = System.Drawing.Color.FromArgb(Convert.ToInt32(r, 16), Convert.ToInt32(g, 16), Convert.ToInt32(b, 16));
Chart.Series[seriesCount - 1].XValueType = ChartValueType.String;
Chart.Series[seriesCount - 1].ChartType = seriesChartType;
#region 图例
//图例文字
Chart.Series[seriesCount - 1].LegendText = chartData.series[seriesCount - 1].name;
#endregion
if (multiY)
{
Chart.Series[seriesCount - 1].YAxisType = seriesCount % 2 == 1 ? AxisType.Primary : AxisType.Secondary;
}
}
Chart.BorderSkin.SkinStyle = BorderSkinStyle.None;
Chart.SaveImage(chartPath);
return true;
}
{
if (chartData == null || chartData.isHasData == false)
return false;
int axisXInterval = 1;
bool multiY = false;
#region 图表横轴interval
if (chartData.categories.Length > 0)
{
string category = chartData.categories[0];
if (Regex.IsMatch(category, "^\\d{4}$"))//年统计图表
axisXInterval = 1;
else
if (chartData.categories.Length.Equals(12) || chartData.categories.Length.Equals(15))
axisXInterval = 1;
else
if (chartData.categories.Length >= 28 && chartData.categories.Length <= 31 || chartData.categories.Length.Equals(34))
axisXInterval = 1;
else
axisXInterval = 24;
}
#endregion
string chartName = chartData.name;
multiY = chartData.units.Length > 1 && chartName.Contains("&");
string unit = string.Empty;
unit = chartData.units[0];
JavaScriptSerializer jserializer = new JavaScriptSerializer();
int seriesCount = 0;
Chart Chart = new Chart();
Chart.Width = 1000;
Chart.Height = 500;
Title title = new Title(chartName, Docking.Top, new System.Drawing.Font("Trebuchet MS", 12, System.Drawing.FontStyle.Regular), System.Drawing.Color.FromArgb(26, 59, 105));
Chart.Titles.Add(title);
Chart.ChartAreas.Add("ChartAreas 1");
Chart.ChartAreas[0].AxisX.Interval = axisXInterval;
Chart.ChartAreas[0].AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
Chart.ChartAreas[0].AxisY2.IntervalAutoMode = IntervalAutoMode.VariableCount;
//Chart.ChartAreas[0].AxisX.MajorTickMark.Interval = 2;//次要辅助线间隔
Chart.ChartAreas[0].AxisX.IntervalOffset = 0;
//Chart.ChartAreas[0].AxisX.MinorGrid.LineWidth = 0;
Chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
Chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 1;
Chart.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(209, 208, 206);
Chart.ChartAreas[0].AxisY2.MajorGrid.LineColor = System.Drawing.Color.FromArgb(209, 208, 206);
Chart.ChartAreas[0].AxisY.Title = string.Format("[{0}]", unit);
Chart.ChartAreas[0].AxisX.LabelStyle.Angle = -90;//横坐标字符倾斜角度
#region 图例配置
Legend legend = new Legend();
legend.Alignment = StringAlignment.Center;
legend.Docking = Docking.Bottom;
legend.Name = string.Format("Legends {0}", 0);
Chart.Legends.Add(legend);
#endregion
Chart.SuppressExceptions = true;
while (seriesCount++ < chartData.series.Length)
{
#region 设置图表类型
SeriesChartType seriesChartType = SeriesChartType.Column;
string chartType = chartData.series[seriesCount - 1].type;
if (chartType.ToLower().Equals("area"))
seriesChartType = SeriesChartType.Area;
if (chartType.ToLower().Equals("line"))
seriesChartType = SeriesChartType.Line;
if (chartType.ToLower().Equals("column"))
seriesChartType = SeriesChartType.Column;
#endregion
#region 设置图表颜色
string color = chartData.series[seriesCount - 1].color;
string r = color.Substring(1, 2);
string g = color.Substring(3, 2);
string b = color.Substring(5, 2);
#endregion
IList<KeyValuePair<string, float?>> outputData = new List<KeyValuePair<string, float?>>();
for (int i = 0; i < chartData.categories.Length; i++)
outputData.Add(new KeyValuePair<string, float?>(chartData.categories[i], chartData.series[seriesCount - 1].data[i]));
Chart.Series.Add(string.Format("Series {0}", seriesCount - 1));
Chart.Series[seriesCount - 1].Points.DataBind(outputData, "key", "value", "");
Chart.Series[seriesCount - 1].Color = System.Drawing.Color.FromArgb(Convert.ToInt32(r, 16), Convert.ToInt32(g, 16), Convert.ToInt32(b, 16));
Chart.Series[seriesCount - 1].XValueType = ChartValueType.String;
Chart.Series[seriesCount - 1].ChartType = seriesChartType;
#region 图例
//图例文字
Chart.Series[seriesCount - 1].LegendText = chartData.series[seriesCount - 1].name;
#endregion
if (multiY)
{
Chart.Series[seriesCount - 1].YAxisType = seriesCount % 2 == 1 ? AxisType.Primary : AxisType.Secondary;
}
}
Chart.BorderSkin.SkinStyle = BorderSkinStyle.None;
Chart.SaveImage(chartPath);
return true;
}