Search code examples
c#interopword-interop

Connect Empty Points in Word Chart


I'm creating a Chart in my C# Application with Word Interop. Inside this graph i have some empty data points. How can i connect the empty points programmatically? I'd like to achieve this but inside code. Can anybode help me? Thank you

Edit: Here is the Code

Dictionary<DateTime, Dictionary<string, decimal>> visuData = new Dictionary<DateTime, Dictionary<string, decimal>>(); 
string startTime = null;
string endTime = null;          //Variablen für die Zeitspanne, aus der Versuchsdaten abgerufen werden sollen
query = "SELECT MIN(Starttime) AS Starttime, MAX(Starttime) AS Endtime FROM dbo.Table WHERE ID = " + checkBox.Text;
con = new SqlConnection(mtecConnectionString);
cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    startTime = reader.GetDateTime(0).ToString("yyyy-MM-dd hh:mm:ss.fff");
    endTime = reader.GetDateTime(1).ToString("yyyy-MM-dd hh:mm:ss.fff");
}
con.Close();            //Zeitspanne bestimmen
if (startTime == null || endTime == null)
    return;         //Falls keine Daten vorliegen, wird die Methode beendet

List<string> variables = new List<string>();
query = "SELECT ...";
con = new SqlConnection(@"...");
cmd = new SqlCommand(query, con);           //Abfrage für Versuchsdaten und Verbindung zur Datenbank setzen
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
    if (visuData.ContainsKey(reader.GetDateTime(2)))
    {
        visuData[reader.GetDateTime(2)].Add(reader.GetString(0), 
        reader.GetDecimal(1));
    }
    else
    {
        visuData.Add(reader.GetDateTime(2), new Dictionary<string, decimal>());
        visuData[reader.GetDateTime(2)].Add(reader.GetString(0), 
        reader.GetDecimal(1));
    }

    if (!variables.Contains(reader.GetString(0)))
        variables.Add(reader.GetString(0));
}
 con.Close();

Chart chart = document.InlineShapes.AddChart2(-1, XlChartType.xlLineMarkers, paragraph.Range).Chart;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = chart.ChartData.Workbook;
excelWorkbook.Application.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = excelWorkbook.Worksheets[1];

for(int j = 2; j < variables.Count + 2; j++)
{
    excelWorksheet.Cells[1, j].Value = variables[j - 2];
}
int rowIndex = 2;
foreach (DateTime moment in visuData.Keys)
{
    excelWorksheet.Cells[rowIndex, 1].Value = moment.ToString("HH:mm:ss");
    for(int j = 2; j < variables.Count + 2; j++)
    {
        if (visuData[moment].ContainsKey(variables[j - 2]))
            excelWorksheet.Cells[rowIndex, j].Value = visuData[moment][variables[j - 2]];
    }
    rowIndex += 1;
}

chart.ChartTitle.Text = "Batch ID " + checkBox.Text;
chart.Refresh();
excelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);

I want to display a few variables over time. So I created a dict with the timestamp as key and another dict as value. Inside this second dict I'm using the key to identify the variable and the value to get the value.

This looks like: 2021-12-02 08:00:00 --> Var 1 : 1 2021-12-02 08:00:00 --> Var 2 : 2

2021-12-02 08:05:00 --> Var 1 : 3 2021-12-02 08:05:00 --> Var 3 : 4

The actual Output is, that I have a chart in the created word document. For Var 3 is the value 0 at 8am displayed but I dont want any value for var 3 at 8 am. Same thing with Var 2...

I hope its more clear now


Solution

  • I found the solution myself.

    I added before refreshing the chart folling line:

    chart.DisplayBlanksAs = Microsoft.Office.Interop.Word.XlDisplayBlanksAs.xlInterpolated;