1218{
1219 wxAutoBufferedPaintDC dc(this);
1220
1221 dc.SetBackground(*wxWHITE_BRUSH);
1222 dc.Clear();
1223
1224 if (!m_grid)
1225 return;
1226
1227 std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
1228
1229 if (!gc)
1230 return;
1231
1232 gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
1233
1234 const wxSize size = GetClientSize();
1235
1236 const int left = 60;
1237 const int right = 20;
1238 const int top = 20;
1239 const int bottom = 50;
1240
1241 const int graphWidth = size.x - left - right;
1242 const int graphHeight = size.y - top - bottom;
1243
1244 if (graphWidth <= 0 || graphHeight <= 0)
1245 return;
1246
1247 struct Point
1248 {
1249 double current;
1250 double time;
1251 };
1252
1253 std::vector<Point> data;
1254
1255 double minCurrent = std::numeric_limits<double>::max();
1256 double maxCurrent = std::numeric_limits<double>::lowest();
1257 double minTime = std::numeric_limits<double>::max();
1258 double maxTime = std::numeric_limits<double>::lowest();
1259
1260 for (int row = 0; row < m_grid->GetNumberRows(); ++row)
1261 {
1262 double current = 0.0;
1263 double time = 0.0;
1264
1265 if (!GetCellDouble(row, 0, current) || !GetCellDouble(row, 1, time))
1266 continue;
1267
1268 if (current <= 0.0 || time <= 0.0)
1269 continue;
1270
1271 data.push_back({ current, time });
1272
1273 minCurrent = std::min(minCurrent, current);
1274 maxCurrent = std::max(maxCurrent, current);
1275 minTime = std::min(minTime, time);
1276 maxTime = std::max(maxTime, time);
1277 }
1278
1279 if (data.size() < 2)
1280 return;
1281
1282
1283 double logMinCurrent = std::floor(std::log10(minCurrent));
1284 double logMaxCurrent = std::ceil(std::log10(maxCurrent));
1285 double logMinTime = std::floor(std::log10(minTime));
1286 double logMaxTime = std::ceil(std::log10(maxTime));
1287
1288 if (logMinCurrent == logMaxCurrent)
1289 {
1290 --logMinCurrent;
1291 ++logMaxCurrent;
1292 }
1293
1294 if (logMinTime == logMaxTime)
1295 {
1296 --logMinTime;
1297 ++logMaxTime;
1298 }
1299
1300 auto mapX = [&](double current) {
1301 return left + (std::log10(current) - logMinCurrent) / (logMaxCurrent - logMinCurrent) * graphWidth;
1302 };
1303
1304 auto mapY = [&](double time) {
1305 return size.y - bottom - (std::log10(time) - logMinTime) / (logMaxTime - logMinTime) * graphHeight;
1306 };
1307
1308 const int firstCurrentDecade = static_cast<int>(logMinCurrent);
1309 const int lastCurrentDecade = static_cast<int>(logMaxCurrent);
1310
1311 const int firstTimeDecade = static_cast<int>(logMinTime);
1312 const int lastTimeDecade = static_cast<int>(logMaxTime);
1313
1314
1315 gc->SetPen(wxPen(wxColour(230, 230, 230), 1));
1316
1317 for (int exponent = firstCurrentDecade; exponent <= lastCurrentDecade; ++exponent)
1318 {
1319 const double decade = std::pow(10.0, exponent);
1320
1321 for (int i = 2; i <= 9; ++i)
1322 {
1323 const double current = i * decade;
1324 const double x = mapX(current);
1325
1326 if (x >= left && x <= size.x - right)
1327 gc->StrokeLine(x, top, x, size.y - bottom);
1328 }
1329 }
1330
1331
1332 for (int exponent = firstTimeDecade; exponent <= lastTimeDecade; ++exponent)
1333 {
1334 const double decade = std::pow(10.0, exponent);
1335
1336 for (int i = 2; i <= 9; ++i)
1337 {
1338 const double time = i * decade;
1339 const double y = mapY(time);
1340
1341 if (y >= top && y <= size.y - bottom)
1342 gc->StrokeLine(left, y, size.x - right, y);
1343 }
1344 }
1345
1346
1347 gc->SetPen(wxPen(wxColour(190, 190, 190), 1));
1348
1349 for (int exponent = firstCurrentDecade; exponent <= lastCurrentDecade; ++exponent)
1350 {
1351 const double current = std::pow(10.0, exponent);
1352 const double x = mapX(current);
1353
1354 if (x >= left && x <= size.x - right)
1355 gc->StrokeLine(x, top, x, size.y - bottom);
1356 }
1357
1358
1359 for (int exponent = firstTimeDecade; exponent <= lastTimeDecade; ++exponent)
1360 {
1361 const double time = std::pow(10.0, exponent);
1362 const double y = mapY(time);
1363
1364 if (y >= top && y <= size.y - bottom)
1365 gc->StrokeLine(left, y, size.x - right, y);
1366 }
1367
1368
1369 gc->SetPen(wxPen(*wxBLACK, 1.5));
1370 gc->StrokeLine(left, top, left, size.y - bottom);
1371 gc->StrokeLine(left, size.y - bottom, size.x - right, size.y - bottom);
1372
1373
1374 gc->SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT), *wxBLACK);
1375
1376 for (int exponent = firstCurrentDecade; exponent <= lastCurrentDecade; ++exponent)
1377 {
1378 const double current = std::pow(10.0, exponent);
1379 const double x = mapX(current);
1380
1381 if (x < left || x > size.x - right)
1382 continue;
1383
1384 const wxString label = wxString::Format("1e%d", exponent);
1385
1386 double textWidth;
1387 double textHeight;
1388 gc->GetTextExtent(label, &textWidth, &textHeight);
1389
1390 gc->DrawText(label, x - textWidth / 2.0, size.y - bottom + 5);
1391 }
1392
1393
1394 for (int exponent = firstTimeDecade; exponent <= lastTimeDecade; ++exponent)
1395 {
1396 const double time = std::pow(10.0, exponent);
1397 const double y = mapY(time);
1398
1399 if (y < top || y > size.y - bottom)
1400 continue;
1401
1402 const wxString label = wxString::Format("1e%d", exponent);
1403
1404 double textWidth;
1405 double textHeight;
1406 gc->GetTextExtent(label, &textWidth, &textHeight);
1407
1408 gc->DrawText(label, left - textWidth - 5, y - textHeight / 2.0);
1409 }
1410
1411
1412 wxGraphicsPath path = gc->CreatePath();
1413
1414 for (size_t i = 0; i < data.size(); ++i)
1415 {
1416 const double x = mapX(data[i].current);
1417 const double y = mapY(data[i].time);
1418
1419 if (i == 0)
1420 path.MoveToPoint(x, y);
1421 else
1422 path.AddLineToPoint(x, y);
1423 }
1424
1425 gc->SetPen(wxPen(wxColour(40, 100, 200), 2.0));
1426 gc->StrokePath(path);
1427
1428
1429 const wxString xLabel = _("Current (A)");
1430
1431 double xLabelWidth;
1432 double xLabelHeight;
1433 gc->GetTextExtent(xLabel, &xLabelWidth, &xLabelHeight);
1434
1435 gc->DrawText(xLabel, left + (graphWidth - xLabelWidth) / 2.0, size.y - 20);
1436
1437
1438 const wxString yLabel = _("Time (s)");
1439
1440 double yLabelWidth;
1441 double yLabelHeight;
1442 gc->GetTextExtent(yLabel, &yLabelWidth, &yLabelHeight);
1443
1444 gc->PushState();
1445 gc->Translate(15, top + graphHeight / 2.0);
1446 gc->Rotate(-M_PI / 2.0);
1447 gc->DrawText(yLabel, -yLabelWidth / 2.0, -yLabelHeight / 2.0);
1448 gc->PopState();
1449}