1, 在excel里面多个柱状图会重叠在图表里面,这是因为多个柱状图不重叠的话type要设置成Orig.ChartType =
XlChartType.xlColumnClustered;; 重叠的话,Orig.ChartType = XlChartType.xlColumnStacked;
2 关于图例:如果设置position 的话,图例会横向排列,不设置
// chart.Legend.Position =
Excel.XlLegendPosition.xlLegendPositionTop; 图例会竖着排列
chart.Legend.Left = 80;
chart.Legend.Height = 15* list.Count();
chart.Legend.IncludeInLayout = true;
chart.Legend.Top = 0;
chart.Legend.Width = 150;
// chart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop;
chart.Legend.Font.Color = ColorTranslator.ToOle(Color.Black);
3 xAxis.MinorTickMark =
Excel.XlTickMark.xlTickMarkOutside;坐标轴刻度
xAxis.TickLabels.NumberFormat = "yyyy-mm-dd"; 坐标轴时间设置
4 调用
Microsoft.Office.Interop.Excel.Shape myChart = newWorksheet.Shapes.AddChart2(-1, XlChartType.xlLine, 250, 50, width, 165, true); 会在图表默认生成一条空数据的图形,要删除
Excel.SeriesCollection seriescollectionB2 = chart.SeriesCollection();
int cc = seriescollectionB2.Count;
seriescollectionB2.Item(1).Delete();
#function
public static Microsoft.Office.Interop.Excel.Shape GetChart(Excel.Application eApp,Document document, WordCharItem onechartData, ContentControl c, int width)
{
Excel.Worksheet newWorksheet = eApp.Worksheets.Add();//获取第一个Worksheet
//newWorksheet.Activate();
//向表格中插入数据
string[,] array = ExtractHelper.GetKeppArray(onechartData.chartIndexDataList);
var list = onechartData.chartIndexList.OrderBy(p => p.axisType);
int indexcount = list.Count();
int rowIndex = 0;
int columnIndex = 1;
int columnlength1 = onechartData.chartIndexDataList[0].Split(ExtractHelper.splitchar).Length;
int headerCount = 1;
int lenght = onechartData.chartIndexDataList.Length - headerCount;
int rowlenght = onechartData.chartIndexDataList.Length - 1;
Microsoft.Office.Interop.Excel.Range c1 = newWorksheet.Cells[rowIndex + 1, columnIndex];
Microsoft.Office.Interop.Excel.Range c3 = newWorksheet.Cells[rowIndex + rowlenght, columnlength1];
Microsoft.Office.Interop.Excel.Range range3 = newWorksheet.Range[c1, c3];
range3.Value2 = array;
//range3.NumberFormat= numberFormat;
int SeriesCount = onechartData.chartIndexList.Count();
Microsoft.Office.Interop.Excel.Range timeStartRange = newWorksheet.Cells[1, 1];
Microsoft.Office.Interop.Excel.Range timeEndRange = newWorksheet.Cells[rowlenght, 1]; ;
Microsoft.Office.Interop.Excel.Range XValues = newWorksheet.Range[timeStartRange, timeEndRange];
XValues.NumberFormat = "yyyy/m/d";
Microsoft.Office.Interop.Excel.Range dataStartRange = newWorksheet.Cells[1, 2];
Microsoft.Office.Interop.Excel.Range dataEndRange = newWorksheet.Cells[rowlenght, columnlength1];
Microsoft.Office.Interop.Excel.Range dataRANGE = newWorksheet.Range[dataStartRange, dataEndRange];
dataRANGE.NumberFormat = numberFormat;
Microsoft.Office.Interop.Excel.Shape myChart = newWorksheet.Shapes.AddChart2(-1, XlChartType.xlLine, 250, 50, width, 165, true);
Excel.Chart chart = myChart.Chart;
// 设置图表标题
chart.HasTitle = true;
chart.ChartTitle.Text = onechartData.title;
chart.ChartTitle.Font.Name = zhongwenstr;
chart.ChartTitle.Font.Size = 8f;
chart.ChartTitle.Font.Bold = 1;
chart.ChartTitle.Font.Color = ColorTranslator.ToOle(Color.Black);
chart.ChartTitle.HorizontalAlignment = XlConstants.xlLeft;
chart.ChartTitle.Left = 0;
//chart.ChartArea.Left = 0;
chart.PlotArea.InsideLeft = 3;
chart.PlotArea.InsideWidth = 230;
Excel.SeriesCollection seriescollectionB2 = chart.SeriesCollection();
seriescollectionB2.Item(1).Delete();
string[] xarray = RangeHelper.GetOneColumnRange(XValues);
for (int i = 1; i <= indexcount; i++)
{
Excel.Series Orig = seriescollectionB2.NewSeries();
double[] yarrayy = new double[rowlenght];
for (int ii = 1; ii <= rowlenght; ii++)
{
Microsoft.Office.Interop.Excel.Range range = newWorksheet.Cells[ii, i + 1];
if (string.IsNullOrEmpty(range.Value2))
{
// yarrayy[ii - 1] = 0;
}
else
{
yarrayy[ii - 1] = Convert.ToDouble(range.Value2);
}
}
Orig.Name = list.ElementAt(i - 1).indexName;
Orig.XValues = xarray;

Orig.Values = yarrayy;
Orig.HasDataLabels = false;
if (i == 1)
{
Orig.AxisGroup = Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary;
if (list.ElementAt(i-1).type == 1)
{
Orig.ChartType = XlChartType.xlLineStacked;
Orig.Format.Line.Weight = 1.2f;
Orig.Format.Line.ForeColor.RGB = StyleHelper.GetColorIntRGB(linecolor);
}
if (list.ElementAt(i-1).type == 2)
{
Orig.ChartType = XlChartType.xlColumnStacked;
Orig.Interior.Color = ColorTranslator.ToOle(Color.Gray);
}
Excel.Axis yAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
yAxis.HasDisplayUnitLabel = true;
yAxis.DisplayUnitCustom = 1;
yAxis.DisplayUnitLabel.Orientation = XlOrientation.xlHorizontal;
yAxis.DisplayUnitLabel.Left = 15;
yAxis.DisplayUnitLabel.Top = 10;
yAxis.DisplayUnitLabel.Font.Name = zhongwenstr;
yAxis.DisplayUnitLabel.Font.Size = 8f;
//yAxis.DisplayUnit=XlDisplayUnit.
yAxis.TickLabels.Font.Name = fontname;
yAxis.TickLabels.Font.Size = fontsize;
yAxis.TickLabels.Font.Color = XlRgbColor.rgbBlack;
yAxis.MajorTickMark = Excel.XlTickMark.xlTickMarkOutside;
yAxis.HasMinorGridlines = false;
yAxis.HasMajorGridlines = false;
yAxis.Format.Line.ForeColor.RGB = (int)XlRgbColor.rgbBlack;
yAxis.Format.Line.Weight = 1;
yAxis.Border.Color = ColorTranslator.ToOle(Color.Black);//y轴颜色
yAxis.DisplayUnitLabel.Text = list.ElementAt(i-1).unit;
}
if (i == 2)
{
if (list.ElementAt(i-1).type == 1)
{
Orig.ChartType = XlChartType.xlLineStacked;
Orig.Format.Line.Weight = 1.2f;
Orig.Format.Line.ForeColor.RGB = StyleHelper.GetColorIntRGB("0,174,239");
}
if (list.ElementAt(i-1).type == 2)
{
Orig.ChartType = XlChartType.xlColumnStacked;
Orig.Interior.Color = ColorTranslator.ToOle(Color.Gray);
}
Orig.AxisGroup = Microsoft.Office.Interop.Excel.XlAxisGroup.xlSecondary;
Excel.Axis ysecondAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary);
ysecondAxis.HasDisplayUnitLabel = true;
//ysecondAxis.DisplayUnit = XlDisplayUnit.xlHundreds;
ysecondAxis.DisplayUnitCustom = 1;
ysecondAxis.DisplayUnitLabel.Left = 210;
ysecondAxis.DisplayUnitLabel.Top = 10;
//ysecondAxis.DisplayUnitLabel.VerticalAlignment=
ysecondAxis.DisplayUnitLabel.Orientation = XlOrientation.xlHorizontal;
ysecondAxis.DisplayUnitLabel.Position = Excel.XlChartElementPosition.xlChartElementPositionCustom;
ysecondAxis.DisplayUnitLabel.Font.Name = zhongwenstr;
ysecondAxis.DisplayUnitLabel.Font.Size = 8f;
ysecondAxis.TickLabels.Font.Name = fontname;
ysecondAxis.TickLabels.Font.Size = fontsize;
ysecondAxis.TickLabels.Font.Color = ColorTranslator.ToOle(Color.Black);
ysecondAxis.DisplayUnitLabel.Text = list.ElementAt(i-1).unit;
ysecondAxis.Border.Color = ColorTranslator.ToOle(Color.Black);//y轴颜色
}
if (i == 3)
{
Orig.AxisGroup = Microsoft.Office.Interop.Excel.XlAxisGroup.xlSecondary;
if (list.ElementAt(i-1).type == 1)
{
Orig.ChartType = XlChartType.xlLine;
Orig.Format.Line.ForeColor.RGB = StyleHelper.GetColorIntRGB(linecolor);
Orig.Format.Line.Weight = 1.2f;
}
if (list.ElementAt(i-1).type == 2)
{
Orig.ChartType = XlChartType.xlColumnClustered;
Orig.Interior.Color = ColorTranslator.ToOle(Color.Gray);
}
Orig.HasDataLabels = false;
//StyleHelper.SetSeries(Orig, "55,155,30");
Orig.Format.Line.ForeColor.RGB = StyleHelper.GetColorIntRGB(linecolor);
Orig.Format.Line.Weight = 1;
Excel.Axis ysecondAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary);
ysecondAxis.HasDisplayUnitLabel = true;
ysecondAxis.DisplayUnitCustom = 1;
ysecondAxis.DisplayUnitLabel.Orientation = XlOrientation.xlHorizontal;
ysecondAxis.DisplayUnitLabel.Font.Name = zhongwenstr;
ysecondAxis.DisplayUnitLabel.Font.Size = 8f;
ysecondAxis.TickLabels.Font.Name = fontname;
ysecondAxis.TickLabels.Font.Size = fontsize;
ysecondAxis.TickLabels.Font.Color = XlRgbColor.rgbBlack;
ysecondAxis.DisplayUnitLabel.Text = list.ElementAt(i-1).unit;
ysecondAxis.Border.Color = ColorTranslator.ToOle(Color.Black);//y轴颜色
}
if (i == 4)
{
Orig.AxisGroup = Microsoft.Office.Interop.Excel.XlAxisGroup.xlSecondary;
if (list.ElementAt(i-1).type == 1)
{
Orig.ChartType = XlChartType.xlXYScatterLinesNoMarkers;
Orig.Format.Line.ForeColor.RGB = StyleHelper.GetColorIntRGB("0,174,239");
Orig.Format.Line.Weight = 1.2f;
}
if (list.ElementAt(i-1).type == 2)
{
Orig.Interior.Color = StyleHelper.GetColorIntRGB("0,55,120");
Orig.ChartType = XlChartType.xlColumnClustered;
}
//StyleHelper.SetSeries(Orig, "5,15,130");
Orig.Format.Line.ForeColor.RGB = StyleHelper.GetColorIntRGB(linecolor);
Orig.Format.Line.Weight = 1;
Excel.Axis ysecondAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary);
ysecondAxis.HasDisplayUnitLabel = true;
ysecondAxis.DisplayUnitLabel.Orientation = XlOrientation.xlHorizontal;
ysecondAxis.DisplayUnitCustom = 1;
ysecondAxis.DisplayUnitLabel.Font.Name = zhongwenstr;
ysecondAxis.DisplayUnitLabel.Font.Size = 8f;
ysecondAxis.TickLabels.Font.Name = fontname;
ysecondAxis.TickLabels.Font.Size = fontsize;
ysecondAxis.TickLabels.Font.Color = XlRgbColor.rgbBlack;
ysecondAxis.DisplayUnitLabel.Text = list.ElementAt(i-1).unit;
ysecondAxis.Border.Color = ColorTranslator.ToOle(Color.Black);//y轴颜色
}
}
// 设置y轴
//设置Y轴的显示
//yAxis.HasTitle = true;
Excel.Axis xAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
xAxis.CategoryNames = xarray;
//xAxis.CategoryType = Excel.XlCategoryType.xlTimeScale;
xAxis.TickLabelPosition = Excel.XlTickLabelPosition.xlTickLabelPositionLow;
xAxis.TickLabels.Orientation = Excel.XlTickLabelOrientation.xlTickLabelOrientationDownward;
xAxis.TickLabels.Font.Name = fontname;
xAxis.TickLabels.NumberFormat = "yyyy-mm-dd";
xAxis.TickLabels.Font.Size = 8f;
xAxis.TickLabels.Font.Color = XlRgbColor.rgbBlack;
xAxis.MinorTickMark = Excel.XlTickMark.xlTickMarkOutside;
// xAxis.HasTitle = true;
//xAxis.AxisTitle.Orientation = Excel.XlOrientation.xlHorizontal;
//yAxis.HasDisplayUnitLabel = true;
xAxis.Format.Line.ForeColor.RGB = (int)XlRgbColor.rgbBlack;
xAxis.Format.Line.Weight = 1;
xAxis.Border.Color = ColorTranslator.ToOle(Color.Black);//y轴颜色
xAxis.TickLabels.MultiLevel = true;
chart.HasLegend = true;
chart.ChartTitle.Font.Name = zhongwenstr;
chart.Legend.Left = 80;
chart.Legend.Height = 15 * indexcount;
chart.Legend.IncludeInLayout = true;
chart.Legend.Top = 0;
chart.Legend.Width = 150;
// chart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop;
chart.Legend.Font.Color = ColorTranslator.ToOle(Color.Black);
chart.Legend.Font.Name = zhongwenstr;
chart.Legend.Font.Size = 8f;
return myChart;






