将多个图表添加到EPPlus工作表,努力使布局正确

时间:2015-10-23 14:20:11

标签: c# excel charts epplus

我目前正在通过EPPlus创建Excel电子表格,数据基本上分为,部分,问题,回复,每个都是另一个的子集,我不知道每个部分可能有多少个问题,所以一切需要通过循环来完成。

我想要的是文档包含一个工作表,显示每个问题的图表,给出答案的细分。

我非常接近让它正常工作,但我的代码中的某些内容导致了问题而我无法理解它是什么。

我目前使用的数据包含10个问题,所有图表都已绘制,但最后一个图表直接位于第一个图表的顶部。我也遇到了正确定位文本结果的问题,但由于代码依赖于图表的代码,我希望对图表进行排序可以帮助我对结果进行排序。

这是我的代码:

 Int32 chartPaddingX = 50;
 Int32 chartPaddingY = 200;
 Int32 chartW = 320;
 Int32 chartH = 200;
 Int32 chartXCount = 0;
 Int32 chartYCount = 1;
 Int32 marginTop = 0;
 Int32 marginLeft = 0;
 Int32 resultsControlVar = 0;
 Int32 numCharts = 0;
 Int32 rnStart = 14;
 chartXCount = 0;
 chartYCount = 0;
 chartPaddingX = 50;

 rowNum = y = 14;
 colNum = 2;


 String title = section.First().title.ToString();
 ExcelWorksheet ws = pkg.Workbook.Worksheets.Add(title);

 int idx = 0;

 foreach (var question in results.questions.Where(q => q.section_guid == section.Key))
 {
     var chart = ws.Drawings.AddChart(question.question.ToString(), OfficeOpenXml.Drawing.Chart.eChartType.ColumnClustered);
     chart.SetSize(chartW, chartH);
     chart.Title.Text = question.question.ToString();
     chart.Legend.Add();

     marginLeft = (chartW + chartPaddingX) * chartXCount;

     // SET CHART PLACEMENT
     if (numCharts == 0)
     {
         marginTop = 50;
     }
     else if ((numCharts <= 3) && (numCharts != 0))
     {
         marginTop = 50;
     }
     else if (numCharts > 3)
     {
         marginTop = ((chartH + chartPaddingY) * chartYCount) + 50;
     }
     else
     {
         marginTop = (chartH + chartPaddingY) * chartYCount;
     }

     if (chartXCount >= 3)
     {
         chart.SetPosition(marginTop, 50);
         chartYCount++;
         chartXCount = 0;
         resultsControlVar = 1;
     }
         else
     {
         chart.SetPosition(marginTop, marginLeft + 50);
     }

     chartXCount++;
     numCharts++;

     ...... Add results data and chart series ......

      if (resultsControlVar == 1)
      {
          rowNum = rnStart;
          rowNum = rowNum + 20;
          rnStart = rowNum;
          colNum = 2;
          resultsControlVar = 0;
      }
      else
      {
          rowNum = rnStart;
          colNum = colNum + 6;
      }
  } // close foreach

2 个答案:

答案 0 :(得分:1)

检查你的算法逻辑,它没有给出你可能期望的左/顶。如果你把它剥离到最低限度:

[TestMethod]
public void Check_Multi_Test()
{
    Int32 chartPaddingX = 50;
    Int32 chartPaddingY = 200;
    Int32 chartW = 320;
    Int32 chartH = 200;
    Int32 chartXCount = 0;
    Int32 chartYCount = 1;
    Int32 marginTop = 0;
    Int32 marginLeft = 0;
    Int32 numCharts = 0;
    chartXCount = 0;
    chartYCount = 0;


    //foreach (var question in results.questions.Where(q => q.section_guid == section.Key))
    for (var i = 0; i < 10; i++)
    {
        marginLeft = (chartW + chartPaddingX)*chartXCount;

        // SET CHART PLACEMENT
        if (numCharts == 0)
        {
            marginTop = 50;
        }
        else if ((numCharts <= 3) && (numCharts != 0))
        {
            marginTop = 50;
        }
        else if (numCharts > 3)
        {
            marginTop = ((chartH + chartPaddingY)*chartYCount) + 50;
        }
        else
        {
            marginTop = (chartH + chartPaddingY)*chartYCount;
        }

        if (chartXCount >= 3)
        {
            //chart.SetPosition(marginTop, 50);
            Console.WriteLine("{{i: {0}, numCharts: {1}, left: {2}, top: {3}}}", i, numCharts, 50, marginTop); //Console.Writeline will simulate chart placement
            chartYCount++;
            chartXCount = 0;
        }
        else
        {
            //chart.SetPosition(marginTop, marginLeft + 50);
            Console.WriteLine("{{i: {0}, numCharts: {1}, left: {2}, top: {3}}}", i, numCharts, marginLeft + 50, marginTop); //Console.Writeline will simulate chart placement
        }

        chartXCount++;
        numCharts++;
    }
}

