* Actually draw the graph. * @param r the rectangle of the data field of the graph */
| 367 | * @param r the rectangle of the data field of the graph |
| 368 | */ |
| 369 | void DrawGraph(Rect r) const |
| 370 | { |
| 371 | uint x, y; ///< Reused whenever x and y coordinates are needed. |
| 372 | ValuesInterval interval; ///< Interval that contains all of the graph data. |
| 373 | int x_axis_offset; ///< Distance from the top of the graph to the x axis. |
| 374 | |
| 375 | /* the colours and cost array of GraphDrawer must accommodate |
| 376 | * both values for cargo and companies. So if any are higher, quit */ |
| 377 | static_assert(GRAPH_MAX_DATASETS >= (int)NUM_CARGO && GRAPH_MAX_DATASETS >= (int)MAX_COMPANIES); |
| 378 | assert(this->num_vert_lines > 0); |
| 379 | |
| 380 | bool rtl = _current_text_dir == TD_RTL; |
| 381 | |
| 382 | /* Rect r will be adjusted to contain just the graph, with labels being |
| 383 | * placed outside the area. */ |
| 384 | r.top += ScaleGUITrad(5) + GetCharacterHeight(FS_SMALL) / 2; |
| 385 | r.bottom -= (this->draw_dates ? 2 : 1) * GetCharacterHeight(FS_SMALL) + ScaleGUITrad(4); |
| 386 | r.left += ScaleGUITrad(rtl ? 5 : 9); |
| 387 | r.right -= ScaleGUITrad(rtl ? 9 : 5); |
| 388 | |
| 389 | /* Initial number of horizontal lines. */ |
| 390 | int num_hori_lines = 160 / ScaleGUITrad(MIN_GRID_PIXEL_SIZE); |
| 391 | /* For the rest of the height, the number of horizontal lines will increase more slowly. */ |
| 392 | int resize = (r.bottom - r.top - 160) / (2 * ScaleGUITrad(MIN_GRID_PIXEL_SIZE)); |
| 393 | if (resize > 0) num_hori_lines += resize; |
| 394 | |
| 395 | interval = GetValuesInterval(num_hori_lines); |
| 396 | |
| 397 | int label_width = GetYLabelWidth(interval, num_hori_lines); |
| 398 | |
| 399 | if (rtl) { |
| 400 | r.right -= label_width; |
| 401 | } else { |
| 402 | r.left += label_width; |
| 403 | } |
| 404 | |
| 405 | int x_sep = (r.right - r.left) / this->num_vert_lines; |
| 406 | int y_sep = (r.bottom - r.top) / num_hori_lines; |
| 407 | |
| 408 | /* Redetermine right and bottom edge of graph to fit with the integer |
| 409 | * separation values. */ |
| 410 | if (rtl) { |
| 411 | r.left = r.right - x_sep * this->num_vert_lines; |
| 412 | } else { |
| 413 | r.right = r.left + x_sep * this->num_vert_lines; |
| 414 | } |
| 415 | r.bottom = r.top + y_sep * num_hori_lines; |
| 416 | |
| 417 | OverflowSafeInt64 interval_size = interval.highest + abs(interval.lowest); |
| 418 | /* Where to draw the X axis. Use floating point to avoid overflowing and results of zero. */ |
| 419 | x_axis_offset = (int)((r.bottom - r.top) * (double)interval.highest / (double)interval_size); |
| 420 | |
| 421 | /* Draw the background of the graph itself. */ |
| 422 | GfxFillRect(r.left, r.top, r.right, r.bottom, GRAPH_BASE_COLOUR); |
| 423 | |
| 424 | /* Draw the grid lines. */ |
| 425 | int gridline_width = WidgetDimensions::scaled.bevel.top; |
| 426 | PixelColour grid_colour = GRAPH_GRID_COLOUR; |
no test coverage detected