gpt4 book ai didi

c# - 使用图表辅助轴会导致 x 轴和主要 y 轴问题 (Excel)

转载 作者:行者123 更新时间:2023-12-02 01:09:39 25 4
gpt4 key购买 nike

使用辅助轴制作图表,使我的图表主要 y 轴 显示一些我不希望有的值。

x 轴 和辅助y 轴。而且 x 轴 是在没有我传递的日期值的情况下绘制的。

代码:

chartType2 = GetChartType(worksheet, chartToDraw, endcolcnt, i, chartType2, chartType);
chartType2.UseSecondaryAxis = true;
Scale(headerString, endcolcnt, worksheet, chartType2, stcol, isFieldSame, endcol, stcolumn1, endrow, startRow);

和缩放功能仅分配标题名称和所有内容。

拍摄系列的详细信息 enter image description here

输出: exceloutput

输入 input

最佳答案

如果没有更多代码,很难说。这些函数到底在做什么?

您是否想将轴置于右侧?根据您发布的黑色图表,这看起来像是您所追求的。您只需执行 chartType.YAxis.Crosses = eCrosses.Max 即可。

或者您实际上想要两个轴,这需要两个图表/系列?如果您想要这样做,那么您需要基于第一个图表创建第二个图表(看起来您的函数可能正在这样做),然后向每个图表添加一个唯一的系列,但具有通用的 x 值数据集。只需确保以正确的顺序添加它们即可。

这显示了两种情况:

[TestMethod]
public void Chart_Secondary_Axis_Test()
{
//http://stackoverflow.com/questions/28540458/using-secondary-axis-for-chart-cause-x-axis-and-primary-y-axis-issue-excel
var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
if (existingFile.Exists)
existingFile.Delete();

using (var pck = new ExcelPackage(existingFile))
{
var wsContent = pck.Workbook.Worksheets.Add("Content");

//Some data
wsContent.Cells["A1"].Value = "A"; wsContent.Cells["B1"].Value = "B"; wsContent.Cells["C1"].Value = "C"; wsContent.Cells["D1"].Value = "D";
wsContent.Cells["A2"].Value = 100; wsContent.Cells["A3"].Value = 400; wsContent.Cells["A4"].Value = 200; wsContent.Cells["A5"].Value = 300; wsContent.Cells["A6"].Value = 600; wsContent.Cells["A7"].Value = 500;
wsContent.Cells["B2"].Value = 300; wsContent.Cells["B3"].Value = 200; wsContent.Cells["B4"].Value = 1000; wsContent.Cells["B5"].Value = 600; wsContent.Cells["B6"].Value = 500; wsContent.Cells["B7"].Value = 200;
wsContent.Cells["D2"].Value = new DateTime(2015, 1, 1); wsContent.Cells["D3"].Value = new DateTime(2015, 1, 2); wsContent.Cells["D4"].Value = new DateTime(2015, 1, 3); wsContent.Cells["D5"].Value = new DateTime(2015, 1, 4); wsContent.Cells["D6"].Value = new DateTime(2015, 1, 5); wsContent.Cells["D7"].Value = new DateTime(2015, 1, 6);
const int dataRow = 7;
const string FORMATDATE = "m/d/yy";
wsContent.Cells[2, 4, dataRow, 4].Style.Numberformat.Format = FORMATDATE;


//Single Axis with intersection on the right
var chart1 = wsContent.Drawings.AddChart("Chart1", eChartType.XYScatterLines);
chart1.SetSize(600, 400);

var serie1 = (ExcelScatterChartSerie)chart1.Series.Add(wsContent.Cells[2, 1, dataRow, 1], wsContent.Cells[2, 4, dataRow, 4]);
serie1.Header = wsContent.Cells[1, 1].Value.ToString();

chart1.YAxis.Crosses = eCrosses.Max;


//Dual Axis
var chart2a = wsContent.Drawings.AddChart("Chart2", eChartType.ColumnStacked);
chart2a.SetSize(600, 400);
chart2a.SetPosition(400, 0);

var serie2a = chart2a.Series.Add(wsContent.Cells[2, 2, dataRow, 2], wsContent.Cells[2, 4, dataRow, 4]);
serie2a.Header = wsContent.Cells[1, 2].Value.ToString();

var chart2b = chart2a.PlotArea.ChartTypes.Add(eChartType.XYScatterLines);

var serie2b = chart2b.Series.Add(wsContent.Cells[2, 1, dataRow, 1], wsContent.Cells[2, 4, dataRow, 4]);
serie2b.Header = wsContent.Cells[1, 1].Value.ToString();

chart2b.UseSecondaryAxis = true; //Flip the axes


pck.Save();
}
}

enter image description here

关于c# - 使用图表辅助轴会导致 x 轴和主要 y 轴问题 (Excel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28540458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com