将辅助轴用于图表会导致x轴和主要y轴问题(Excel) [英] Using secondary axis for chart cause x-axis and primary y-axis issue (Excel)

查看:177
本文介绍了将辅助轴用于图表会导致x轴和主要y轴问题(Excel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用辅助轴制作图表,
使我的图表成为主要的 y轴,其中显示了一些我不想拥有的值。

Making a chart using secondary axis, makes my chart primary y-axis is shown with some values which I don't want to have.

x轴和次要的 y轴
x轴的绘制都没有经过的日期值。

Only x-axis and secondary y-axis. and also the x-axis is drawn without the date values what I've passed.

代码:

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

和Scale Function仅分配标题名称和全部。

and Scale Function only assigns the header names and all.

有关拍摄的系列的详细信息

输出

输入

推荐答案

不用多说就很难说。这些功能到底在做什么?

Hard to say without more code. What are those functions doing exactly?

您是否要使轴位于右侧?根据您发布的黑色图表,您看起来像是在追求什么。您可以只执行 chartType.YAxis.Crosses = eCrosses.Max

Are you trying to just get the axis on the right side? Based on the black chart you posted that would seem like what you are after. You could just do chartType.YAxis.Crosses = eCrosses.Max.

还是您实际上想要两个需要两个图表/系列的轴?如果需要,则需要根据第一个图表创建第二个图表(看起来您的函数可能正在执行此操作),然后向每个图表添加唯一的序列,但要使用共同的x值数据集。只需确保以正确的顺序添加它们即可。

Or do you actually want TWO axes which would require two charts/series? If you want that then you would need to create a second chart based on the first (looks like your function might be doing that) and then add a unique series to each but with a common x-value dataset. Just make sure you add them in the right order.

这将显示两种情况:

[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();
    }
}

这篇关于将辅助轴用于图表会导致x轴和主要y轴问题(Excel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