protected void Page_Load(object sender, EventArgs e) { //创建图表控件 ChartSpace myspace = new ChartSpace(); //添加一个图表对象 ChChart mychart = myspace.Charts.Add(0); //设置图表类型为柱形 mychart.Type = ChartChartTypeEnum.chChartTypePie; //设置图表的相关属性 mychart.HasLegend = true; //添加图列 mychart.HasTitle = true; //添加主题 mychart.Title.Caption = "员工信息图表"; //设置主题内容 //连接数据库 String str = ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString(); SqlConnection con = new SqlConnection(str); con.Open(); String sel = "select jiguan,count(jiguan) as number from xx group by jiguan"; SqlDataAdapter adsa = new SqlDataAdapter(sel, con); DataSet adds = new DataSet(); adsa.Fill(adds); if (adds.Tables[0].Rows.Count > 0) { string strDataName = ""; string strData = ""; //添加图表块 mychart.SeriesCollection.Add(0); //添加图表数据 for (int j = 0; j < adds.Tables[0].Rows.Count; j++) { if (j == adds.Tables[0].Rows.Count - 1) { strDataName += adds.Tables[0].Rows[j][0].ToString(); } else { strDataName += adds.Tables[0].Rows[j][0].ToString() + "\t"; } strData += adds.Tables[0].Rows[j][1].ToString() + "\t"; } //设置图表的属性 mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strDataName); mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strData); //设置百分比 ChDataLabels labels = mychart.SeriesCollection[0].DataLabelsCollection.Add(); labels.HasPercentage = true; } con.Close(); myspace.ExportPicture(Server.MapPath(".") + @"\temp.jpg", "jpg", 600, 450); }
private void MakeLineChart(DataTable mDataTable)//string[] DataName,string[] Data,string[] Data1, string[] Data2) { string strValue1 = ""; string strValue = ""; string strValue2 = ""; string strCateory = ""; //循环取得数据并格式化为OWC10需要的格式,(加'\t') foreach (DataRow mRow in mDataTable.Rows) { strCateory += mRow[0].ToString() + '\t'; strValue += mRow[1].ToString() + '\t'; strValue1 += mRow[2].ToString() + '\t'; } ChartSpace mySpace = new ChartSpace(); ChChart myChart = mySpace.Charts.Add(0); myChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered; myChart.HasLegend = true; myChart.HasTitle = true; if (cbDate.Checked == true) { myChart.Title.Caption = dtpStart.Value.ToString("yyyy年MM月dd日--") + dtpEnd.Value.ToString("yyyy年MM月dd日") + cmbCompareType.Text + "交易曲线图"; } else { myChart.Title.Caption = "交易曲线图"; } myChart.Axes[0].HasTitle = true; myChart.Axes[0].Title.Caption = mDataTable.Columns[0].Caption; myChart.Axes[1].HasTitle = true; myChart.Axes[1].Title.Caption = "总额"; myChart.SeriesCollection.Add(0); //myChart. myChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, (int)ChartSpecialDataSourcesEnum.chDataLiteral, mDataTable.Columns[1].Caption); myChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strCateory); ChDataLabels myLabels1 = myChart.SeriesCollection[0].DataLabelsCollection.Add(); myLabels1.NumberFormat = "0.00"; myLabels1.HasValue = true; myChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strValue); if (strValue1 != "") { myChart.SeriesCollection.Add(1); myChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimSeriesNames, (int)ChartSpecialDataSourcesEnum.chDataLiteral, mDataTable.Columns[2].Caption); myChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strValue1); ChDataLabels myLabels2 = myChart.SeriesCollection[1].DataLabelsCollection.Add(); myLabels2.NumberFormat = "0.00"; myLabels2.HasValue = true; } //if (strValue2 !="") //{ // myChart.SeriesCollection.Add(2); // myChart.SeriesCollection[2].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strValue2); // ChDataLabels myLabels3 = myChart.SeriesCollection[2].DataLabelsCollection.Add(); // myLabels3.NumberFormat = "0.00"; // myLabels3.HasValue = true; //} //if (pictureBox1.Image != null) //{ // pictureBox1.Image.Dispose(); //} string mPath = Application.StartupPath + @"temp.gif"; //临时图片保存地址 System.IO.File.Delete(mPath); mySpace.ExportPicture(mPath, "GIF", pictureBox1.Width, pictureBox1.Height); //Image image = Image.FromFile(@"C:\temp.gif"); //Image image1 = (Image)image.Clone(); //image.Dispose(); FileStream mFs = File.OpenRead(mPath); pictureBox1.Image = Image.FromStream(mFs); mFs.Close(); //pictureBox1.Size = image.Size; //pictureBox1.Visible = true; }
private void MakeLineChart(DataTable mDataTable)//string[] DataName,string[] Data,string[] Data1, string[] Data2) { //Y坐标轴 //string[] DataName = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" }; //string[] DataName = { "一", "二", "三", "四", "五", "六", "日" }; //第一条曲线的数据 //int[] Data = { 0, 10, 20, 100, 40, 50, 60 }; //第二条曲线的数据 //int[] Data1 = { 100, 50, 41, 86 }; //第三条曲线的数据 // int[] Data2 = { 10, 50, 100, 30, 50, 60, 200 }; string strValue1 = ""; string strValue = ""; string strValue2 = ""; string strCateory = ""; //循环取得数据并格式化为OWC10需要的格式,(加'\t') foreach (DataRow mRow in mDataTable.Rows) { strCateory += mRow[0].ToString() + '\t'; strValue += mRow[1].ToString() + '\t'; strValue1 += mRow[2].ToString() + '\t'; } //for (int i = 0; i < DataName.Length; i++) //{ // strCateory += DataName[i] + '\t'; //} //for (int i = 0; i < Data.Length; i++) //{ // strValue += Data[i] + '\t'; //} //for (int i = 0; i < Data1.Length; i++) //{ // strValue1 += Data1[i] + '\t'; //} //for (int i = 0; i < Data2.Length; i++) //{ // strValue2 += Data2[i] + '\t'; //} ChartSpace mySpace = new ChartSpace(); ChChart myChart = mySpace.Charts.Add(0); myChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered; myChart.HasLegend = true; myChart.HasTitle = true; myChart.Title.Caption = "交易曲线图"; myChart.Axes[0].HasTitle = true; myChart.Axes[0].Title.Caption = mDataTable.Columns[0].Caption; myChart.Axes[1].HasTitle = true; myChart.Axes[1].Title.Caption = "总额"; myChart.SeriesCollection.Add(0); //myChart. myChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, (int)ChartSpecialDataSourcesEnum.chDataLiteral, mDataTable.Columns[1].Caption); myChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strCateory); ChDataLabels myLabels1 = myChart.SeriesCollection[0].DataLabelsCollection.Add(); myLabels1.NumberFormat = "0.00"; myLabels1.HasValue = true; myChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strValue); if (strValue1 != "") { myChart.SeriesCollection.Add(1); myChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimSeriesNames, (int)ChartSpecialDataSourcesEnum.chDataLiteral, mDataTable.Columns[2].Caption); myChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strValue1); ChDataLabels myLabels2 = myChart.SeriesCollection[1].DataLabelsCollection.Add(); myLabels2.NumberFormat = "0.00"; myLabels2.HasValue = true; } //if (strValue2 !="") //{ // myChart.SeriesCollection.Add(2); // myChart.SeriesCollection[2].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strValue2); // ChDataLabels myLabels3 = myChart.SeriesCollection[2].DataLabelsCollection.Add(); // myLabels3.NumberFormat = "0.00"; // myLabels3.HasValue = true; //} System.IO.File.Delete(@"C:\temp.gif"); mySpace.ExportPicture(@"C:\temp.gif", "GIF", this.Width, this.Height); Image image = Image.FromFile(@"C:\temp.gif"); pictureBox1.Image = image; pictureBox1.Size = image.Size; pictureBox1.Visible = true; }
public string PieChart(Pie pie, Page page) { //创建X坐标的值 string[] str = pie.DataNames; //创建Y坐标的值,表示销售额 double[] count = pie.DataValues; string strDataName = ""; string strData = ""; //创建图表空间 ChartSpace mychartSpace = new ChartSpace(); mychartSpace.Border.Color = pie.ChartBorderColor; //在图表空间内添加一个图表对象 ChChart mychart = mychartSpace.Charts.Add(0); //设置每块饼的数据 for (int i = 0; i < count.Length; i++) { strDataName += str[i].ToString() + "\t"; strData += count[i].ToString() + "\t"; } strDataName = strDataName.Substring(0, strDataName.Length - 1); strData = strData.Substring(0, strData.Length - 1); //设置图表类型,本例使用饼 switch (pie.PicChartType) { case PieType.Pie: mychart.Type = ChartChartTypeEnum.chChartTypePie; break; case PieType.Pie3D: mychart.Type = ChartChartTypeEnum.chChartTypePie3D; break; case PieType.Exploded: mychart.Type = ChartChartTypeEnum.chChartTypePieExploded; break; case PieType.Exploded3D: mychart.Type = ChartChartTypeEnum.chChartTypePieExploded3D; break; default: mychart.Type = ChartChartTypeEnum.chChartTypePie; break; } //设置图表的一些属性 //是否需要图例 mychart.HasLegend = true; //是否需要主题 mychart.HasTitle = true; //主题内容 mychart.Title.Caption = pie.ChartTitle; mychart.Title.Font.Size = pie.ChartTitleSize; mychart.Title.Font.Bold = pie.ChartTitleBold; mychart.Title.Font.Color = pie.ChartTitleColor; switch (pie.LegendPosition) { case LegendPosition.Top: mychart.Legend.Position = ChartLegendPositionEnum.chLegendPositionTop; break; case LegendPosition.Bottom: mychart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom; break; case LegendPosition.Left: mychart.Legend.Position = ChartLegendPositionEnum.chLegendPositionLeft; break; case LegendPosition.Right: mychart.Legend.Position = ChartLegendPositionEnum.chLegendPositionRight; break; default: mychart.Legend.Position = ChartLegendPositionEnum.chLegendPositionRight; break; } mychart.Legend.Interior.Color = pie.LegendBgColor; mychart.Legend.Font.Bold = pie.LegenFontBold; mychart.Legend.Font.Size = pie.LegendFontSize; mychart.Legend.Border.Color = pie.LegendBorderColor; //添加图表块 mychart.SeriesCollection.Add(0); //设置图表块的属性 //分类属性 mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strDataName); //mychart.SeriesCollection[0].Interior.Color = "#C1DBEE"; //mychart.SeriesCollection[1].Interior.Color = "#D1A00B"; //值属性 mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strData); for (int j = 0; j < mychart.SeriesCollection[0].Points.Count; j++) { mychart.SeriesCollection[0].Points[j].Border.Color = pie.SeriesCollectionBorderColor; if (pie.DataColor != null) { mychart.SeriesCollection[0].Points[j].Interior.Color = pie.DataColor[j].ToString(); } } //显示百分比 ChDataLabels mytb = mychart.SeriesCollection[0].DataLabelsCollection.Add(); mytb.HasPercentage = pie.HasPercentage; mytb.Font.Color = pie.DataFontColor; mytb.Font.Size = pie.DataFontSize; mytb.HasValue = true; //生成图片 //劉宏哲修改,先删除文件再创建文件。解决第一次生成图片以后,再次生成报错。时间:2010-04-21 9:44。 string path = page.MapPath(".") + @"\" + pie.PicName + ".gif"; if (File.Exists(path)) { File.Delete(path); } mychartSpace.ExportPicture(path, "gif", pie.ChartWidth, pie.ChartHeight); //返回图片路径 return(pie.PicName + ".gif" + "?temp=" + System.DateTime.Now.Ticks.ToString() + ""); }