Skip to content

Commit

Permalink
Fixed insert and fill
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Jul 17, 2024
1 parent 9ee65b9 commit 130184c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 45 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## v1.0.5.0

* Added: Calculate entries: It's possible to select multiple rows or cells to calculate only that cells and also to select a row or cell and let then calculate all values below automatically
* Added: Filter entries
* Added: Select related row when clicking on graph
* Added: Automatically add missing datapoints. This allows you for example to change the first or last date and easily fill up datapoints until that dates
* Added: Calculate datapoints: It's possible to select multiple rows or cells to calculate only that cells and also to select a row or cell and let then calculate all values below automatically
* Added: Filter statistic entries
* Added: Insert and delete datapoints
* Added: Select related datapoint when clicking on graph
* Changed: Optimized window opening positions
* Changed: Save sorting and selection arrow when refreshing file list
* Changed: Show a busy window, to let the user know something is happening when longer calculations are done
Expand Down
124 changes: 82 additions & 42 deletions LoxStatFileForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void DataGridView_MouseClick(object sender, DataGridViewCellMouseEventAr
ContextMenu m = new ContextMenu();
m.MenuItems.Add(new MenuItem("Calculate selected", modalCalcSelected_Click));
m.MenuItems.Add(new MenuItem("Calculate downwards", modalCalcFrom_Click));
m.MenuItems.Add(new MenuItem("Insert entry", modalInsertEntry_Click));
m.MenuItems.Add(new MenuItem("Insert entry above", modalInsertEntry_Click));
m.MenuItems.Add(new MenuItem("Delete selected entries", modalDeleteSelected_Click));
m.MenuItems.Add(new MenuItem("Fill entries", modalFillEntries_Click));

Expand Down Expand Up @@ -425,39 +425,64 @@ private void modalCalcFrom_Click(object sender, EventArgs e)
private void modalInsertEntry_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;

DataGridViewRow newRow = (DataGridViewRow)_dataGridView.Rows[0].Clone();
DataGridViewRow atInsert = _dataGridView.SelectedRows[0];
LoxStatDataPoint beforeDP;
LoxStatDataPoint beforeDP = null; // Initialize beforeDP to null
LoxStatDataPoint newDP;
LoxStatDataPoint atDP = _loxStatFile.DataPoints[atInsert.Index];
for(int x=atDP.Index; x <_loxStatFile.DataPoints.Count; x++)
{
_loxStatFile.DataPoints[x].Index += 1;
}

bool insertAllowed = false;

if (atInsert.Index == 0)
{
newDP = atDP.Clone();
newDP.Index = 0;
newDP.Values[0] = 0;
newDP.Timestamp = new DateTime(atDP.Timestamp.Year, atDP.Timestamp.Month, 1, 0, 0, 0);
_dataGridView.Rows.Insert(0, newRow);
_loxStatFile.DataPoints.Insert(0, newDP);
} else
insertAllowed = true; // Insertion at the beginning is always possible
}
else
{
beforeDP = _loxStatFile.DataPoints[atDP.Index - 2];
if (beforeDP.Timestamp < atDP.Timestamp.AddMinutes(-119))
// Ensure beforeDP is assigned before this point
beforeDP = _loxStatFile.DataPoints[atDP.Index - 1];
if (beforeDP != null && beforeDP.Timestamp < atDP.Timestamp.AddMinutes(-119)) // Check for null to satisfy the compiler
{
newDP = beforeDP.Clone();
newDP.Index = atInsert.Index;
newDP.Timestamp = beforeDP.Timestamp.AddHours(1);
_dataGridView.Rows.Insert(atInsert.Index - 1, newRow);
_loxStatFile.DataPoints.Insert(atInsert.Index - 1, newDP);
} else
insertAllowed = true; // Insertion is possible based on timestamp comparison
}
else
{
MessageBox.Show("There is no place to insert an entry!\n\nPlease check again, if there is a missing entry.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}

// Only adjust indices and insert new data point if insertion is confirmed
if (insertAllowed)
{
for (int x = atDP.Index; x < _loxStatFile.DataPoints.Count; x++)
{
_loxStatFile.DataPoints[x].Index += 1;
}

if (atInsert.Index == 0)
{
newDP = atDP.Clone();
newDP.Index = 0;
newDP.Values[0] = 0;
newDP.Timestamp = new DateTime(atDP.Timestamp.Year, atDP.Timestamp.Month, 1, 0, 0, 0);
_dataGridView.Rows.Insert(0, newRow);
_loxStatFile.DataPoints.Insert(0, newDP);
}
else
{
// Insertion logic for non-first row already validated above
if (beforeDP != null) // Additional null check for safety
{
newDP = beforeDP.Clone();
newDP.Index = atInsert.Index;
newDP.Timestamp = beforeDP.Timestamp.AddHours(1);
_dataGridView.Rows.Insert(atInsert.Index - 1, newRow);
_loxStatFile.DataPoints.Insert(atInsert.Index - 1, newDP);
}
}
}

Cursor = Cursors.Default;
}

Expand Down Expand Up @@ -501,28 +526,43 @@ private void modalDeleteSelected_Click(object sender, EventArgs e)
private void modalFillEntries_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
if (_dataGridView.SelectedRows[_dataGridView.SelectedRows.Count - 1].Index == 0) return;
DataGridViewRow newRow = (DataGridViewRow)_dataGridView.Rows[0].Clone();
DataGridViewRow atInsert = _dataGridView.SelectedRows[_dataGridView.SelectedRows.Count - 1];
LoxStatDataPoint beforeDP = _loxStatFile.DataPoints[atInsert.Index -1];
LoxStatDataPoint newDP;
LoxStatDataPoint atDP = _loxStatFile.DataPoints[atInsert.Index];
TimeSpan ts = atDP.Timestamp - beforeDP.Timestamp;
double InsertCount = ts.TotalHours -1;
for (int x = atDP.Index; x < _loxStatFile.DataPoints.Count; x++)
{
_loxStatFile.DataPoints[x].Index += (int)InsertCount;
}
double diff = ((double)atDP.Values[0] - (double)beforeDP.Values[0]) / (InsertCount +1);
for (int x = 1; x <= InsertCount; x++)

// Iterate through all but the last row to check gaps with the next row
for (int i = 0; i < _loxStatFile.DataPoints.Count - 1; i++)
{
newDP = beforeDP.Clone();
newDP.Index += x;
newDP.Timestamp = newDP.Timestamp.AddHours(x);
newDP.Values[0] += diff * x;
_loxStatFile.DataPoints.Insert(newDP.Index, newDP);
_dataGridView.Rows.Insert(newDP.Index, 1);
LoxStatDataPoint currentDP = _loxStatFile.DataPoints[i];
LoxStatDataPoint nextDP = _loxStatFile.DataPoints[i + 1];
TimeSpan ts = nextDP.Timestamp - currentDP.Timestamp;
double hoursDiff = ts.TotalHours;

// Check if there's more than 1 hour difference indicating a gap
if (hoursDiff > 1)
{
double insertCount = hoursDiff - 1;
double diff = (nextDP.Values[0] - currentDP.Values[0]) / (insertCount + 1);

// Adjust indices for subsequent data points
for (int x = nextDP.Index; x < _loxStatFile.DataPoints.Count; x++)
{
_loxStatFile.DataPoints[x].Index += (int)insertCount;
}

// Insert missing data points
for (int x = 1; x <= insertCount; x++)
{
LoxStatDataPoint newDP = currentDP.Clone();
newDP.Index += x;
newDP.Timestamp = newDP.Timestamp.AddHours(x);
newDP.Values[0] += diff * x;
_loxStatFile.DataPoints.Insert(newDP.Index, newDP);
_dataGridView.Rows.Insert(newDP.Index, 1);
}

// Adjust loop counter to skip newly inserted points
i += (int)insertCount;
}
}

_dataGridView.Refresh();
Cursor = Cursors.Default;
}
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Open tasks

- Add possibility to insert a date and change the month/date for all entries
- Read Loxone XML file to be able to show statistics name before downloading statistic files

0 comments on commit 130184c

Please sign in to comment.