并向左/上输出到控制台,您可以轻松地看到从迭代3开始的错误:

{i: 0, numCharts: 0, left: 50,  top: 50}
{i: 1, numCharts: 1, left: 420, top: 50}
{i: 2, numCharts: 2, left: 790, top: 50}
{i: 3, numCharts: 3, left: 50,  top: 50}
{i: 4, numCharts: 4, left: 420, top: 450}
{i: 5, numCharts: 5, left: 790, top: 450}
{i: 6, numCharts: 6, left: 50,  top: 450}
{i: 7, numCharts: 7, left: 420, top: 850}
{i: 8, numCharts: 8, left: 790, top: 850}
{i: 9, numCharts: 9, left: 50,  top: 850}

实际上不应该这么复杂:

//foreach (var question in results.questions.Where(q => q.section_guid == section.Key))
for (var i = 0; i < 10; i++)
{
    marginLeft = (chartW + chartPaddingX)*chartXCount + 50;
    marginTop = (chartH + chartPaddingY)*chartYCount + 50;

    //chart.SetPosition(marginTop, marginLeft);  //Console.Writeline will simulate chart placement
    Console.WriteLine("{{i: {0}, numCharts: {1}, left: {2}, top: {3}}}", i, numCharts, marginLeft, marginTop);

    if (chartXCount >= 2)
    {
        chartYCount++;
        chartXCount = 0;
    }
    else
        chartXCount++;

    numCharts++;
}

给你这个:

{i: 0, numCharts: 0, left: 50,  top: 50}
{i: 1, numCharts: 1, left: 420, top: 50}
{i: 2, numCharts: 2, left: 790, top: 50}
{i: 3, numCharts: 3, left: 50,  top: 450}
{i: 4, numCharts: 4, left: 420, top: 450}
{i: 5, numCharts: 5, left: 790, top: 450}
{i: 6, numCharts: 6, left: 50,  top: 850}
{i: 7, numCharts: 7, left: 420, top: 850}
{i: 8, numCharts: 8, left: 790, top: 850}
{i: 9, numCharts: 9, left: 50,  top: 1250}

答案 1 :(得分:0)

接受了Ernie的答案,因为他在我之前到达那里并提供了一些有用的调试技巧。非常感谢你的时间厄尼。

我确实今天早上设法解决了这个问题,然后再回过头来看看这个问题:

foreach (var question in results.questions.Where(q => q.section_guid == section.Key))
{
// SET CHART PLACEMENT
if (chartXCount != chartXMax)
{
    if (numCharts == 0)
    {
       chartX = ((chartW + chartPaddingX) * chartXCount) + 50;
       chartY = ((chartH + chartPaddingY) * chartYCount) + 50;
    }
    else
    {
       chartX = ((chartW + chartPaddingX) * chartXCount) + 50;
    }
 chart.SetPosition(chartY, chartX);
 chartXCount++;
 }
 else
 {
     chartX = ((chartW + chartPaddingX) * chartXCount) + 50;
     chart.SetPosition(chartY, chartX);

     chartYCount++;
     resultsControlVar = 1;
 }

 numCharts++;

 ... DOING SOME OTHER BITS N BOBS ...

 // INCREMENT Y VALUE
 if (resultsControlVar == 1) // INCREMENT THE YPOS VALUE OF THE CHART
 {
     rowNum = rnStart;
     rowNum = rowNum + 20;
     rnStart = rowNum;
     colNum = 2;
     chartY = ((chartH + chartPaddingY) * chartYCount) + 50;
     resultsControlVar = 0;
     chartXCount = 0;
  }
  else                      // RESETS THE ROWNUM FOR DATA VALUES
  {
     rowNum = rnStart;
     colNum = colNum + 6;
  }
}