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 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
|
{
Function series for TAChart.
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Authors: Alexander Klenin
}
unit TAFuncSeries;
{$H+}
interface
uses
Classes, Graphics, typ, Types,
TAChartUtils, TACustomFuncSeries, TACustomSeries, TACustomSource,
TADrawUtils, TAFitUtils, TALegend, TATypes;
const
DEF_FUNC_STEP = 2;
DEF_SPLINE_DEGREE = 3;
DEF_SPLINE_STEP = 4;
DEF_FIT_STEP = 4;
DEF_FIT_PARAM_COUNT = 3;
DEF_COLORMAP_STEP = 4;
type
TFuncCalculateEvent = procedure (const AX: Double; out AY: Double) of object;
TFuncSeriesStep = 1..MaxInt;
TFuncSeries = class(TBasicFuncSeries)
strict private
FDomainExclusions: TIntervalList;
FExtentAutoY: Boolean;
FOnCalculate: TFuncCalculateEvent;
FPen: TChartPen;
FStep: TFuncSeriesStep;
function DoCalcIdentity(AX: Double): Double;
function DoCalculate(AX: Double): Double;
procedure SetExtentAutoY(AValue: Boolean);
procedure SetOnCalculate(AValue: TFuncCalculateEvent);
procedure SetPen(AValue: TChartPen);
procedure SetStep(AValue: TFuncSeriesStep);
protected
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;
public
property DomainExclusions: TIntervalList read FDomainExclusions;
published
property AxisIndexX;
property AxisIndexY;
property OnCalculate: TFuncCalculateEvent
read FOnCalculate write SetOnCalculate;
property ExtentAutoY: Boolean
read FExtentAutoY write SetExtentAutoY default false;
property Pen: TChartPen read FPen write SetPen;
property Step: TFuncSeriesStep
read FStep write SetStep default DEF_FUNC_STEP;
end;
TParametricCurveCalculateEvent = procedure (
const AT: Double; out AX, AY: Double) of object;
TParametricCurveSeries = class(TBasicFuncSeries)
strict private
FOnCalculate: TParametricCurveCalculateEvent;
FParamMax: Double;
FParamMaxStep: Double;
FParamMin: Double;
FPen: TChartPen;
FStep: TFuncSeriesStep;
function DoCalcIdentity(AT: Double): TDoublePoint;
function DoCalculate(AT: Double): TDoublePoint;
function ParamMaxIsStored: Boolean;
function ParamMaxStepIsStored: Boolean;
function ParamMinIsStored: Boolean;
procedure SetOnCalculate(AValue: TParametricCurveCalculateEvent);
procedure SetParamMax(AValue: Double);
procedure SetParamMaxStep(AValue: Double);
procedure SetParamMin(AValue: Double);
procedure SetPen(AValue: TChartPen);
procedure SetStep(AValue: TFuncSeriesStep);
protected
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 IsEmpty: Boolean; override;
published
property AxisIndexX;
property AxisIndexY;
property OnCalculate: TParametricCurveCalculateEvent
read FOnCalculate write SetOnCalculate;
property ParamMax: Double read FParamMax write SetParamMax
stored ParamMaxIsStored;
property ParamMaxStep: Double
read FParamMaxStep write SetParamMaxStep stored ParamMaxStepIsStored;
property ParamMin: Double
read FParamMin write SetParamMin stored ParamMinIsStored;
property Pen: TChartPen read FPen write SetPen;
property Step: TFuncSeriesStep
read FStep write SetStep default DEF_FUNC_STEP;
end;
TSplineDegree = 1..100;
{ TBSplineSeries }
TBSplineSeries = class(TBasicPointSeries)
strict private
FDegree: TSplineDegree;
FPen: TChartPen;
FStep: TFuncSeriesStep;
procedure InternalPrepareGraphPoints;
procedure SetDegree(AValue: TSplineDegree);
procedure SetPen(AValue: TChartPen);
procedure SetStep(AValue: TFuncSeriesStep);
protected
procedure GetLegendItems(AItems: TChartLegendItems); override;
public
procedure Assign(ASource: TPersistent); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function Calculate(AX: Double): Double;
procedure Draw(ADrawer: IChartDrawer); override;
published
property Active default true;
property AxisIndexX;
property AxisIndexY;
property ShowInLegend;
property Source;
property Title;
property ZPosition;
published
property Degree: TSplineDegree
read FDegree write SetDegree default DEF_SPLINE_DEGREE;
property Pen: TChartPen read FPen write SetPen;
property Pointer;
property Step: TFuncSeriesStep
read FStep write SetStep default DEF_SPLINE_STEP;
end;
TBadDataChartPen = class(TChartPen)
published
property Color default clRed;
end;
TCubicSplineOptions = set of (
csoDrawFewPoints, csoDrawUnorderedX, csoExtrapolateLeft,
csoExtrapolateRight);
TCubicSplineSeries = class(TBasicPointSeries)
strict private
FBadDataPen: TBadDataChartPen;
FOptions: TCubicSplineOptions;
FPen: TChartPen;
FStep: TFuncSeriesStep;
procedure SetPen(AValue: TChartPen);
procedure SetStep(AValue: TFuncSeriesStep);
strict private
type
TSpline = class
public
FCoeff, FX, FY: array of ArbFloat;
FIntervals: TIntervalList;
FIsUnorderedX: Boolean;
FStartIndex: Integer;
destructor Destroy; override;
function Calculate(AX: Double): Double;
function IsFewPoints: Boolean; inline;
function PrepareCoeffs(
ASource: TCustomChartSource; var AIndex: Integer): Boolean;
procedure PrepareIntervals(AOptions: TCubicSplineOptions);
end;
var
FSplines: array of TSpline;
procedure FreeSplines;
function IsUnorderedVisible: Boolean; inline;
function IsFewPointsVisible: Boolean; inline;
procedure PrepareCoeffs;
procedure SetBadDataPen(AValue: TBadDataChartPen);
procedure SetOptions(AValue: TCubicSplineOptions);
protected
procedure GetLegendItems(AItems: TChartLegendItems); override;
procedure SourceChanged(ASender: TObject); override;
public
procedure Assign(ASource: TPersistent); override;
function Calculate(AX: Double): Double;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Draw(ADrawer: IChartDrawer); override;
function Extent: TDoubleRect; override;
function GetNearestPoint(
const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean; override;
published
property Active default true;
property AxisIndexX;
property AxisIndexY;
property Pointer;
property ShowInLegend;
property Source;
property Title;
property ZPosition;
published
// Used when data is not suitable for drawing cubic spline --
// e.g. points are too few or not ordered by X value.
property BadDataPen: TBadDataChartPen read FBadDataPen write SetBadDataPen;
property Options: TCubicSplineOptions
read FOptions write SetOptions default [];
property Pen: TChartPen read FPen write SetPen;
property Step: TFuncSeriesStep
read FStep write SetStep default DEF_SPLINE_STEP;
end;
TFitParamsState = (fpsUnknown, fpsInvalid, fpsValid);
TFitSeries = class(TBasicPointSeries)
strict private
FDrawFitRangeOnly: Boolean;
FFitEquation: TFitEquation;
FFitParams: TDoubleDynArray;
FFitRange: TChartRange;
FOnFitComplete: TNotifyEvent;
FPen: TChartPen;
FState: TFitParamsState;
FStep: TFuncSeriesStep;
function GetParam(AIndex: Integer): Double;
function GetParamCount: Integer;
function PrepareIntervals: TIntervalList;
procedure SetDrawFitRangeOnly(AValue: Boolean);
procedure SetFitEquation(AValue: TFitEquation);
procedure SetFitRange(AValue: TChartRange);
procedure SetParam(AIndex: Integer; AValue: Double);
procedure SetParamCount(AValue: Integer);
procedure SetPen(AValue: TChartPen);
procedure SetStep(AValue: TFuncSeriesStep);
strict protected
procedure CalcXRange(out AXMin, AXMax: Double);
procedure Transform(AX, AY: Double; out ANewX, ANewY: Extended);
protected
procedure AfterAdd; override;
procedure GetLegendItems(AItems: TChartLegendItems); override;
procedure SourceChanged(ASender: TObject); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
public
function Calculate(AX: Double): Double; virtual;
procedure Draw(ADrawer: IChartDrawer); override;
procedure ExecFit; virtual;
function EquationText: IFitEquationText;
function GetFitEquationString(
ANumFormat: String; AXText: String = 'x'; AYText: String = 'y'): String;
deprecated 'Use EquationText';
function GetNearestPoint(
const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean; override;
property Param[AIndex: Integer]: Double read GetParam write SetParam;
property State: TFitParamsState read FState;
published
property AxisIndexX;
property AxisIndexY;
property DrawFitRangeOnly: Boolean
read FDrawFitRangeOnly write SetDrawFitRangeOnly default true;
property FitEquation: TFitEquation read FFitEquation write SetFitEquation default fePolynomial;
property FitRange: TChartRange read FFitRange write SetFitRange;
property OnFitComplete: TNotifyEvent read FOnFitComplete write FOnFitComplete;
property ParamCount: Integer
read GetParamCount write SetParamCount default DEF_FIT_PARAM_COUNT;
property Pen: TChartPen read FPen write SetPen;
property Source;
property Step: TFuncSeriesStep read FStep write SetStep default DEF_FIT_STEP;
end;
TFuncCalculate3DEvent =
procedure (const AX, AY: Double; out AZ: Double) of object;
TColorMapSeries = class(TBasicFuncSeries)
public
type
TUseImage = (cmuiAuto, cmuiAlways, cmuiNever);
strict private
FBrush: TBrush;
FColorSource: TCustomChartSource;
FColorSourceListener: TListener;
FInterpolate: Boolean;
FOnCalculate: TFuncCalculate3DEvent;
FStepX: TFuncSeriesStep;
FStepY: TFuncSeriesStep;
FUseImage: TUseImage;
procedure SetBrush(AValue: TBrush);
procedure SetColorSource(AValue: TCustomChartSource);
procedure SetInterpolate(AValue: Boolean);
procedure SetOnCalculate(AValue: TFuncCalculate3DEvent);
procedure SetStepX(AValue: TFuncSeriesStep);
procedure SetStepY(AValue: TFuncSeriesStep);
procedure SetUseImage(AValue: TUseImage);
protected
procedure GetLegendItems(AItems: TChartLegendItems); override;
public
procedure Assign(ASource: TPersistent); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
public
function ColorByValue(AValue: Double): TColor;
procedure Draw(ADrawer: IChartDrawer); override;
function IsEmpty: Boolean; override;
published
property AxisIndexX;
property AxisIndexY;
property Brush: TBrush read FBrush write SetBrush;
property ColorSource: TCustomChartSource
read FColorSource write SetColorSource;
property Interpolate: Boolean
read FInterpolate write SetInterpolate default false;
property OnCalculate: TFuncCalculate3DEvent
read FOnCalculate write SetOnCalculate;
property StepX: TFuncSeriesStep
read FStepX write SetStepX default DEF_COLORMAP_STEP;
property StepY: TFuncSeriesStep
read FStepY write SetStepY default DEF_COLORMAP_STEP;
property UseImage: TUseImage
read FUseImage write SetUseImage default cmuiAuto;
end;
// Builds an equation string based on the parameters and the type of equation.
// AXText and AYText are placeholders for the x and y variables, respectively.
// Parameters are formatted by passing ANumFormat to the "Format" function.
function ParamsToEquation(
AEquation: TFitEquation; const AParams: array of Double;
ANumFormat: String; AXText: String = 'x'; AYText: String = 'y'): String;
deprecated 'Use IFitEquationText';
implementation
uses
ipf, GraphType, IntfGraphics, Math, StrUtils, SysUtils,
TAGeometry, TAGraph, TAMath;
const
DEF_PARAM_MIN = 0.0;
DEF_PARAM_MAX = 1.0;
type
TFitSeriesRange = class(TChartRange)
strict private
FSeries: TFitSeries;
strict protected
procedure StyleChanged(ASender: TObject); override;
public
constructor Create(ASeries: TFitSeries);
end;
TLegendItemColorMap = class(TLegendItem)
strict private
FColor2: TColor;
FFramePen: TChartPen;
public
constructor Create(
AColor1, AColor2: TColor; AFramePen: TChartPen; const AText: String);
procedure Draw(ADrawer: IChartDrawer; const ARect: TRect); override;
end;
TParametricFunc = function (A: Double): TDoublePoint of object;
function ParamsToEquation(
AEquation: TFitEquation; const AParams: array of Double;
ANumFormat, AXText, AYText: String): String;
begin
Result :=
TFitEquationText.Create.Equation(AEquation).
X(AXText).Y(AYText).NumFormat(ANumFormat).Params(AParams);
end;
{ TColorMapLegendItem }
constructor TLegendItemColorMap.Create(
AColor1, AColor2: TColor; AFramePen: TChartPen; const AText: String);
begin
inherited Create(AText);
Color := AColor1;
FColor2 := AColor2;
FFramePen := AFramePen;
end;
procedure TLegendItemColorMap.Draw(ADrawer: IChartDrawer; const ARect: TRect);
var
x, w, pw: Integer;
c: TColor;
begin
inherited Draw(ADrawer, ARect);
with FFramePen do
pw := IfThen(EffVisible, (Width + 1) div 2, 0);
w := ARect.Right - ARect.Left - 2 * pw;
if w <= 0 then exit;
for x := ARect.Left + pw to ARect.Right - pw do begin
c := InterpolateRGB(Color, FColor2, (x - ARect.Left - pw) / w);
ADrawer.SetPenParams(psSolid, c);
ADrawer.Line(x, ARect.Top, x, ARect.Bottom - 1);
end;
if pw > 0 then begin
ADrawer.Pen := FFramePen;
ADrawer.SetBrushParams(bsClear, clTAColor);
ADrawer.Rectangle(ARect);
end;
end;
{ TFitSeriesRange }
constructor TFitSeriesRange.Create(ASeries: TFitSeries);
begin
inherited Create(ASeries.ParentChart);
FSeries := ASeries;
end;
procedure TFitSeriesRange.StyleChanged(ASender: TObject);
begin
FSeries.SourceChanged(nil); // reset FState to fpsUnknown
FSeries.ExecFit;
inherited;
end;
{ TFuncSeries }
procedure TFuncSeries.Assign(ASource: TPersistent);
begin
if ASource is TFuncSeries then
with TFuncSeries(ASource) do begin
Self.FDomainExclusions.Assign(FDomainExclusions);
Self.FExtentAutoY := FExtentAutoY;
Self.FOnCalculate := FOnCalculate;
Self.Pen := FPen;
Self.FStep := FStep;
end;
inherited Assign(ASource);
end;
constructor TFuncSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDomainExclusions := TIntervalList.Create;
FDomainExclusions.OnChange := @StyleChanged;
FPen := TChartPen.Create;
FPen.OnChange := @StyleChanged;
FStep := DEF_FUNC_STEP;
end;
destructor TFuncSeries.Destroy;
begin
FreeAndNil(FDomainExclusions);
FreeAndNil(FPen);
inherited;
end;
function TFuncSeries.DoCalcIdentity(AX: Double): Double;
begin
Result := AX;
end;
function TFuncSeries.DoCalculate(AX: Double): Double;
begin
OnCalculate(AX, Result)
end;
procedure TFuncSeries.Draw(ADrawer: IChartDrawer);
var
calc: TTransformFunc;
begin
if Assigned(OnCalculate) then
calc := @DoCalculate
else if csDesigning in ComponentState then
calc := @DoCalcIdentity
else
exit;
ADrawer.Pen := Pen;
with TDrawFuncHelper.Create(Self, DomainExclusions, calc, Step) do
try
DrawFunction(ADrawer);
finally
Free;
end;
end;
procedure TFuncSeries.GetBounds(var ABounds: TDoubleRect);
var
ymin, ymax: Double;
begin
inherited GetBounds(ABounds);
if
not Extent.UseXMin or not Extent.UseXMax or not ExtentAutoY
or not Assigned(OnCalculate)
then
exit;
ymin := SafeInfinity;
ymax := NegInfinity;
with TDrawFuncHelper.Create(Self, DomainExclusions, @DoCalculate, Step) do
try
CalcAxisExtentY(ABounds.a.X, ABounds.b.X, ymin, ymax);
if not Extent.UseYMin or (ymin > Extent.YMin) then
ABounds.a.Y := ymin;
if not Extent.UseYMax or (ymax < Extent.YMax) then
ABounds.b.Y := ymax;
finally
Free;
end;
end;
procedure TFuncSeries.GetLegendItems(AItems: TChartLegendItems);
begin
AItems.Add(TLegendItemLine.Create(Pen, LegendTextSingle));
end;
function TFuncSeries.GetNearestPoint(
const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean;
begin
AResults.FIndex := -1;
if not Assigned(OnCalculate) then exit(false);
with TDrawFuncHelper.Create(Self, DomainExclusions, @DoCalculate, Step) do
try
Result := GetNearestPoint(AParams, AResults);
finally
Free;
end;
end;
function TFuncSeries.IsEmpty: Boolean;
begin
Result := not Assigned(OnCalculate);
end;
procedure TFuncSeries.SetExtentAutoY(AValue: Boolean);
begin
if FExtentAutoY = AValue then exit;
FExtentAutoY := AValue;
UpdateParentChart;
end;
procedure TFuncSeries.SetOnCalculate(AValue: TFuncCalculateEvent);
begin
if TMethod(FOnCalculate) = TMethod(AValue) then exit;
FOnCalculate := AValue;
UpdateParentChart;
end;
procedure TFuncSeries.SetPen(AValue: TChartPen);
begin
if FPen = AValue then exit;
FPen.Assign(AValue);
UpdateParentChart;
end;
procedure TFuncSeries.SetStep(AValue: TFuncSeriesStep);
begin
if FStep = AValue then exit;
FStep := AValue;
UpdateParentChart;
end;
{ TParametricCurveSeries }
procedure TParametricCurveSeries.Assign(ASource: TPersistent);
begin
if ASource is TFuncSeries then
with TFuncSeries(ASource) do begin
Self.FOnCalculate := FOnCalculate;
Self.FParamMax := FParamMax;
Self.FParamMin := FParamMin;
Self.Pen := FPen;
Self.FStep := FStep;
end;
inherited Assign(ASource);
end;
constructor TParametricCurveSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FParamMin := DEF_PARAM_MIN;
FParamMax := DEF_PARAM_MAX;
FPen := TChartPen.Create;
FPen.OnChange := @StyleChanged;
FStep := DEF_FUNC_STEP;
end;
destructor TParametricCurveSeries.Destroy;
begin
FreeAndNil(FPen);
inherited;
end;
function TParametricCurveSeries.DoCalcIdentity(AT: Double): TDoublePoint;
begin
Result := DoublePoint(AT, AT);
end;
function TParametricCurveSeries.DoCalculate(AT: Double): TDoublePoint;
begin
OnCalculate(AT, Result.X, Result.Y);
end;
procedure TParametricCurveSeries.Draw(ADrawer: IChartDrawer);
var
calc: TParametricFunc;
function PointAt(AT: Double): TPoint;
begin
Result := ParentChart.GraphToImage(AxisToGraph(calc(AT)))
end;
var
t, ts, ms: Double;
p, pp: TPoint;
begin
if Assigned(OnCalculate) then
calc := @DoCalculate
else if csDesigning in ComponentState then
calc := @DoCalcIdentity
else
exit;
ADrawer.Pen := Pen;
t := ParamMin;
pp := PointAt(ParamMin);
ADrawer.MoveTo(pp);
ms := IfThen(ParamMaxStep > 0, ParamMaxStep, (ParamMax - ParamMin) / 4);
ts := ms;
while t < ParamMax do begin
p := PointAt(t + ts);
if PointDist(p, pp) > Sqr(Step) then
ts /= 2
else begin
ADrawer.LineTo(p);
pp := p;
t += ts;
ts := MinValue([ts * 2, ms, ParamMax - t]);
end;
end;
end;
procedure TParametricCurveSeries.GetLegendItems(AItems: TChartLegendItems);
begin
AItems.Add(TLegendItemLine.Create(Pen, LegendTextSingle));
end;
function TParametricCurveSeries.IsEmpty: Boolean;
begin
Result := not Assigned(OnCalculate);
end;
function TParametricCurveSeries.ParamMaxIsStored: Boolean;
begin
Result := ParamMax <> DEF_PARAM_MAX;
end;
function TParametricCurveSeries.ParamMaxStepIsStored: Boolean;
begin
Result := ParamMaxStep > 0;
end;
function TParametricCurveSeries.ParamMinIsStored: Boolean;
begin
Result := ParamMin <> DEF_PARAM_MIN;
end;
procedure TParametricCurveSeries.SetOnCalculate(
AValue: TParametricCurveCalculateEvent);
begin
if TMethod(FOnCalculate) = TMethod(AValue) then exit;
FOnCalculate := AValue;
UpdateParentChart;
end;
procedure TParametricCurveSeries.SetParamMax(AValue: Double);
begin
if FParamMax = AValue then exit;
FParamMax := AValue;
UpdateParentChart;
end;
procedure TParametricCurveSeries.SetParamMaxStep(AValue: Double);
begin
if FParamMaxStep = AValue then exit;
FParamMaxStep := AValue;
UpdateParentChart;
end;
procedure TParametricCurveSeries.SetParamMin(AValue: Double);
begin
if FParamMin = AValue then exit;
FParamMin := AValue;
UpdateParentChart;
end;
procedure TParametricCurveSeries.SetPen(AValue: TChartPen);
begin
if FPen = AValue then exit;
FPen.Assign(AValue);
UpdateParentChart;
end;
procedure TParametricCurveSeries.SetStep(AValue: TFuncSeriesStep);
begin
if FStep = AValue then exit;
FStep := AValue;
UpdateParentChart;
end;
{ TBSplineSeries }
procedure TBSplineSeries.Assign(ASource: TPersistent);
begin
if ASource is TBSplineSeries then
with TBSplineSeries(ASource) do begin
Self.FDegree := FDegree;
Self.Pen := FPen;
Self.FStep := FStep;
end;
inherited Assign(ASource);
end;
function TBSplineSeries.Calculate(AX: Double): Double;
var
p: array of TDoublePoint;
startIndex: Integer;
splineStart: Integer = 0;
splineEnd: Integer = -2;
level: Integer = 0;
pStart, pEnd: TDoublePoint;
function CalcSpline(APos: Double): TDoublePoint;
var
i, d: Integer;
w, denom: Double;
begin
// Duplicate end points Degree times to fix spline to them.
for i := 0 to Degree do
p[i] := FGraphPoints[
EnsureRange(startIndex - Degree + i, splineStart, splineEnd)];
// De Boor's algorithm, source points used as control points.
// Parametric coordinate is equal to point index.
for d := 1 to Degree do begin
denom := 1 / (Degree + 1 - d);
for i := Degree downto d do begin
w := (APos + Degree - i) * denom;
p[i].X := WeightedAverage(p[i - 1].X, p[i].X, w);
p[i].Y := WeightedAverage(p[i - 1].Y, p[i].Y, w);
end;
end;
Result := p[Degree];
end;
function Interpolate(ATest: Double): TDoublePoint;
// calculates the B-Spline at n pivot points of the parametric coordinate t=0..1
// and seeks the t for the requested x value (ATest) by means of
// interpolating a cubic spline
var
i,n: Integer;
pp: TDoublePoint;
xval, yval: array of ArbFloat;
coeff: array of ArbFloat;
ok: Integer;
t: ArbFloat;
begin
n := 10;
SetLength(xval, n+1);
SetLength(yval, n+1);
SetLength(coeff, n+1);
// calculate pivots
for i:=0 to n do begin
pp := CalcSpline(i/n);
xval[i] := pp.X;
yval[i] := i/n;
end;
// calc interpolation spline coefficients
ok := 0;
ipfisn(N, xval[0], yval[0], coeff[0], ok);
// calc interpolation spline value at ATest
t := ipfspn(High(coeff), xval[0], yval[0], coeff[0], ATest, ok);
// calc B-Spline value at t
Result := CalcSpline(t);
end;
begin
Result := NaN;
if IsEmpty then
exit;
if Length(FGraphPoints) = 0 then
InternalPrepareGraphPoints;
SetLength(p, Degree + 1);
while NextNumberSeq(FGraphPoints, splineStart, splineEnd) do begin
startIndex := splineStart;
pStart := CalcSpline(0.0);
while startIndex <= splineEnd + Degree - 1 do begin
pEnd := CalcSpline(1.0);
// find interval
if (AX = pStart.X) and (pStart.X = pEnd.X) then
Result := pStart.Y
else
if InRange(AX, pStart.X, pEnd.X) and (pStart.X <> pEnd.X) then begin
// calculate B-spline y value by interpolation
if SameValue(AX, 15.88, 0.01) then
Result := 1;
Result := Interpolate(AX).Y;
exit;
end;
pStart := pEnd;
inc(startIndex);
end;
end;
end;
constructor TBSplineSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDegree := DEF_SPLINE_DEGREE;
FPen := TChartPen.Create;
FPen.OnChange := @StyleChanged;
FPointer := TSeriesPointer.Create(ParentChart);
FStep := DEF_SPLINE_STEP;
end;
destructor TBSplineSeries.Destroy;
begin
FreeAndNil(FPen);
inherited;
end;
procedure TBSplineSeries.Draw(ADrawer: IChartDrawer);
var
p: array of TDoublePoint;
startIndex: Integer;
splineStart: Integer = 0;
splineEnd: Integer = -2;
function SplinePoint(APos: Double): TPoint;
var
i, d: Integer;
w, denom: Double;
begin
// Duplicate end points Degree times to fix spline to them.
for i := 0 to Degree do
p[i] := FGraphPoints[
EnsureRange(startIndex - Degree + i, splineStart, splineEnd)];
// De Boor's algorithm, source points used as control points.
// Parametric coordinate is equal to point index.
for d := 1 to Degree do begin
denom := 1 / (Degree + 1 - d);
for i := Degree downto d do begin
w := (APos + Degree - i) * denom;
p[i].X := WeightedAverage(p[i - 1].X, p[i].X, w);
p[i].Y := WeightedAverage(p[i - 1].Y, p[i].Y, w);
end;
end;
Result := ParentChart.GraphToImage(p[Degree]);
end;
var
level: Integer = 0;
// Pass screen coordinates down to calculate them only once for each point.
procedure SplineSegment(AL, AR: Double; const APL, APR: TPoint);
const
INF_SENTINEL = 15; // Arbitrary guard against infinite recursion.
var
m: Double;
pm: TPoint;
begin
if (level > INF_SENTINEL) or (PointDist(APL, APR) <= Sqr(Step)) then
// Left-then-right recursive call order guarantees that
// the last drawn segment is the immediately preceding one.
ADrawer.LineTo(APR)
else begin
m := (AL + AR) / 2;
pm := SplinePoint(m);
level += 1;
SplineSegment(AL, m, APL, pm);
SplineSegment(m, AR, pm, APR);
level -= 1;
end;
end;
begin
if IsEmpty then exit;
InternalPrepareGraphPoints;
SetLength(p, Degree + 1);
ADrawer.Pen := Pen;
while NextNumberSeq(FGraphPoints, splineStart, splineEnd) do begin
ADrawer.MoveTo(ParentChart.GraphToImage(FGraphPoints[splineStart]));
for startIndex := splineStart to splineEnd + Degree - 1 do
SplineSegment(0.0, 1.0, SplinePoint(0.0), SplinePoint(1.0));
end;
DrawLabels(ADrawer);
DrawPointers(ADrawer);
end;
procedure TBSplineSeries.GetLegendItems(AItems: TChartLegendItems);
begin
AItems.Add(TLegendItemLine.Create(Pen, LegendTextSingle));
end;
procedure TBSplineSeries.InternalPrepareGraphPoints;
var
ext: TDoubleRect;
begin
with Extent do begin
ext.a := AxisToGraph(a);
ext.b := AxisToGraph(b);
end;
NormalizeRect(ext);
ExpandRange(ext.a.X, ext.b.X, 1.0);
ExpandRange(ext.a.Y, ext.b.Y, 1.0);
PrepareGraphPoints(ext, true);
end;
procedure TBSplineSeries.SetDegree(AValue: TSplineDegree);
begin
if FDegree = AValue then exit;
FDegree := AValue;
UpdateParentChart;
end;
procedure TBSplineSeries.SetPen(AValue: TChartPen);
begin
if FPen = AValue then exit;
FPen.Assign(AValue);
UpdateParentChart;
end;
procedure TBSplineSeries.SetStep(AValue: TFuncSeriesStep);
begin
if FStep = AValue then exit;
FStep := AValue;
UpdateParentChart;
end;
{ TCubicSplineSeries.TSpline }
function TCubicSplineSeries.TSpline.Calculate(AX: Double): Double;
var
ok: Integer = 0;
begin
if IsFewPoints then exit(SafeNaN);
Result := ipfspn(High(FCoeff), FX[0], FY[0], FCoeff[0], AX, ok);
if ok > 1 then
Result := SafeNaN;
end;
destructor TCubicSplineSeries.TSpline.Destroy;
begin
FreeAndNil(FIntervals);
inherited;
end;
function TCubicSplineSeries.TSpline.IsFewPoints: Boolean;
begin
Result := Length(FX) < 4;
end;
function TCubicSplineSeries.TSpline.PrepareCoeffs(
ASource: TCustomChartSource; var AIndex: Integer): Boolean;
var
n, ok: Integer;
begin
n := ASource.Count - AIndex;
SetLength(FX, n);
SetLength(FY, n);
SetLength(FCoeff, n);
FIsUnorderedX := false;
while (AIndex < ASource.Count) and IsNan(ASource[AIndex]^.Point) do
AIndex += 1;
FStartIndex := AIndex;
n := 0;
while (AIndex < ASource.Count) and not IsNan(ASource[AIndex]^.Point) do begin
with ASource[AIndex]^ do
if (n > 0) and (FX[n - 1] >= X) then
FIsUnorderedX := true
else begin
FX[n] := X;
FY[n] := Y;
n += 1;
end;
AIndex += 1;
end;
SetLength(FX, n);
SetLength(FY, n);
SetLength(FCoeff, n);
if n = 0 then exit(false);
if IsFewPoints then exit(true);
ok := 0;
ipfisn(n - 1, FX[0], FY[0], FCoeff[0], ok);
Result := ok = 1;
end;
procedure TCubicSplineSeries.TSpline.PrepareIntervals(
AOptions: TCubicSplineOptions);
begin
FIntervals := TIntervalList.Create;
try
if not (csoExtrapolateLeft in AOptions) then
FIntervals.AddRange(NegInfinity, FX[0]);
if not (csoExtrapolateRight in AOptions) then
FIntervals.AddRange(FX[High(FX)], SafeInfinity);
except
FreeAndNil(FIntervals);
raise;
end;
end;
{ TCubicSplineSeries }
procedure TCubicSplineSeries.Assign(ASource: TPersistent);
begin
if ASource is TCubicSplineSeries then
with TCubicSplineSeries(ASource) do begin
Self.Pen := FPen;
Self.FStep := FStep;
end;
inherited Assign(ASource);
end;
function TCubicSplineSeries.Calculate(AX: Double): Double;
var
hint: Integer;
s: TSpline;
x: Double;
begin
for s in FSplines do begin
hint := 0;
x := AX;
if not s.FIntervals.Intersect(x, x, hint) then
exit(s.Calculate(AX));
end;
Result := SafeNaN;
end;
constructor TCubicSplineSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBadDataPen := TBadDataChartPen.Create;
FBadDataPen.OnChange := @StyleChanged;
FPen := TChartPen.Create;
FPen.OnChange := @StyleChanged;
FPointer := TSeriesPointer.Create(ParentChart);
FStep := DEF_SPLINE_STEP;
FUseReticule := true;
end;
destructor TCubicSplineSeries.Destroy;
begin
FreeSplines;
FreeAndNil(FBadDataPen);
FreeAndNil(FPen);
inherited;
end;
procedure TCubicSplineSeries.Draw(ADrawer: IChartDrawer);
function DrawFewPoints(ASpline: TSpline): Boolean;
var
pts: TPointArray;
i, lb, ub: Integer;
begin
Result := ASpline.IsFewPoints;
if not Result or not IsFewPointsVisible then exit;
lb := Max(ASpline.FStartIndex, FLoBound);
ub := Min(ASpline.FStartIndex + High(ASpline.FX), FUpBound);
if lb > ub then exit;
SetLength(pts, ub - lb + 1);
for i := lb to ub do
pts[i - lb] := ParentChart.GraphToImage(FGraphPoints[i - FLoBound]);
ADrawer.Pen := BadDataPen;
ADrawer.Polyline(pts, 0, Length(pts));
end;
procedure DrawSpline(ASpline: TSpline);
begin
if ASpline.FIsUnorderedX then begin
if not IsUnorderedVisible then exit;
ADrawer.Pen := BadDataPen;
end
else begin
if not Pen.EffVisible then exit;
ADrawer.Pen := Pen;
end;
with TDrawFuncHelper.Create(Self, ASpline.FIntervals, @ASpline.Calculate, Step) do
try
DrawFunction(ADrawer);
finally
Free;
end;
end;
var
s: TSpline;
begin
if IsEmpty then exit;
if FSplines = nil then
PrepareCoeffs;
PrepareGraphPoints(FChart.CurrentExtent, true);
for s in FSplines do
if not DrawFewPoints(s) then
DrawSpline(s);
DrawLabels(ADrawer);
DrawPointers(ADrawer);
end;
function TCubicSplineSeries.Extent: TDoubleRect;
var
r: Integer = 0;
minv, maxv: ArbFloat;
extY: TDoubleInterval = (FStart: Infinity; FEnd: NegInfinity);
s: TSpline;
begin
Result := inherited Extent;
if FSplines = nil then
PrepareCoeffs;
if FSplines = nil then exit;
for s in FSplines do begin
if s.IsFewPoints then continue;
minv := Result.a.Y;
maxv := Result.b.Y;
ipfsmm(High(s.FCoeff), s.FX[0], s.FY[0], s.FCoeff[0], minv, maxv, r);
extY.FStart := Min(minv, extY.FStart);
extY.FEnd := Max(maxv, extY.FEnd);
end;
Result.a.Y := extY.FStart;
Result.b.Y := extY.FEnd;
end;
procedure TCubicSplineSeries.FreeSplines;
var
s: TSpline;
begin
for s in FSplines do
s.Free;
FSplines := nil;
end;
procedure TCubicSplineSeries.GetLegendItems(AItems: TChartLegendItems);
begin
AItems.Add(TLegendItemLine.Create(Pen, LegendTextSingle));
end;
function TCubicSplineSeries.GetNearestPoint(
const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean;
var
s: TSpline;
r: TNearestPointResults;
begin
Result := inherited GetNearestPoint(AParams, AResults);
for s in FSplines do begin
if s.IsFewPoints or (s.FIsUnorderedX and not IsUnorderedVisible) then
continue;
with TDrawFuncHelper.Create(Self, s.FIntervals, @s.Calculate, Step) do
try
if
not GetNearestPoint(AParams, r) or
Result and (AResults.FDist <= r.FDist)
then
continue;
AResults := r;
Result := true;
finally
Free;
end;
end;
end;
function TCubicSplineSeries.IsFewPointsVisible: Boolean;
begin
Result := (csoDrawFewPoints in Options) and BadDataPen.EffVisible;
end;
function TCubicSplineSeries.IsUnorderedVisible: Boolean;
begin
Result := (csoDrawUnorderedX in Options) and BadDataPen.EffVisible;
end;
procedure TCubicSplineSeries.PrepareCoeffs;
var
i: Integer = 0;
s: TSpline;
begin
FreeSplines;
while i < Source.Count do begin
s := TSpline.Create;
if s.PrepareCoeffs(Source, i) then begin
s.PrepareIntervals(Options);
SetLength(FSplines, Length(FSplines) + 1);
FSplines[High(FSplines)] := s;
end
else
s.Free;
end;
end;
procedure TCubicSplineSeries.SetBadDataPen(AValue: TBadDataChartPen);
begin
if FBadDataPen = AValue then exit;
FBadDataPen.Assign(AValue);
UpdateParentChart;
end;
procedure TCubicSplineSeries.SetOptions(AValue: TCubicSplineOptions);
begin
if FOptions = AValue then exit;
FOptions := AValue;
FreeSplines;
UpdateParentChart;
end;
procedure TCubicSplineSeries.SetPen(AValue: TChartPen);
begin
if FPen = AValue then exit;
FPen.Assign(AValue);
UpdateParentChart;
end;
procedure TCubicSplineSeries.SetStep(AValue: TFuncSeriesStep);
begin
if FStep = AValue then exit;
FStep := AValue;
UpdateParentChart;
end;
procedure TCubicSplineSeries.SourceChanged(ASender: TObject);
begin
inherited SourceChanged(ASender);
FreeSplines;
end;
{ TFitSeries }
procedure TFitSeries.AfterAdd;
begin
inherited AfterAdd;
FFitRange.SetOwner(ParentChart);
end;
function TFitSeries.Calculate(AX: Double): Double;
var
i: Integer;
begin
if IsInfinite(AX) then exit(AX);
Result := SafeNaN;
if IsNaN(AX) or (State <> fpsValid) then exit;
case FFitEquation of
fePolynomial, feLinear:
begin
Result := 0;
for i := High(FFitParams) downto 0 do
Result := Result * AX + FFitParams[i];
end;
feExp:
Result := FFitParams[0] * Exp(FFitParams[1] * AX);
fePower:
if AX < 0 then
Result := SafeNaN
else
Result := FFitParams[0] * Power(AX, FFitParams[1]);
end;
end;
procedure TFitSeries.CalcXRange(out AXMin, AXMax: Double);
var
ext: TDoubleRect;
begin
with Extent do begin
ext.a := AxisToGraph(a);
ext.b := AxisToGraph(b);
end;
NormalizeRect(ext);
AXMin := GraphToAxisX(ext.a.X);
AXMax := GraphToAxisX(ext.b.X);
EnsureOrder(AXMin, AXMax);
FFitRange.Intersect(AXMin, AXMax);
end;
constructor TFitSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFitEquation := fePolynomial;
FFitRange := TFitSeriesRange.Create(Self);
FDrawFitRangeOnly := true;
FPen := TChartPen.Create;
FPen.OnChange := @StyleChanged;
FStep := DEF_FIT_STEP;
ParamCount := DEF_FIT_PARAM_COUNT; // Parabolic fit as default.
end;
destructor TFitSeries.Destroy;
begin
FreeAndNil(FPen);
FreeAndNil(FFitRange);
inherited;
end;
procedure TFitSeries.Draw(ADrawer: IChartDrawer);
var
de : TIntervalList;
begin
if IsEmpty then exit;
ExecFit;
if State <> fpsValid then exit;
ADrawer.Pen := Pen;
de := PrepareIntervals;
try
with TDrawFuncHelper.Create(Self, de, @Calculate, Step) do
try
DrawFunction(ADrawer);
finally
Free;
end;
finally
de.Free;
end;
end;
function TFitSeries.EquationText: IFitEquationText;
begin
if State = fpsValid then
Result := TFitEquationText.Create
else
Result := TFitEmptyEquationText.Create;
Result.Equation(FitEquation).Params(FFitParams);
end;
procedure TFitSeries.ExecFit;
var
xmin, xmax: Double;
function IsValidPoint(AX, AY: Double): Boolean; inline;
begin
Result := not IsNaN(AX) and not IsNaN(AY) and InRange(AX, xmin, xmax);
end;
procedure TryFit;
var
i, j, term, ns, np, n: Integer;
xv, yv, fp: array of ArbFloat;
begin
np := ParamCount;
ns := Source.Count;
if (np <= 0) or (ns = 0) or (ns < np) then exit;
CalcXRange(xmin, xmax);
n := 0;
for i := 0 to ns - 1 do
with Source.Item[i]^ do
n += Ord(IsValidPoint(X, Y));
if n < np then exit;
// Copy data in fit range to temporary arrays.
SetLength(xv, n);
SetLength(yv, n);
j := 0;
for i := 0 to ns - 1 do
with Source.Item[i]^ do
if IsValidPoint(X, Y) then begin
Transform(X, Y, xv[j], yv[j]);
j += 1;
end;
// Execute the polynomial fit; the degree of the polynomial is np - 1.
SetLength(fp, np);
term := 0;
ipfpol(n, np - 1, xv[0], yv[0], fp[0], term);
if term <> 1 then exit;
for i := 0 to High(FFitParams) do
FFitParams[i] := fp[i];
// See comment for "Transform": for exponential and power fit equations, the
// first fitted parameter is the logarithm of the "real" parameter. It needs
// to be transformed back to real units by exp function.
if FFitEquation in [feExp, fePower] then
FFitParams[0] := Exp(FFitParams[0]);
FState := fpsValid;
end;
begin
if State <> fpsUnknown then exit;
FState := fpsInvalid;
try
TryFit;
finally
if Assigned(FOnFitComplete) then
FOnFitComplete(Self);
UpdateParentChart;
end;
end;
function TFitSeries.GetFitEquationString(ANumFormat: String; AXText: String;
AYText: String): String;
begin
Result := EquationText.NumFormat(ANumFormat).X(AXText).Y(AYText);
end;
procedure TFitSeries.GetLegendItems(AItems: TChartLegendItems);
var
t: String;
begin
if Legend.Format = '' then
t := Title
else
t := Format(Legend.Format, [Title, Index, EquationText.NumFormat('%f').Get]);
AItems.Add(TLegendItemLine.Create(Pen, t));
end;
function TFitSeries.GetNearestPoint(
const AParams: TNearestPointParams; out AResults: TNearestPointResults): Boolean;
var
de : TIntervalList;
begin
Result := false;
AResults.FIndex := -1;
ExecFit;
if State <> fpsValid then exit;
de := PrepareIntervals;
try
with TDrawFuncHelper.Create(Self, de, @Calculate, Step) do
try
Result := GetNearestPoint(AParams, AResults);
finally
Free;
end;
finally
de.Free;
end;
end;
function TFitSeries.GetParam(AIndex: Integer): Double;
begin
if not InRange(AIndex, 0, ParamCount - 1) then
raise EChartError.Create('TFitSeries.GetParam index out of range');
Result := FFitParams[AIndex]
end;
function TFitSeries.GetParamCount: Integer;
begin
Result := Length(FFitParams);
end;
function TFitSeries.PrepareIntervals: TIntervalList;
var
xmin, xmax: Double;
begin
Result := TIntervalList.Create;
try
CalcXRange(xmin, xmax);
if DrawFitRangeOnly then begin
Result.AddRange(NegInfinity, xmin);
Result.AddRange(xmax, SafeInfinity);
end;
except
Result.Free;
raise;
end;
end;
procedure TFitSeries.SetDrawFitRangeOnly(AValue: Boolean);
begin
if FDrawFitRangeOnly = AValue then exit;
FDrawFitRangeOnly := AValue;
UpdateParentChart;
end;
procedure TFitSeries.SetFitEquation(AValue: TFitEquation);
begin
if FFitEquation = AValue then exit;
FFitEquation := AValue;
SetLength(
FFitParams, IfThen(FFitEquation = fePolynomial, DEF_FIT_PARAM_COUNT, 2));
FState := fpsUnknown;
UpdateParentChart;
end;
procedure TFitSeries.SetFitRange(AValue: TChartRange);
begin
if FFitRange = AValue then exit;
FFitRange := AValue;
FState := fpsUnknown;
UpdateParentChart;
end;
procedure TFitSeries.SetParam(AIndex: Integer; AValue: Double);
begin
if not InRange(AIndex, 0, ParamCount - 1) then
raise EChartError.Create('TFitSeries.SetParam index out of range');
FFitParams[AIndex] := AValue;
UpdateParentChart;
end;
procedure TFitSeries.SetParamCount(AValue: Integer);
begin
if (AValue = ParamCount) or (FFitEquation <> fePolynomial) then exit;
SetLength(FFitParams, AValue);
FState := fpsUnknown;
UpdateParentChart;
end;
procedure TFitSeries.SetPen(AValue: TChartPen);
begin
if FPen = AValue then exit;
FPen.Assign(AValue);
UpdateParentChart;
end;
procedure TFitSeries.SetStep(AValue: TFuncSeriesStep);
begin
if FStep = AValue then exit;
FStep := AValue;
UpdateParentChart;
end;
procedure TFitSeries.SourceChanged(ASender: TObject);
begin
inherited;
FState := fpsUnknown;
end;
procedure TFitSeries.Transform(AX, AY: Double; out ANewX, ANewY: Extended);
begin
// The exponential and power fitting equations can be transformed to a
// polynomial by taking the logarithm:
// feExp: y = a exp(b*x) ==> ln(y) = ln(a) + b*x
// fePower: y = a*x^b ==> ln(y) = ln(a) + b*ln(x)
// In each case, the first parameter (a) needs to be transformed back
// after the fitting -- see "ExecFit".
if FitEquation in [fePower] then
ANewX := Ln(AX)
else
ANewX := AX;
if FitEquation in [feExp, fePower] then
ANewY := Ln(AY)
else
ANewY := AY;
end;
{ TColorMapSeries }
procedure TColorMapSeries.Assign(ASource: TPersistent);
begin
if ASource is TColorMapSeries then
with TColorMapSeries(ASource) do begin
Self.Brush := FBrush;
Self.ColorSource := FColorSource;
Self.FInterpolate := FInterpolate;
Self.FOnCalculate := FOnCalculate;
Self.FStepX := FStepX;
Self.FStepY := FStepY;
end;
inherited Assign(ASource);
end;
function TColorMapSeries.ColorByValue(AValue: Double): TColor;
var
lb, ub: Integer;
c1, c2: TColor;
v1, v2: Double;
begin
if (ColorSource = nil) or (ColorSource.Count = 0) then exit(clTAColor);
ColorSource.FindBounds(AValue, SafeInfinity, lb, ub);
if Interpolate and InRange(lb, 1, ColorSource.Count - 1) then begin
with ColorSource[lb - 1]^ do begin
v1 := X;
c1 := Color;
end;
with ColorSource[lb]^ do begin
v2 := X;
c2 := Color;
end;
if v2 <= v1 then
Result := c1
else
Result := InterpolateRGB(c1, c2, (AValue - v1) / (v2 - v1));
end
else
Result := ColorSource[EnsureRange(lb, 0, ColorSource.Count - 1)]^.Color;
end;
constructor TColorMapSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FColorSourceListener := TListener.Create(@FColorSource, @StyleChanged);
FBrush := TBrush.Create;
FBrush.OnChange := @StyleChanged;
FStepX := DEF_COLORMAP_STEP;
FStepY := DEF_COLORMAP_STEP;
end;
destructor TColorMapSeries.Destroy;
begin
FreeAndNil(FColorSourceListener);
FreeAndNil(FBrush);
inherited Destroy;
end;
procedure TColorMapSeries.Draw(ADrawer: IChartDrawer);
var
ext: TDoubleRect;
bounds: TDoubleRect;
r, cell: TRect;
pt, next, offset: TPoint;
gp: TDoublePoint;
v: Double;
img: TLazIntfImage = nil;
rawImage: TRawImage;
optimize: Boolean;
x, y: Integer;
cellColor: TChartColor;
begin
if not (csDesigning in ComponentState) and IsEmpty then exit;
ext := ParentChart.CurrentExtent;
bounds := EmptyExtent;
GetBounds(bounds);
bounds.a := AxisToGraph(bounds.a);
bounds.b := AxisToGraph(bounds.b);
if not RectIntersectsRect(ext, bounds) then exit;
r.TopLeft := ParentChart.GraphToImage(ext.a);
r.BottomRight := ParentChart.GraphToImage(ext.b);
NormalizeRect(r);
offset := ParentChart.GraphToImage(ZeroDoublePoint);
pt.Y := (r.Top div StepY - 1) * StepY + offset.Y mod StepY;
case UseImage of
cmuiAuto: optimize := (StepX <= 2) and (StepY <= 2);
cmuiAlways: optimize := true;
cmuiNever: optimize := false;
end;
if optimize then
img := CreateLazIntfImage(rawImage, r.BottomRight - r.TopLeft)
else begin
ADrawer.Brush := Brush;
ADrawer.SetPenParams(psClear, clTAColor);
end;
try
while pt.Y <= r.Bottom do begin
next.Y := pt.Y + StepY;
if next.Y <= r.Top then begin
pt.Y := next.Y;
continue;
end;
pt.X := (r.Left div StepX - 1) * StepX + offset.X mod StepX;
while pt.X <= r.Right do begin
next.X := pt.X + StepX;
if next.X <= r.Left then begin
pt.X := next.X;
continue;
end;
gp := GraphToAxis(ParentChart.ImageToGraph((pt + next) div 2));
if not (csDesigning in ComponentState) then
OnCalculate(gp.X, gp.Y, v);
cell := Rect(
Max(pt.X, r.Left), Max(pt.Y, r.Top),
Min(next.X, r.Right) + 1, Min(next.Y, r.Bottom) + 1);
if optimize then begin
if ColorSource = nil then
cellColor := Brush.Color
else
cellColor := ColorByValue(v);
for y := cell.Top - r.Top to cell.Bottom - r.Top - 2 do
for x := cell.Left - r.Left to cell.Right - r.Left - 2 do
img.TColors[x, y] := cellColor;
end
else begin
if ColorSource <> nil then
ADrawer.BrushColor := ColorByValue(v);
ADrawer.Rectangle(cell);
end;
pt.X := next.X;
end;
pt.Y := next.Y;
end;
if optimize then
ADrawer.PutImage(r.Left, r.Top, img);
finally
FreeAndNil(img);
end;
end;
procedure TColorMapSeries.GetLegendItems(AItems: TChartLegendItems);
function PrepareFormats: TStrings;
const
FORMAT_DEF = 'z ≤ %1:g|%g < z ≤ %g|%g < z';
begin
Result := Split(IfThen(Legend.Format = '', FORMAT_DEF, Legend.Format));
with Result do
try
while Count < 3 do
Add(Strings[Count - 1]);
except
Result.Free;
raise;
end;
end;
var
prev: Double;
formats: TStrings;
function ItemTitle(AIndex: Integer; const AText: String; AX: Double): String;
var
idx: Integer;
begin
if AText <> '' then exit(AText);
if ColorSource.Count = 1 then exit('');
if AIndex = 0 then idx := 0
else if AIndex = ColorSource.Count - 1 then idx := 2
else idx := 1;
Result := Format(formats[idx], [prev, AX]);
end;
procedure MakePointItems;
var
t: String;
prevColor: TColor;
li: TLegendItem;
i: Integer;
begin
prev := ColorSource[0]^.X;
prevColor := clTAColor;
formats := PrepareFormats;
try
for i := 0 to ColorSource.Count - 1 do
with ColorSource[i]^ do begin
t := ItemTitle(i, Text, X);
if Interpolate then
li := TLegendItemColorMap.Create(
ColorDef(prevColor, Color), Color, ParentChart.Legend.SymbolFrame, t)
else begin
li := TLegendItemBrushRect.Create(Brush, t);
li.Color := Color;
end;
AItems.Add(li);
prev := X;
prevColor := Color;
end;
finally
formats.Free;
end;
end;
begin
case Legend.Multiplicity of
lmSingle:
AItems.Add(TLegendItemBrushRect.Create(Brush, LegendTextSingle));
lmPoint:
if (ColorSource <> nil) and (ColorSource.Count > 0) then
MakePointItems;
end;
end;
function TColorMapSeries.IsEmpty: Boolean;
begin
Result := not Assigned(OnCalculate);
end;
procedure TColorMapSeries.SetBrush(AValue: TBrush);
begin
if FBrush = AValue then exit;
FBrush := AValue;
UpdateParentChart;
end;
procedure TColorMapSeries.SetColorSource(AValue: TCustomChartSource);
begin
if FColorSource = AValue then exit;
if FColorSourceListener.IsListening then
ColorSource.Broadcaster.Unsubscribe(FColorSourceListener);
FColorSource := AValue;
if ColorSource <> nil then
ColorSource.Broadcaster.Subscribe(FColorSourceListener);
UpdateParentChart;
end;
procedure TColorMapSeries.SetInterpolate(AValue: Boolean);
begin
if FInterpolate = AValue then exit;
FInterpolate := AValue;
UpdateParentChart;
end;
procedure TColorMapSeries.SetOnCalculate(AValue: TFuncCalculate3DEvent);
begin
if TMethod(FOnCalculate) = TMethod(AValue) then exit;
FOnCalculate := AValue;
UpdateParentChart;
end;
procedure TColorMapSeries.SetStepX(AValue: TFuncSeriesStep);
begin
if FStepX = AValue then exit;
FStepX := AValue;
UpdateParentChart;
end;
procedure TColorMapSeries.SetStepY(AValue: TFuncSeriesStep);
begin
if FStepY = AValue then exit;
FStepY := AValue;
UpdateParentChart;
end;
procedure TColorMapSeries.SetUseImage(AValue: TUseImage);
begin
if FUseImage = AValue then exit;
FUseImage := AValue;
UpdateParentChart;
end;
initialization
RegisterSeriesClass(TFuncSeries, 'Function series');
RegisterSeriesClass(TParametricCurveSeries, 'Parametric curve series');
RegisterSeriesClass(TBSplineSeries, 'B-Spline series');
RegisterSeriesClass(TCubicSplineSeries, 'Cubic spline series');
RegisterSeriesClass(TFitSeries, 'Least-squares fit series');
RegisterSeriesClass(TColorMapSeries, 'Color map series');
end.
|