1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
|
{
/***************************************************************************
TASeries.pas
------------
Component Library Standard Graph Series
***************************************************************************/
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Authors: LuÃs Rodrigues, Philippe Martinole, Alexander Klenin
}
unit TASeries;
{$H+}
interface
uses
Classes, Graphics, Types,
TAChartUtils, TADrawUtils, TACustomSeries, TALegend, TARadialSeries, TATypes;
const
DEF_BAR_WIDTH_PERCENT = 70;
type
EBarError = class(EChartError);
TBarWidthStyle = (bwPercent, bwPercentMin);
TBarSeries = class;
TBeforeDrawBarEvent = procedure (
ASender: TBarSeries; ACanvas: TCanvas; const ARect: TRect;
APointIndex, AStackIndex: Integer; var ADoDefaultDrawing: Boolean
) of object;
{ TBarSeries }
TBarSeries = class(TBasicPointSeries)
private
FBarBrush: TBrush;
FBarOffsetPercent: Integer;
FBarPen: TPen;
FBarWidthPercent: Integer;
FBarWidthStyle: TBarWidthStyle;
FOnBeforeDrawBar: TBeforeDrawBarEvent;
FZeroLevel: Double;
function IsZeroLevelStored: boolean;
procedure SetBarBrush(Value: TBrush);
procedure SetBarOffsetPercent(AValue: Integer);
procedure SetBarPen(Value: TPen);
procedure SetBarWidthPercent(Value: Integer);
procedure SetBarWidthStyle(AValue: TBarWidthStyle);
procedure SetOnBeforeDrawBar(AValue: TBeforeDrawBarEvent);
procedure SetSeriesColor(AValue: TColor);
procedure SetZeroLevel(AValue: Double);
strict protected
function GetLabelDataPoint(AIndex, AYIndex: Integer): TDoublePoint; override;
function ToolTargetDistance(const AParams: TNearestPointParams;
AGraphPt: TDoublePoint; APointIdx, AXIdx, AYIdx: Integer): Integer; override;
protected
procedure BarOffsetWidth(
AX: Double; AIndex: Integer; out AOffset, AWidth: Double);
procedure GetLegendItems(AItems: TChartLegendItems); override;
function GetSeriesColor: TColor; override;
function GetZeroLevel: Double; override;
public
procedure Assign(ASource: TPersistent); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
public
function GetBarWidth(AIndex: Integer): Integer;
procedure Draw(ADrawer: IChartDrawer); override;
function Extent: TDoubleRect; override;
function GetNearestPoint(const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean; override;
published
property AxisIndexX;
property AxisIndexY;
property BarBrush: TBrush read FBarBrush write SetBarBrush;
property BarOffsetPercent: Integer
read FBarOffsetPercent write SetBarOffsetPercent default 0;
property BarPen: TPen read FBarPen write SetBarPen;
property BarWidthPercent: Integer
read FBarWidthPercent write SetBarWidthPercent default DEF_BAR_WIDTH_PERCENT;
property BarWidthStyle: TBarWidthStyle
read FBarWidthStyle write SetBarWidthStyle default bwPercent;
property Depth;
property MarkPositionCentered;
property MarkPositions;
property SeriesColor: TColor
read GetSeriesColor write SetSeriesColor stored false default clRed;
property Source;
property Stacked default true;
property Styles;
property ToolTargets default [nptPoint, nptYList, nptCustom];
property UseReticule; deprecated 'Use DatapointCrosshairTool instead';
property ZeroLevel: Double
read FZeroLevel write SetZeroLevel stored IsZeroLevelStored;
published
property OnBeforeDrawBar: TBeforeDrawBarEvent
read FOnBeforeDrawBar write SetOnBeforeDrawBar;
end;
{ TPieSeries }
TPieSeries = class(TCustomPieSeries)
public
property Radius;
published
property EdgePen;
property Depth;
property Exploded;
property FixedRadius;
property MarkDistancePercent;
property MarkPositions;
property RotateLabels;
property Source;
end;
TConnectType = (ctLine, ctStepXY, ctStepYX);
{ TAreaSeries }
TAreaSeries = class(TBasicPointSeries)
private
FAreaBrush: TBrush;
FAreaContourPen: TPen;
FAreaLinesPen: TPen;
FBanded: Boolean;
FConnectType: TConnectType;
FUseZeroLevel: Boolean;
FZeroLevel: Double;
function IsZeroLevelStored: boolean;
procedure SetAreaBrush(AValue: TBrush);
procedure SetAreaContourPen(AValue: TPen);
procedure SetAreaLinesPen(AValue: TPen);
procedure SetBanded(AValue: Boolean);
procedure SetConnectType(AValue: TConnectType);
procedure SetSeriesColor(AValue: TColor);
procedure SetUseZeroLevel(AValue: Boolean);
procedure SetZeroLevel(AValue: Double);
protected
procedure GetLegendItems(AItems: TChartLegendItems); override;
function GetSeriesColor: TColor; override;
function GetZeroLevel: Double; override;
public
procedure Assign(ASource: TPersistent); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Draw(ADrawer: IChartDrawer); override;
function Extent: TDoubleRect; override;
published
property AxisIndexX;
property AxisIndexY;
published
property AreaBrush: TBrush read FAreaBrush write SetAreaBrush;
property AreaContourPen: TPen read FAreaContourPen write SetAreaContourPen;
property AreaLinesPen: TPen read FAreaLinesPen write SetAreaLinesPen;
property Banded: Boolean read FBanded write SetBanded default false;
property ConnectType: TConnectType
read FConnectType write SetConnectType default ctLine;
property Depth;
property MarkPositionCentered;
property MarkPositions;
property SeriesColor: TColor
read GetSeriesColor write SetSeriesColor stored false default clWhite;
property Source;
property Stacked default true;
property Styles;
property ToolTargets;
property UseReticule; deprecated 'Use DatapointCrosshairTool instead';
property UseZeroLevel: Boolean
read FUseZeroLevel write SetUseZeroLevel default false;
property ZeroLevel: Double
read FZeroLevel write SetZeroLevel stored IsZeroLevelStored;
end;
TSeriesPointerDrawEvent = procedure (
ASender: TChartSeries; ACanvas: TCanvas; AIndex: Integer;
ACenter: TPoint) of object;
TLineType = (ltNone, ltFromPrevious, ltFromOrigin, ltStepXY, ltStepYX);
TColorEachMode = (ceNone, cePoint, ceLineBefore, ceLineAfter,
cePointAndLineBefore, cePointAndLineAfter);
{ TLineSeries }
TLineSeries = class(TBasicPointSeries)
private
FLinePen: TPen;
FLineType: TLineType;
FOnDrawPointer: TSeriesPointerDrawEvent;
FColorEach: TColorEachMode;
procedure DrawSingleLineInStack(ADrawer: IChartDrawer; AIndex: Integer);
function GetShowLines: Boolean;
function GetShowPoints: Boolean;
procedure SetColorEach(AValue: TColorEachMode);
procedure SetLinePen(AValue: TPen);
procedure SetLineType(AValue: TLineType);
procedure SetSeriesColor(AValue: TColor);
procedure SetShowLines(Value: Boolean);
procedure SetShowPoints(AValue: Boolean);
protected
procedure AfterDrawPointer(
ADrawer: IChartDrawer; AIndex: Integer; const APos: TPoint); override;
procedure GetLegendItems(AItems: TChartLegendItems); override;
function GetSeriesColor: TColor; override;
public
procedure Assign(ASource: TPersistent); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Draw(ADrawer: IChartDrawer); override;
published
property AxisIndexX;
property AxisIndexY;
property ColorEach: TColorEachMode
read FColorEach write SetColorEach default cePoint;
property Depth;
property LinePen: TPen read FLinePen write SetLinePen;
property LineType: TLineType
read FLineType write SetLineType default ltFromPrevious;
property MarkPositions;
property Pointer;
property SeriesColor: TColor
read GetSeriesColor write SetSeriesColor stored false default clBlack;
property ShowLines: Boolean
read GetShowLines write SetShowLines stored false default true;
property ShowPoints: Boolean
read GetShowPoints write SetShowPoints default false;
property Stacked default false;
property Source;
property Styles;
property ToolTargets;
property UseReticule default true; deprecated 'Use DatapointCrosshairTool instead';
property XErrorBars;
property YErrorBars;
// Events
property OnCustomDrawPointer;
property OnGetPointerStyle;
end;
// Scatter plot displaying a single pixel per data point.
// Optimized to work efficiently for millions of points.
// See http://en.wikipedia.org/wiki/Manhattan_plot
TManhattanSeries = class(TBasicPointSeries)
private
FSeriesColor: TColor;
procedure SetSeriesColor(AValue: TColor);
protected
procedure GetLegendItems(AItems: TChartLegendItems); override;
public
procedure Assign(ASource: TPersistent); override;
procedure Draw(ADrawer: IChartDrawer); override;
published
property AxisIndexX;
property AxisIndexY;
property SeriesColor: TColor
read FSeriesColor write SetSeriesColor default clBlack;
property Source;
property UseReticule; deprecated 'Use DatapointCrosshairTool instead';
end;
TLineStyle = (lsVertical, lsHorizontal);
{ TConstantLine }
TConstantLine = class(TCustomChartSeries)
strict private
FArrow: TChartArrow;
FLineStyle: TLineStyle;
FPen: TPen;
FPosition: Double; // Graph coordinate of line
FUseBounds: Boolean;
function GetAxisIndex: TChartAxisIndex;
function GetSeriesColor: TColor;
procedure SavePosToCoord(var APoint: TDoublePoint);
procedure SetArrow(AValue: TChartArrow);
procedure SetAxisIndex(AValue: TChartAxisIndex);
procedure SetLineStyle(AValue: TLineStyle);
procedure SetPen(AValue: TPen);
procedure SetPosition(AValue: Double);
procedure SetSeriesColor(AValue: TColor);
procedure SetUseBounds(AValue: Boolean);
protected
procedure AfterAdd; override;
procedure GetBounds(var ABounds: TDoubleRect); override;
procedure GetLegendItems(AItems: TChartLegendItems); override;
public
procedure Assign(ASource: TPersistent); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Draw(ADrawer: IChartDrawer); override;
function GetNearestPoint(
const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean; override;
function IsEmpty: Boolean; override;
procedure MovePoint(var AIndex: Integer; const ANewPos: TDoublePoint); override;
procedure UpdateBiDiMode; override;
published
property Active default true;
property Arrow: TChartArrow read FArrow write SetArrow;
property AxisIndex: TChartAxisIndex
read GetAxisIndex write SetAxisIndex default DEF_AXIS_INDEX;
property AxisIndexX stored false; deprecated 'Use "AxisIndex"';
property LineStyle: TLineStyle
read FLineStyle write SetLineStyle default lsHorizontal;
property Pen: TPen read FPen write SetPen;
property Position: Double read FPosition write SetPosition;
property SeriesColor: TColor
read GetSeriesColor write SetSeriesColor stored false default clBlack;
property ShowInLegend;
property Title;
property UseBounds: Boolean read FUseBounds write SetUseBounds default true;
property ZPosition;
end;
TSeriesDrawEvent = procedure (ACanvas: TCanvas; const ARect: TRect) of object;
TSeriesGetBoundsEvent = procedure (var ABounds: TDoubleRect) of object;
{ TUserDrawnSeries }
TUserDrawnSeries = class(TCustomChartSeries)
private
FOnDraw: TSeriesDrawEvent;
FOnGetBounds: TSeriesGetBoundsEvent;
procedure SetOnDraw(AValue: TSeriesDrawEvent);
procedure SetOnGetBounds(AValue: TSeriesGetBoundsEvent);
protected
procedure GetBounds(var ABounds: TDoubleRect); override;
procedure GetLegendItems(AItems: TChartLegendItems); override;
public
procedure Assign(ASource: TPersistent); override;
procedure Draw(ADrawer: IChartDrawer); override;
function IsEmpty: Boolean; override;
published
property Active default true;
property ZPosition;
published
property OnDraw: TSeriesDrawEvent read FOnDraw write SetOnDraw;
property OnGetBounds: TSeriesGetBoundsEvent
read FOnGetBounds write SetOnGetBounds;
end;
implementation
uses
GraphMath, GraphType, IntfGraphics, LResources, Math, PropEdits, SysUtils,
TAChartStrConsts, TADrawerCanvas, TAGeometry, TAGraph, TAMath, TAStyles;
{ TLineSeries }
procedure TLineSeries.AfterDrawPointer(
ADrawer: IChartDrawer; AIndex: Integer; const APos: TPoint);
var
ic: IChartTCanvasDrawer;
begin
if Supports(ADrawer, IChartTCanvasDrawer, ic) and Assigned(FOnDrawPointer) then
FOnDrawPointer(Self, ic.Canvas, AIndex, APos);
end;
procedure TLineSeries.Assign(ASource: TPersistent);
begin
if ASource is TLineSeries then
with TLineSeries(ASource) do begin
Self.LinePen := FLinePen;
Self.FLineType := FLineType;
Self.FOnDrawPointer := FOnDrawPointer;
Self.FColorEach := FColorEach;
end;
inherited Assign(ASource);
end;
constructor TLineSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FColorEach := cePoint;
FLinePen := TPen.Create;
FLinePen.OnChange := @StyleChanged;
FPointer := TSeriesPointer.Create(FChart);
SetPropDefaults(Self, ['LineType', 'UseReticule']);
end;
destructor TLineSeries.Destroy;
begin
FreeAndNil(FLinePen);
inherited;
end;
procedure TLineSeries.Draw(ADrawer: IChartDrawer);
var
ext: TDoubleRect;
i: Integer;
begin
with Extent do begin
ext.a := AxisToGraph(a);
ext.b := AxisToGraph(b);
end;
NormalizeRect(ext);
if LineType = ltFromOrigin then
ExpandRect(ext, AxisToGraph(ZeroDoublePoint));
// Do not draw anything if the series extent does not intersect CurrentExtent.
if not RectIntersectsRect(ext, ParentChart.CurrentExtent) then exit;
PrepareGraphPoints(ext, LineType <> ltFromOrigin);
DrawSingleLineInStack(ADrawer, 0);
for i := 0 to Source.YCount - 2 do begin
if Source.IsYErrorIndex(i+1) then Continue;
UpdateGraphPoints(i, FStacked);
DrawSingleLineInStack(ADrawer, i + 1);
end;
end;
procedure TLineSeries.DrawSingleLineInStack(
ADrawer: IChartDrawer; AIndex: Integer);
var
points: array of TPoint;
pointCount: Integer = 0;
breaks: TIntegerDynArray;
breakCount: Integer = 0;
// Drawing long polylines with wide pen is very inefficient on Windows and GTK.
// On Windows it is so bad that trying to draw polyline with 50000 points
// will cause hard freeze of entire OS. (!)
// Also, Windows refuses to draw any polyline with number of points
// above approximately one million.
// So, split long polylines into segments.
function PolylineIsTooLong: Boolean; inline;
// There is a trade-off between the call overhead for short serment and
// the above-mentioned inefficiency for long ones.
// First value was selected by some experiments as "optimal enough" for
// both affected platforms.
{$IF defined(LCLWIN32) or defined(LCLGTK2)}
const
MAX_LENGTH: array [Boolean] of Integer = (50000, 1000000);
{$ENDIF}
begin
{$IF defined(LCLWIN32)}
Result :=
(breakCount > 0) and
(pointCount - breaks[breakCount - 1] > MAX_LENGTH[LinePen.Width = 1]);
{$ELSEIF defined(LCLGTK2)}
Result :=
(LinePen.Width > 1) and (breakCount > 0) and
(pointCount - breaks[breakCount - 1] > MAX_LENGTH[false]);
{$ELSE}
Result := false;
{$ENDIF}
end;
procedure PushPoint(const APoint: TPoint); inline;
begin
if pointCount > High(points) then
SetLength(points, Length(points) * 2);
points[pointCount] := APoint;
pointCount += 1;
end;
procedure CacheLine(AA, AB: TDoublePoint);
var
ai, bi: TPoint;
begin
// This is not an optimization, but a safety check to avoid
// integer overflow with extreme zoom-ins.
if not LineIntersectsRect(AA, AB, ParentChart.CurrentExtent) then exit;
ai := ParentChart.GraphToImage(AA);
bi := ParentChart.GraphToImage(AB);
if ai = bi then exit;
if
(pointCount = 0) or (points[pointCount - 1] <> ai) or PolylineIsTooLong
then begin
breaks[breakCount] := pointCount;
breakCount += 1;
PushPoint(ai);
end;
PushPoint(bi);
end;
procedure DrawStep(const AP1, AP2: TDoublePoint);
var
m: TDoublePoint;
begin
if (LineType = ltStepXY) xor IsRotated then
m := DoublePoint(AP2.X, AP1.Y)
else
m := DoublePoint(AP1.X, AP2.Y);
CacheLine(AP1, m);
CacheLine(m, AP2);
end;
procedure DrawDefaultLines;
var
i, j: Integer;
p, pPrev: TDoublePoint;
pNan, pPrevNan: Boolean;
scaled_depth: Integer;
begin
if LineType = ltNone then exit;
// For extremely long series (10000 points or more), the Canvas.Line call
// becomes a bottleneck. So represent a serie as a sequence of polylines.
// This achieves approximately 3x speedup for the typical case.
SetLength(points, Length(FGraphPoints) + 1);
SetLength(breaks, Length(FGraphPoints) + 1);
pPrevNan := true;
// Actually needed only for ltFromOrigin, but moved to silence a warning.
pPrev := AxisToGraph(ZeroDoublePoint);
case LineType of
ltFromPrevious:
for p in FGraphPoints do begin
pNan := IsNan(p);
if not (pNan or pPrevNan) then
CacheLine(pPrev, p);
pPrev := p;
pPrevNan := pNan;
end;
ltFromOrigin:
for p in FGraphPoints do
if not IsNan(p) then
CacheLine(pPrev, p);
ltStepXY, ltStepYX:
for p in FGraphPoints do begin
pNan := IsNan(p);
if not (pNan or pPrevNan) then
DrawStep(pPrev, p);
pPrev := p;
pPrevNan := pNan;
end;
end;
breaks[breakCount] := pointCount;
breakCount += 1;
SetLength(points, pointCount);
SetLength(breaks, breakCount);
ADrawer.SetBrushParams(bsClear, clTAColor);
ADrawer.Pen := LinePen;
if Styles <> nil then
Styles.Apply(ADrawer, AIndex);
if Depth = 0 then
for i := 0 to High(breaks) - 1 do
ADrawer.Polyline(points, breaks[i], breaks[i + 1] - breaks[i])
else begin
if Styles = nil then begin
ADrawer.SetBrushParams(bsSolid, LinePen.Color);
ADrawer.SetPenParams(LinePen.Style, clBlack);
end;
scaled_depth := ADrawer.Scale(Depth);
for i := 0 to High(breaks) - 1 do
for j := breaks[i] to breaks[i + 1] - 2 do
ADrawer.DrawLineDepth(points[j], points[j + 1], scaled_depth);
end;
end;
function GetPtColor(AIndex: Integer): TColor;
begin
Result := Source[AIndex]^.Color;
if Result = clTAColor then Result := SeriesColor;
end;
procedure DrawColoredLines;
var
i, n: Integer;
gp: TDoublepoint;
col, col1, col2: TColor;
imgPt1, imgPt2: TPoint;
pt, origin: TPoint;
hasBreak: Boolean;
begin
if LineType = ltNone then exit;
n := Length(FGraphPoints);
// Find first point
i := 0;
while (i < n) do begin
gp := FGraphPoints[i];
if not IsNaN(gp) then break;
inc(i);
end;
if i = n then
exit;
ADrawer.Pen := LinePen;
imgPt1 := ParentChart.GraphToImage(gp);
col1 := GetPtColor(i);
// First line for line type ltFromOrigin
if LineType = ltFromOrigin then begin
origin := ParentChart.GraphToImage(AxisToGraph(ZeroDoublePoint));
ADrawer.SetPenParams(FLinePen.Style, col1);
ADrawer.Line(origin, imgPt1);
end;
// iterate through all other points
hasBreak := false;
while (i < n) do begin
gp := FGraphPoints[i];
if IsNaN(gp) then begin
hasBreak := true;
end else begin
if hasBreak then begin
imgPt1 := ParentChart.GraphToImage(gp);
hasBreak := false;
end;
imgPt2 := ParentChart.GraphToImage(gp);
col2 := GetPtColor(i);
if imgPt1 <> imgPt2 then begin
case FColorEach of
ceLineBefore, cePointAndLineBefore: col := col2;
ceLineAfter, cePointAndLineAfter: col := col1;
else raise Exception.Create('TLineSeries: ColorEach error');
end;
ADrawer.SetPenParams(FLinePen.Style, col);
case LineType of
ltFromPrevious:
ADrawer.Line(imgPt1, imgPt2);
ltStepXY:
begin
pt := Point(imgPt2.x, imgPt1.Y);
ADrawer.Line(imgPt1, pt);
ADrawer.Line(pt, imgPt2);
end;
ltStepYX:
begin
pt := Point(imgPt1.x, imgPt2.Y);
ADrawer.Line(imgPt1, pt);
ADrawer.Line(pt, imgPt2);
end;
ltFromOrigin:
ADrawer.Line(origin, imgPt2);
end;
end;
imgPt1 := imgPt2;
col1 := col2;
end;
inc(i);
end;
end;
begin
case FColorEach of
ceNone, cePoint:
DrawDefaultLines;
else
DrawColoredLines;
end;
DrawErrorBars(ADrawer);
DrawLabels(ADrawer);
if ShowPoints then
DrawPointers(ADrawer, AIndex, FColorEach in [cePoint, cePointAndLineBefore, cePointAndLineAfter]);
end;
procedure TLineSeries.GetLegendItems(AItems: TChartLegendItems);
var
lb: TBrush;
lp: TPen;
p: TSeriesPointer;
i: Integer;
li: TLegendItemLinePointer;
s: TChartStyle;
begin
if LineType = ltNone then
lp := nil
else
lp := LinePen;
if ShowPoints then
p := Pointer
else
p := nil;
case Legend.Multiplicity of
lmSingle:
AItems.Add(TLegendItemLinePointer.Create(lp, p, LegendTextSingle));
lmPoint:
for i := 0 to Count - 1 do begin
li := TLegendItemLinePointer.Create(lp, p, LegendTextPoint(i));
li.Color := GetColor(i);
AItems.Add(li);
end;
lmStyle:
if Styles <> nil then begin
if Assigned(p) then lb := p.Brush else lb := nil;
for s in Styles.Styles do
AItems.Add(TLegendItemLinePointer.CreateWithBrush(
IfThen((lp <> nil) and s.UsePen, s.Pen, lp) as TPen,
IfThen(s.UseBrush, s.Brush, lb) as TBrush,
p,
LegendTextStyle(s)
));
end;
end;
end;
function TLineSeries.GetSeriesColor: TColor;
begin
Result := FLinePen.Color;
end;
function TLineSeries.GetShowLines: Boolean;
begin
Result := FLineType <> ltNone;
end;
function TLineSeries.GetShowPoints: Boolean;
begin
Result := FPointer.Visible;
end;
procedure TLineSeries.SetColorEach(AValue: TColorEachMode);
begin
if FColorEach = AValue then exit;
FColorEach := AValue;
UpdateParentChart;
end;
procedure TLineSeries.SetLinePen(AValue: TPen);
begin
FLinePen.Assign(AValue);
end;
procedure TLineSeries.SetLineType(AValue: TLineType);
begin
if FLineType = AValue then exit;
FLineType := AValue;
UpdateParentChart;
end;
procedure TLineSeries.SetSeriesColor(AValue: TColor);
begin
FLinePen.Color := AValue;
end;
procedure TLineSeries.SetShowLines(Value: Boolean);
begin
if ShowLines = Value then exit;
if Value then
FLineType := ltFromPrevious
else
FLineType := ltNone;
UpdateParentChart;
end;
procedure TLineSeries.SetShowPoints(AValue: Boolean);
begin
if ShowPoints = AValue then exit;
FPointer.Visible := AValue;
UpdateParentChart;
end;
{ TManhattanSeries }
procedure TManhattanSeries.Assign(ASource: TPersistent);
begin
if ASource is TManhattanSeries then
with TManhattanSeries(ASource) do
Self.FSeriesColor := SeriesColor;
inherited Assign(ASource);
end;
procedure TManhattanSeries.Draw(ADrawer: IChartDrawer);
var
img: TLazIntfImage;
topLeft, pt: TPoint;
i, cnt: Integer;
ext: TDoubleRect;
rawImage: TRawImage;
r: TRect;
procedure PutPixel(const APoint: TPoint; AColor: TChartColor); inline;
begin
PCardinal(rawImage.Data)[APoint.Y * r.Right + APoint.X] :=
Cardinal(ColorDef(AColor, SeriesColor)) or $FF000000; // Opacity.
cnt += 1;
end;
begin
with Extent do begin
ext.a := AxisToGraph(a);
ext.b := AxisToGraph(b);
end;
NormalizeRect(ext);
if not RectIntersectsRect(ext, ParentChart.CurrentExtent) then exit;
// Do not cache graph points to reduce memory overhead.
FindExtentInterval(ext, true);
topLeft := ParentChart.ClipRect.TopLeft;
r := BoundsSize(0, 0, ParentChart.ClipRect.BottomRight - topLeft);
cnt := 0;
img := CreateLazIntfImage(rawImage, r.BottomRight);
try
// AxisToGraph is slow, so split loop to optimize non-transformed case.
if (AxisIndexX = -1) and (AxisIndexY = -1) then
for i := FLoBound to FUpBound do
with Source[i]^ do begin
pt := ParentChart.GraphToImage(Point) - topLeft;
if PtInRect(r, pt) then
PutPixel(pt, Color);
end
else
for i := FLoBound to FUpBound do
with Source[i]^ do begin
pt := ParentChart.GraphToImage(AxisToGraph(Point)) - topLeft;
if PtInRect(r, pt) then
PutPixel(pt, Color);
end;
if cnt > 0 then
ADrawer.PutImage(topLeft.X, topLeft.Y, img);
finally
img.Free;
end;
end;
procedure TManhattanSeries.GetLegendItems(AItems: TChartLegendItems);
begin
Unused(AItems); // TODO
end;
procedure TManhattanSeries.SetSeriesColor(AValue: TColor);
begin
if FSeriesColor = AValue then exit;
FSeriesColor := AValue;
UpdateParentChart;
end;
{ TConstantLine }
procedure TConstantLine.AfterAdd;
begin
inherited;
Arrow.SetOwner(ParentChart);
end;
procedure TConstantLine.Assign(ASource: TPersistent);
begin
if ASource is TConstantLine then
with TConstantLine(ASource) do begin
Self.FArrow.Assign(FArrow);
Self.FLineStyle := FLineStyle;
Self.Pen := FPen;
Self.FPosition := FPosition;
Self.FUseBounds := FUseBounds;
end;
inherited Assign(ASource);
end;
constructor TConstantLine.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FArrow := TChartArrow.Create(ParentChart);
FLineStyle := lsHorizontal;
FPen := TPen.Create;
FPen.OnChange := @StyleChanged;
FUseBounds := true;
end;
destructor TConstantLine.Destroy;
begin
FreeAndNil(FArrow);
FreeAndNil(FPen);
inherited;
end;
procedure TConstantLine.Draw(ADrawer: IChartDrawer);
var
p: Integer;
begin
if Pen.Style = psClear then exit;
ADrawer.SetBrushParams(bsClear, clTAColor);
ADrawer.Pen := FPen;
with ParentChart do
case LineStyle of
lsHorizontal: begin
p := YGraphToImage(AxisToGraphX(Position));
// The "X" here is correct:
// The constant line series needs only a single axis, which is its
// "x axis" - the user will set the axis index to that of the y axis
// for the case of a horizontal line. Therefore, AxisToGraph must get
// the transformation from the line's x axis (even if it is the y axis
// of the chart!).
DrawLineHoriz(ADrawer, p);
if Arrow.Inverted then
Arrow.Draw(ADrawer, Point(ClipRect.Left, p), 0, Pen)
else
Arrow.Draw(ADrawer, Point(ClipRect.Right - 1, p), 0, Pen);
end;
lsVertical: begin
p := XGraphToImage(AxisToGraphX(Position));
DrawLineVert(ADrawer, p);
if Arrow.Inverted then
Arrow.Draw(ADrawer, Point(p, ClipRect.Bottom - 1), -Pi / 2, Pen)
else
Arrow.Draw(ADrawer, Point(p, ClipRect.Top), -Pi / 2, Pen);
end;
end;
end;
function TConstantLine.GetAxisIndex: TChartAxisIndex;
begin
Result := inherited AxisIndexX;
end;
procedure TConstantLine.GetBounds(var ABounds: TDoubleRect);
begin
if not UseBounds then exit;
SavePosToCoord(ABounds.a);
SavePosToCoord(ABounds.b);
end;
procedure TConstantLine.GetLegendItems(AItems: TChartLegendItems);
begin
AItems.Add(TLegendItemLine.Create(Pen, LegendTextSingle));
end;
function TConstantLine.GetNearestPoint(
const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean;
begin
AResults.FIndex := -1;
AResults.FImg := AParams.FPoint;
// Return the actual nearest point of the line.
if LineStyle = lsVertical then begin
AResults.FValue.Y := FChart.YImageToGraph(AParams.FPoint.Y);
AResults.FImg.X := FChart.XGraphToImage(AxisToGraphX(Position));
end
else begin
AResults.FValue.X := FChart.XImageToGraph(AParams.FPoint.X);
AResults.FImg.Y := FChart.YGraphToImage(AxisToGraphX(Position));
end;
AResults.FDist := AParams.FDistFunc(AParams.FPoint, AResults.FImg);
Result := AResults.FDist <= Sqr(AParams.FRadius);
SavePosToCoord(AResults.FValue);
end;
function TConstantLine.GetSeriesColor: TColor;
begin
Result := FPen.Color;
end;
function TConstantLine.IsEmpty: Boolean;
begin
Result := false;
end;
procedure TConstantLine.MovePoint(
var AIndex: Integer; const ANewPos: TDoublePoint);
begin
Unused(AIndex);
Position :=
GraphToAxisX(TDoublePointBoolArr(ANewPos)[LineStyle = lsHorizontal]);
end;
procedure TConstantLine.SavePosToCoord(var APoint: TDoublePoint);
begin
TDoublePointBoolArr(APoint)[LineStyle = lsHorizontal] := Position;
end;
procedure TConstantLine.SetArrow(AValue: TChartArrow);
begin
FArrow.Assign(AValue);
UpdateParentChart;
end;
procedure TConstantLine.SetAxisIndex(AValue: TChartAxisIndex);
begin
inherited AxisIndexX := AValue;
AxisIndexY := AValue;
// Make sure that both axis indexes have the same value. The ConstantLineSeries
// does use only the x axis index, but transformations of the y axis outside
// this unit may require tha y axis index - which would not be correct without
// this here...
end;
procedure TConstantLine.SetLineStyle(AValue: TLineStyle);
begin
if FLineStyle = AValue then exit;
FLineStyle := AValue;
UpdateParentChart;
end;
procedure TConstantLine.SetPen(AValue: TPen);
begin
FPen.Assign(AValue);
end;
procedure TConstantLine.SetPosition(AValue: Double);
begin
if FPosition = AValue then exit;
FPosition := AValue;
UpdateParentChart;
end;
procedure TConstantLine.SetSeriesColor(AValue: TColor);
begin
if FPen.Color = AValue then exit;
FPen.Color := AValue;
end;
procedure TConstantLine.SetUseBounds(AValue: Boolean);
begin
if FUseBounds = AValue then exit;
FUseBounds := AValue;
UpdateParentChart;
end;
procedure TConstantLine.UpdateBiDiMode;
begin
if LineStyle = lsHorizontal then
Arrow.Inverted := not Arrow.Inverted;
end;
{ TBarSeries }
procedure TBarSeries.Assign(ASource: TPersistent);
begin
if ASource is TBarSeries then
with TBarSeries(ASource) do begin
Self.BarBrush := FBarBrush;
Self.FBarOffsetPercent := FBarOffsetPercent;
Self.BarPen := FBarPen;
Self.FBarWidthPercent := FBarWidthPercent;
Self.FBarWidthStyle := FBarWidthStyle;
Self.FOnBeforeDrawBar := FOnBeforeDrawBar;
Self.FZeroLevel := FZeroLevel;
end;
inherited Assign(ASource);
end;
procedure TBarSeries.BarOffsetWidth(
AX: Double; AIndex: Integer; out AOffset, AWidth: Double);
var
r: Double;
begin
case BarWidthStyle of
bwPercent: r := GetXRange(AX, AIndex) * PERCENT;
bwPercentMin: r := FMinXRange * PERCENT;
else
raise EBarError.Create('BarWidthStyle not implemented');
end;
AOffset := r * BarOffsetPercent;
AWidth := r * BarWidthPercent / 2;
end;
constructor TBarSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ToolTargets := [nptPoint, nptYList, nptCustom];
FBarWidthPercent := DEF_BAR_WIDTH_PERCENT;
FBarBrush := TBrush.Create;
FBarBrush.OnChange := @StyleChanged;
FBarPen := TPen.Create;
FBarPen.OnChange := @StyleChanged;
FBarPen.Color := clBlack;
FBarBrush.Color := clRed;
FStacked := true;
FOptimizeX := false;
FSupportsZeroLevel := true;
end;
destructor TBarSeries.Destroy;
begin
FreeAndNil(FBarPen);
FreeAndNil(FBarBrush);
inherited;
end;
procedure TBarSeries.Draw(ADrawer: IChartDrawer);
var
pointIndex, stackIndex: Integer;
scaled_depth: Integer;
procedure DrawBar(const AR: TRect);
var
sz: TSize;
defaultDrawing: Boolean = true;
c: TColor;
ic: IChartTCanvasDrawer;
begin
ADrawer.Brush := BarBrush;
ADrawer.Pen := BarPen;
if Styles <> nil then
Styles.Apply(ADrawer, stackIndex);
c := Source[pointIndex]^.Color;
if c <> clTAColor then
ADrawer.BrushColor := c;
sz := Size(AR);
if (sz.cx <= 2) or (sz.cy <= 2) then begin
// Bars are too small to distinguish the border from the interior.
ADrawer.SetPenParams(psSolid, ADrawer.BrushColor);
end;
if Supports(ADrawer, IChartTCanvasDrawer, ic) and Assigned(OnBeforeDrawBar) then
OnBeforeDrawBar(Self, ic.Canvas, AR, pointIndex, stackIndex, defaultDrawing);
if not defaultDrawing then exit;
ADrawer.Rectangle(AR);
if Depth = 0 then exit;
ADrawer.DrawLineDepth(AR.Left, AR.Top, AR.Right - 1, AR.Top, scaled_depth);
ADrawer.DrawLineDepth(
AR.Right - 1, AR.Top, AR.Right - 1, AR.Bottom - 1, scaled_depth);
end;
var
ext2: TDoubleRect;
w: Double;
p: TDoublePoint;
heights: TDoubleDynArray;
procedure BuildBar(x, y1, y2: Double);
var
graphBar: TDoubleRect;
imageBar: TRect;
begin
graphBar := DoubleRect(x - w, y1, x + w, y2);
if IsRotated then
with graphBar do begin
Exchange(a.X, a.Y);
Exchange(b.X, b.Y);
end;
if not RectIntersectsRect(graphBar, ext2) then exit;
with imageBar do begin
TopLeft := ParentChart.GraphToImage(graphBar.a);
BottomRight := ParentChart.GraphToImage(graphBar.b);
TAGeometry.NormalizeRect(imageBar);
// Draw a line instead of an empty rectangle.
// if Bottom = Top then Dec(Top);
// if Left = Right then Inc(Right);
end;
DrawBar(imageBar);
end;
var
ofs, y: Double;
begin
if IsEmpty then exit;
if BarWidthStyle = bwPercentMin then
UpdateMinXRange;
ext2 := ParentChart.CurrentExtent;
ExpandRange(ext2.a.X, ext2.b.X, 1.0);
ExpandRange(ext2.a.Y, ext2.b.Y, 1.0);
scaled_depth := ADrawer.Scale(Depth);
PrepareGraphPoints(ext2, true);
SetLength(heights, Source.YCount + 1);
for pointIndex := FLoBound to FUpBound do begin
p := Source[pointIndex]^.Point;
if IsNan(p.X) then continue;
p.X := AxisToGraphX(p.X);
BarOffsetWidth(p.X, pointIndex, ofs, w);
p.X += ofs;
heights[0] := ZeroLevel;
if FStacked then begin
heights[1] := NumberOr(p.Y, ZeroLevel);
for stackIndex := 1 to Source.YCount - 1 do begin
y := Source[pointIndex]^.YList[stackIndex - 1];
if not IsNan(y) then
heights[stackIndex + 1] := heights[stackIndex] + y;
end;
for stackIndex := 0 to High(heights) do
heights[stackindex] := AxisToGraphY(heights[stackindex]);
for stackIndex := 0 to Source.YCount - 1 do
BuildBar(p.X, heights[stackindex], heights[stackIndex+1]);
end else begin
for stackIndex := 0 to Source.YCount - 1 do begin
y := Source[pointIndex]^.GetY(stackIndex);
if not IsNaN(y) then
heights[stackIndex + 1] := AxisToGraphY(y)
else
heights[stackIndex + 1] := ZeroLevel;
end;
p.X -= w;
w := w / High(heights);
p.X += w;
for stackIndex := 0 to Source.YCount - 1 do begin
BuildBar(p.X, heights[0], heights[stackIndex+1]);
p.X += 2*w;
end;
end;
end;
DrawLabels(ADrawer);
end;
function TBarSeries.Extent: TDoubleRect;
var
x, ofs, w: Double;
i: Integer;
begin
Result := inherited Extent;
if IsEmpty then exit;
if BarWidthStyle = bwPercentMin then
UpdateMinXRange;
UpdateMinMax(ZeroLevel, Result.a.Y, Result.b.Y);
// Show first and last bars fully.
i := 0;
x := NearestXNumber(i, +1); // --> x is in graph units
if not IsNan(x) then begin
BarOffsetWidth(x, i, ofs, w);
x := GraphToAxisX(x + ofs - w); // x is in graph units, Extent in axis units!
Result.a.X := Min(Result.a.X, x);
end;
i := Count - 1;
x := NearestXNumber(i, -1);
if not IsNan(x) then begin
BarOffsetWidth(x, i, ofs, w);
x := GraphToAxisX(x + ofs + w);
Result.b.X := Max(Result.b.X, x);
end;
end;
function TBarSeries.GetBarWidth(AIndex: Integer): Integer;
var
ofs, w: Double;
f: TGraphToImageFunc;
begin
BarOffsetWidth(GetGraphPointX(AIndex), AIndex, ofs, w);
if IsRotated then
f := @FChart.YGraphToImage
else
f := @FChart.XGraphToImage;
Result := Abs(f(2 * w) - f(0));
end;
function TBarSeries.GetLabelDataPoint(AIndex, AYIndex: Integer): TDoublePoint;
var
ofs, w, wbar: Double;
begin
Result := inherited GetLabelDataPoint(AIndex, AYIndex);
BarOffsetWidth(TDoublePointBoolArr(Result)[IsRotated], AIndex, ofs, w);
TDoublePointBoolArr(Result)[IsRotated] += ofs;
// Find x centers of bars in non-stacked bar series with multiple y values.
if (not FStacked) and (Source.YCount > 1) then begin
wbar := 2 * w / Source.YCount;
TDoublePointboolArr(Result)[IsRotated] += (wbar * (AYIndex + 0.5) - w);
end;
end;
procedure TBarSeries.GetLegendItems(AItems: TChartLegendItems);
begin
GetLegendItemsRect(AItems, BarBrush);
end;
function TBarSeries.GetNearestPoint(const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean;
var
pointIndex: Integer;
graphClickPt: TDoublePoint;
sp: TDoublePoint;
ofs, w: Double;
heights: TDoubleDynArray;
y: Double;
stackindex: Integer;
begin
Result := false;
AResults.FDist := Sqr(AParams.FRadius) + 1;
AResults.FIndex := -1;
AResults.FXIndex := 0;
AResults.FYIndex := 0;
if not ((nptCustom in AParams.FTargets) and (nptCustom in ToolTargets))
then begin
Result := inherited;
exit;
end;
SetLength(heights, Source.YCount + 1);
// clicked point in image units
graphClickPt := ParentChart.ImageToGraph(AParams.FPoint);
if IsRotated then
Exchange(graphclickpt.X, graphclickpt.Y);
// Iterate through all points of the series
for pointIndex := 0 to Count - 1 do begin
sp := Source[pointindex]^.Point;
if IsNan(sp) then
continue;
sp.X := AxisToGraphX(sp.X);
BarOffsetWidth(sp.X, pointindex, ofs, w); // works with graph units
sp.X := sp.X + ofs;
if not InRange(graphClickPt.X, sp.X - w, sp.X + w) then
continue;
// Calculate stacked bar levels (in axis units)
heights[0] := ZeroLevel;
heights[1] := NumberOr(sp.Y, ZeroLevel);
for stackIndex := 1 to Source.YCount-1 do begin
y := NumberOr(Source[pointindex]^.YList[stackIndex - 1], 0);
heights[stackIndex + 1] := heights[stackindex] + y;
end;
// Convert heights to graph units
for stackIndex := 0 to High(heights) do
heights[stackIndex] := AxisToGraphY(heights[stackIndex]);
// Check if clicked pt is inside stacked bar
for stackindex := 0 to High(heights)-1 do
if ((heights[stackindex] < heights[stackindex + 1]) and
InRange(graphClickPt.Y, heights[stackindex], heights[stackIndex + 1]))
or
((heights[stackindex + 1] < heights[stackindex]) and
InRange(graphClickPt.Y, heights[stackindex + 1], heights[stackIndex]))
then begin
AResults.FDist := 0;
AResults.FIndex := pointindex;
AResults.FYIndex := stackIndex;
AResults.FValue := DoublePoint(Source[pointindex]^.X, Source[pointindex]^.GetY(stackIndex));
AResults.FValue := AxisToGraph(AResults.FValue);
AResults.FImg := ParentChart.GraphToImage(AResults.FValue);
Result := true;
exit;
end;
end;
end;
function TBarSeries.GetSeriesColor: TColor;
begin
Result := FBarBrush.Color;
end;
function TBarSeries.GetZeroLevel: Double;
begin
Result := ZeroLevel;
end;
function TBarSeries.IsZeroLevelStored: boolean;
begin
Result := ZeroLevel <> 0.0;
end;
procedure TBarSeries.SetBarBrush(Value: TBrush);
begin
FBarBrush.Assign(Value);
end;
procedure TBarSeries.SetBarOffsetPercent(AValue: Integer);
begin
if FBarOffsetPercent = AValue then exit;
FBarOffsetPercent := AValue;
UpdateParentChart;
end;
procedure TBarSeries.SetBarPen(Value:TPen);
begin
FBarPen.Assign(Value);
end;
procedure TBarSeries.SetBarWidthPercent(Value: Integer);
begin
if (Value < 1) or (Value > 100) then
raise EBarError.Create('Wrong BarWidth Percent');
FBarWidthPercent := Value;
UpdateParentChart;
end;
procedure TBarSeries.SetBarWidthStyle(AValue: TBarWidthStyle);
begin
if FBarWidthStyle = AValue then exit;
FBarWidthStyle := AValue;
UpdateParentChart;
end;
procedure TBarSeries.SetOnBeforeDrawBar(AValue: TBeforeDrawBarEvent);
begin
if TMethod(FOnBeforeDrawBar) = TMethod(AValue) then exit;
FOnBeforeDrawBar := AValue;
UpdateParentChart;
end;
procedure TBarSeries.SetSeriesColor(AValue: TColor);
begin
FBarBrush.Color := AValue;
end;
procedure TBarSeries.SetZeroLevel(AValue: Double);
begin
if FZeroLevel = AValue then exit;
FZeroLevel := AValue;
UpdateParentChart;
end;
function TBarSeries.ToolTargetDistance(const AParams: TNearestPointParams;
AGraphPt: TDoublePoint; APointIdx, AXIdx, AYIdx: Integer): Integer;
var
sp1, sp2: TDoublePoint;
clickPt, pt1, pt2: TPoint;
ofs, w: Double;
dist1, dist2: Integer;
begin
Unused(APointIdx);
Unused(AXIdx, AYIdx);
clickPt := AParams.FPoint;
if IsRotated then begin
Exchange(clickPt.X, clickPt.Y);
Exchange(AGraphPt.X, AGraphPt.Y);
end;
BarOffsetWidth(AGraphPt.X, APointIdx, ofs, w);
sp1 := DoublePoint(AGraphPt.X + ofs - w, AGraphPt.Y);
sp2 := DoublePoint(AGraphPt.X + ofs + w, AGraphPt.Y);
if IsRotated then begin
Exchange(sp1.X, sp1.Y);
Exchange(sp2.X, sp2.Y);
end;
pt1 := ParentChart.GraphToImage(sp1);
pt2 := ParentChart.GraphToImage(sp2);
if IsRotated then begin
Exchange(pt1.X, pt1.Y);
Exchange(pt2.X, pt2.Y);
if pt1.X > pt2.X then Exchange(pt1.X, pt2.X);
end;
if InRange(clickPt.X, pt1.X, pt2.X) then
Result := sqr(clickPt.Y - pt1.Y)
else begin
dist1 := AParams.FDistFunc(clickPt, pt1);
dist2 := AParams.FDistFunc(clickPt, pt2);
Result := Min(dist1, dist2);
end;
end;
{ TAreaSeries }
procedure TAreaSeries.Assign(ASource: TPersistent);
begin
if ASource is TAreaSeries then
with TAreaSeries(ASource) do begin
Self.AreaBrush := FAreaBrush;
Self.AreaContourPen := FAreaContourPen;
Self.AreaLinesPen := FAreaLinesPen;
Self.FConnectType := FConnectType;
Self.FUseZeroLevel := FUseZeroLevel;
Self.FZeroLevel := FZeroLevel;
end;
inherited Assign(ASource);
end;
constructor TAreaSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAreaBrush := TBrush.Create;
FAreaBrush.OnChange := @StyleChanged;
FAreaContourPen := TPen.Create;
FAreaContourPen.OnChange := @StyleChanged;
FAreaLinesPen := TPen.Create;
FAreaLinesPen.OnChange := @StyleChanged;
FStacked := true;
FSupportsZeroLevel := FUseZeroLevel;
end;
destructor TAreaSeries.Destroy;
begin
FreeAndNil(FAreaBrush);
FreeAndNil(FAreaContourPen);
FreeAndNil(FAreaLinesPen);
inherited;
end;
procedure TAreaSeries.Draw(ADrawer: IChartDrawer);
var
pts: TPointArray;
numPts: Integer;
scaled_depth: Integer;
procedure PushPoint(const AP: TPoint); overload;
begin
if (numPts > 0) and (AP = pts[numPts - 1]) then exit;
pts[numPts] := AP;
numPts += 1;
end;
procedure PushPoint(const AP: TDoublePoint); overload;
begin
PushPoint(ParentChart.GraphToImage(AP));
end;
function ProjToLine(const APt: TDoublePoint; ACoord: Double): TDoublePoint;
begin
Result := APt;
if IsRotated then
Result.X := ACoord
else
Result.Y := ACoord;
end;
var
ext, ext2: TDoubleRect;
prevPts: TPointArray;
procedure CollectPoints(AStart, AEnd: Integer);
var
i: Integer;
a, b: TDoublePoint;
begin
for i := AStart to AEnd - 1 do begin
a := FGraphPoints[i];
b := FGraphPoints[i + 1];
case ConnectType of
ctLine: ;
ctStepXY:
if IsRotated then
b.X := a.X
else
b.Y := a.Y;
ctStepYX:
if IsRotated then
a.X := b.X
else
a.Y := b.Y;
end;
if not IsRotated then begin
PushPoint(a);
PushPoint(b);
end else begin
PushPoint(a);
PushPoint(b);
end
end;
end;
procedure CopyPoints(var ADest: TPointArray; ASource: TPointArray;
ANumPts: Integer);
var
i: Integer;
begin
for i:=0 to ANumPts - 1 do
ADest[i] := ASource[i];
end;
procedure DrawSegment(AStart, AEnd: Integer);
var
i, j, j0, n2, numPrevPts, numSavedPts: Integer;
a, b: TDoublePoint;
a0, b0: TDoublePoint;
z, z1, z2: Double;
begin
numPts := 0;
if UseZeroLevel then
z := AxisToGraphY(ZeroLevel)
else
z := IfThen(IsRotated, ext2.a.X, ext2.a.Y);
z1 := z;
z2 := z;
a0 := FGraphPoints[AStart];
b0 := FGraphPoints[AEnd];
// Collect points of top-most curve
UpdateGraphPoints(Source.YCount-2, FLoBound, FUpBound, FStacked);
// Index (1st parameter) refers to YList --> one less than usual!)
numPts := 0;
CollectPoints(AStart, AEnd);
CopyPoints(prevPts, pts, numPts);
numPrevPts := numPts;
numSavedPts := numPts;
// Collect points of lower end of each level
j0 := IfThen(FBanded and (Source.YCount > 1), 0, -1);
for j := Source.YCount - 2 downto j0 do begin
numPts := 0;
if (j > -1) then begin
// Stack level points
UpdateGraphPoints(j - 1, FLoBound, FUpBound, FStacked);
CollectPoints(AStart, AEnd);
numSavedPts := numPts;
for i := 0 to numPrevPts-1 do
PushPoint(prevPts[numPrevPts - 1 - i]);
CopyPoints(prevPts, pts, numSavedPts);
numPrevPts := numSavedPts;
end else
begin
// Zerolevel points
a := ProjToRect(a0, ext2);
PushPoint(ProjToLine(a, z1));
z1 := IfThen(IsRotated, a.X, a.Y);
for i:=0 to numPrevPts - 1 do
PushPoint(prevPts[i]);
b := ProjToRect(b0, ext2);
PushPoint(ProjToLine(b, z2));
a := ProjToRect(FGraphPoints[AEnd], ext2);
z2 := IfThen(IsRotated, a.X, a.Y);
end;
n2 := numPts;
// Prepare painting
ADrawer.Brush := AreaBrush;
ADrawer.Pen := AreaContourPen;
if Styles <> nil then
Styles.Apply(ADrawer, j - j0);
// Draw 3D sides
// Note: Rendering is often incorrect, e.g. when values cross zero level!
if (Depth > 0) then begin
if (Source.YCount = 1) or ((not FStacked) and (j = -1)) then
for i := 1 to numSavedPts do
ADrawer.DrawlineDepth(pts[i], pts[i+1], scaled_depth)
else
if ((not FStacked) or (j = Source.YCount-2)) then
for i:= n2-2 downto n2 - numSavedPts - 1 do
ADrawer.DrawLineDepth(pts[i], pts[i + 1], scaled_depth)
else
if FStacked then begin
if (j > -1) then
ADrawer.DrawLineDepth(pts[numSavedPts - 1], pts[numSavedPts], scaled_depth)
else
if (j = -1) and (FStacked or FBanded) then
ADrawer.DrawLineDepth(pts[numSavedPts], pts[numSavedPts+1], scaled_depth);
end;
end;
// Fill area
ADrawer.Polygon(pts, 0, numPts);
// Draw droplines
if (AreaLinesPen.Style <> psClear) and (j > -1) then begin
ADrawer.Pen := AreaLinesPen;
for i := 1 to numPrevPts-2 do
ADrawer.Line(pts[i], pts[numpts - 1 - i]);
end;
end;
// Drop lines of lowest layer
if (AreaLinesPen.Style <> psClear) and (not FBanded) then begin
ADrawer.Pen := AreaLinesPen;
for i := AStart + 1 to AEnd - 1 do begin
a := ProjToRect(FGraphPoints[i], ext2);
b := ProjToLine(a, z);
ADrawer.Line(ParentChart.GraphToImage(a), ParentChart.GraphToImage(b));
end;
end;
end;
var
i, j: Integer;
begin
if IsEmpty then exit;
ext := ParentChart.CurrentExtent;
ext2 := ext;
ExpandRange(ext2.a.X, ext2.b.X, 0.1);
ExpandRange(ext2.a.Y, ext2.b.Y, 0.1);
PrepareGraphPoints(ext, true);
if Length(FGraphPoints) = 0 then exit;
scaled_depth := ADrawer.Scale(Depth);
SetLength(pts, Length(FGraphPoints) * 4 + 4);
SetLength(prevPts, Length(pts));
j := -1;
for i := 0 to High(FGraphPoints) do
if IsNan(FGraphPoints[i]) = (j >= 0) then
if j >= 0 then begin
DrawSegment(j, i - 1);
j := -1;
end
else
j := i;
if j >= 0 then
DrawSegment(j, High(FGraphPoints));
DrawLabels(ADrawer);
end;
function TAreaSeries.Extent: TDoubleRect;
begin
Result := inherited Extent;
if not IsEmpty and UseZeroLevel then
UpdateMinMax(ZeroLevel, Result.a.Y, Result.b.Y);
end;
procedure TAreaSeries.GetLegendItems(AItems: TChartLegendItems);
begin
GetLegendItemsRect(AItems, AreaBrush);
end;
function TAreaSeries.GetSeriesColor: TColor;
begin
Result := FAreaBrush.Color;
end;
function TAreaSeries.GetZeroLevel: Double;
begin
Result := IfThen(UseZeroLevel, ZeroLevel, 0.0);
end;
function TAreaSeries.IsZeroLevelStored: boolean;
begin
Result := ZeroLevel <> 0.0;
end;
procedure TAreaSeries.SetAreaBrush(AValue: TBrush);
begin
FAreaBrush.Assign(AValue);
UpdateParentChart;
end;
procedure TAreaSeries.SetAreaContourPen(AValue: TPen);
begin
FAreaContourPen.Assign(AValue);
UpdateParentChart;
end;
procedure TAreaSeries.SetAreaLinesPen(AValue: TPen);
begin
FAreaLinesPen.Assign(AValue);
UpdateParentChart;
end;
procedure TAreaSeries.SetBanded(AValue: Boolean);
begin
if FBanded = AValue then exit;
FBanded := AValue;
UpdateParentChart;
end;
procedure TAreaSeries.SetConnectType(AValue: TConnectType);
begin
if FConnectType = AValue then exit;
FConnectType := AValue;
UpdateParentChart;
end;
procedure TAreaSeries.SetSeriesColor(AValue: TColor);
begin
FAreaBrush.Color := AValue;
end;
procedure TAreaSeries.SetUseZeroLevel(AValue: Boolean);
begin
if FUseZeroLevel = AValue then exit;
FUseZeroLevel := AValue;
FSupportsZeroLevel := FUseZeroLevel;
UpdateParentChart;
end;
procedure TAreaSeries.SetZeroLevel(AValue: Double);
begin
if FZeroLevel = AValue then exit;
FZeroLevel := AValue;
UpdateParentChart;
end;
{ TUserDrawnSeries }
procedure TUserDrawnSeries.Assign(ASource: TPersistent);
begin
if ASource is TUserDrawnSeries then
with TUserDrawnSeries(ASource) do begin
Self.FOnDraw := FOnDraw;
Self.FOnGetBounds := FOnGetBounds;
end;
inherited Assign(ASource);
end;
procedure TUserDrawnSeries.Draw(ADrawer: IChartDrawer);
var
ic: IChartTCanvasDrawer;
begin
if Supports(ADrawer, IChartTCanvasDrawer, ic) and Assigned(FOnDraw) then
FOnDraw(ic.Canvas, FChart.ClipRect);
end;
procedure TUserDrawnSeries.GetBounds(var ABounds: TDoubleRect);
begin
if Assigned(FOnGetBounds) then
FOnGetBounds(ABounds);
end;
procedure TUserDrawnSeries.GetLegendItems(AItems: TChartLegendItems);
begin
Unused(AItems);
end;
function TUserDrawnSeries.IsEmpty: Boolean;
begin
Result := not Assigned(FOnDraw);
end;
procedure TUserDrawnSeries.SetOnDraw(AValue: TSeriesDrawEvent);
begin
if TMethod(FOnDraw) = TMethod(AValue) then exit;
FOnDraw := AValue;
UpdateParentChart;
end;
procedure TUserDrawnSeries.SetOnGetBounds(AValue: TSeriesGetBoundsEvent);
begin
if TMethod(FOnGetBounds) = TMethod(AValue) then exit;
FOnGetBounds := AValue;
UpdateParentChart;
end;
procedure SkipObsoleteProperties;
const
STAIRS_NOTE = 'Obsolete, use ConnectType instead';
DRAWPOINTER_NOTE = 'Obsolete, use OnCustomDrawPointer instead';
begin
RegisterPropertyEditor(
TypeInfo(TChartAxisIndex), TConstantLine, 'AxisIndexX', THiddenPropertyEditor);
RegisterPropertyToSkip(TAreaSeries, 'Stairs', STAIRS_NOTE, '');
RegisterPropertyToSkip(TAreaSeries, 'InvertedStairs', STAIRS_NOTE, '');
RegisterPropertyToSkip(TLineSeries, 'OnDrawPointer', DRAWPOINTER_NOTE, '');
end;
initialization
RegisterSeriesClass(TLineSeries, @rsLineSeries);
RegisterSeriesClass(TAreaSeries, @rsAreaSeries);
RegisterSeriesClass(TBarSeries, @rsBarSeries);
RegisterSeriesClass(TPieSeries, @rsPieSeries);
RegisterSeriesClass(TUserDrawnSeries, @rsUserDrawnSeries);
RegisterSeriesClass(TConstantLine, @rsConstantLine);
RegisterSeriesClass(TManhattanSeries, @rsManhattanPlotSeries);
// {$WARNINGS OFF}RegisterSeriesClass(TLine, nil);{$WARNINGS ON}
SkipObsoleteProperties;
end.
|