目的:設置chart的y坐標軸顯示值
用aspose.cell生成的chart生成的Y軸是默認生成的,自己要定義y軸坐標值
1.把數據源寫到excel里面,list里面
2.y軸坐標自己定義
第一種:默認設置:chart里面會自己定義y軸坐標
第二種:y周坐標以對數顯示 chart.ValueAxis.IsLogarithmic = true; 以10 100 1000格式顯示
第三種:只設置間隔值 chart.ValueAxis.MajorUnit =20000;,會自動獲取y軸的最大值與最小值
第四種:設置y軸的最大值與最小值,設置間隔值
//設置y坐標軸的厚度
chart.ValueAxis.AxisLine.Weight = WeightType.WideLine;
chart.ValueAxis.Title.Text = "y軸坐標";
chart.ValueAxis.MajorUnit =20000;//設置y軸的顯示值間隔
chart.ValueAxis.MaxValue = 200000;//設置y軸開始最大值
chart.ValueAxis.MinValue = 0;//設置y軸的最小值
3.設置右邊坐標軸是不是顯示
//設置右邊坐標軸顯示
chart.SecondValueAxis.IsVisible = true;
//設置y坐標軸間隔值字大小
chart.SecondValueAxis.TickLabels.Font.Size = 12;
chart.SecondValueAxis.Title.Text = "y軸坐標2";
導出效果:
//設置y坐標軸的厚度
chart.ValueAxis.AxisLine.Weight = WeightType.WideLine;
chart.ValueAxis.Title.Text = "y軸坐標";
chart.ValueAxis.MajorUnit =20000;//設置y軸的顯示值間隔
chart.ValueAxis.MaxValue = 200000;//設置y軸開始最大值
chart.ValueAxis.MinValue = 0;//設置y軸的最小值
第二種:y周坐標以對數顯示 chart.ValueAxis.IsLogarithmic = true; 以10 100 1000格式顯示
//設置y坐標軸的厚度
chart.ValueAxis.AxisLine.Weight = WeightType.WideLine;
chart.ValueAxis.Title.Text = "y軸坐標";
chart.ValueAxis.MajorUnit =20000;//設置y軸的顯示值間隔
chart.ValueAxis.MaxValue = 80000;//設置y軸開始最大值
chart.ValueAxis.MinValue = 0;//設置y軸的最小值
入口函數
public ActionResult excels() { WorkbookDesigner designer = new WorkbookDesigner(); string path = Server.MapPath("/Templete/11111.xls"); designer.Workbook.Open(path); Workbook workbook = designer.Workbook; CreateStaticData(workbook); CreateStaticReport(workbook); designer.Process(); //將流文件寫到客戶端流的形式寫到客戶端,名稱是_report.xls designer.Save("_report.xls", SaveType.OpenInExcel, FileFormatType.Excel2003, System.Web.HttpContext.Current.Response); Response.Flush(); Response.Close(); designer = null; // Response.End(); return View("getexcel"); }
設置數據源
private void CreateStaticData(Workbook workbook) { //Initialize Cells object Cells cells = workbook.Worksheets[0].Cells; //Put string into a cells of Column A cells["A1"].PutValue("class"); cells["A2"].PutValue("紅蘿卜"); cells["A3"].PutValue("白蘿卜"); cells["A4"].PutValue("青蘿卜"); //Put a value into a Row 1 cells["B1"].PutValue(2002); cells["C1"].PutValue(2003); cells["D1"].PutValue(2004); cells["E1"].PutValue(2005); cells["F1"].PutValue(2006); //Put a value into a Row 2 cells["B2"].PutValue(40000); cells["C2"].PutValue(45000); cells["D2"].PutValue(50000); cells["E2"].PutValue(55000); cells["F2"].PutValue(70000); //Put a value into a Row 3 cells["B3"].PutValue(10000); cells["C3"].PutValue(25000); cells["D3"].PutValue(40000); cells["E3"].PutValue(52000); cells["F3"].PutValue(60000); //Put a value into a Row 4 cells["B4"].PutValue(5000); cells["C4"].PutValue(15000); cells["D4"].PutValue(35000); cells["E4"].PutValue(30000); cells["F4"].PutValue(20000); }
設置chart y軸的顯示值
private void CreateStaticReport(Workbook workbook) { //初始化 Worksheet Worksheet sheet = workbook.Worksheets[0]; //設置 worksheet名稱 sheet.Name = "Line"; //設置worksheet不顯示 sheet.IsGridlinesVisible = false; //根據數據源 創建 chart int chartIndex = 0; chartIndex = sheet.Charts.Add(ChartType.Line, 5, 1, 29, 15); //初始化chart Chart chart = sheet.Charts[chartIndex]; //設置豎線不顯示 chart.CategoryAxis.MajorGridLines.IsVisible = false; //設置Title樣式 chart.Title.Text = "Sales By Class For Years"; chart.Title.TextFont.Color = Color.Black; chart.Title.TextFont.IsBold = true; chart.Title.TextFont.Size = 12; //設置chart的數據源 chart.NSeries.Add("B2:F4", false); chart.NSeries.CategoryData = "B1:F1"; //Set Nseries color varience to True chart.NSeries.IsColorVaried = true; //初始化 Cells Cells cells = workbook.Worksheets[0].Cells; //循環 cells for (int i = 0; i < chart.NSeries.Count; i++) { //設置系列的名稱 chart.NSeries[i].Name = cells[i + 1, 0].Value.ToString(); chart.NSeries[i].MarkerStyle = ChartMarkerType.Circle; //設置系列的名稱 Background 與ForeGround chart.NSeries[i].MarkerBackgroundColor = Color.Yellow; chart.NSeries[i].MarkerForegroundColor = Color.Gold; //設置系列標記 chart.NSeries[i].MarkerSize = 10; //設置Category的名稱 chart.CategoryAxis.Title.Text = "Year(2002-2006)"; chart.CategoryAxis.Title.TextFont.Color = Color.Black; chart.CategoryAxis.Title.TextFont.IsBold = true; chart.CategoryAxis.Title.TextFont.Size = 10; //設置圖例的位置 chart.Legend.Position = LegendPositionType.Top; } //設置y軸的樣式 chart.ValueAxis.TickLabelPosition = TickLabelPositionType.NextToAxis; chart.ValueAxis.TickLabels.Font.Color = Color.Gray; chart.ValueAxis.AxisBetweenCategories = false; //chart.ValueAxis.TickLabels.Font.Size = 13; chart.ValueAxis.TickLabels.Font.IsBold = true; //Y坐標軸對數間隔展示 // chart.ValueAxis.IsLogarithmic = true; chart.ValueAxis.MajorGridLines.Color = Color.Red; chart.ValueAxis.AxisLine.Color = Color.DarkGreen; //設置y坐標軸的厚度 chart.ValueAxis.AxisLine.Weight = WeightType.WideLine; chart.ValueAxis.Title.Text = "y軸坐標"; chart.ValueAxis.MajorUnit = 20000;//MajorUnit =2000; chart.ValueAxis.MaxValue = 200000; chart.ValueAxis.MinValue = 0; //設置右邊坐標軸顯示 chart.SecondValueAxis.IsVisible = true; //設置y坐標軸間隔值字大小 chart.SecondValueAxis.TickLabels.Font.Size = 12; chart.SecondValueAxis.Title.Text = "y軸坐標2"; // chart.SecondValueAxis.MinorGridLines.IsVisible = true; }