demopie.lpr 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. program demopie;
  2. {$MODE OBJFPC}
  3. {$MODESWITCH EXTERNALCLASS}
  4. uses
  5. JS,
  6. Web,
  7. SysUtils,
  8. ChartJS;
  9. var
  10. config: TChartConfiguration;
  11. dataset: TChartBarDataset;
  12. begin
  13. TChart.defaults.global.tooltips.custom :=
  14. procedure(const tooltip: TChartTooltipModel)
  15. var
  16. tooltipEl: TJSHTMLElement;
  17. titleLines, bodyLines: TJSArray;
  18. innerHtml, style, span: string;
  19. colors: TChartTooltipReturnColors;
  20. positionY, positionX: NativeUInt;
  21. begin
  22. tooltipEl := TJSHTMLElement(document.getElementById('chartjs-tooltip'));
  23. // Hide if no tooltip
  24. if tooltip.opacity = 0 then
  25. begin
  26. tooltipEl.style['opacity'] := 0;
  27. Exit;
  28. end;
  29. // Set caret Position
  30. tooltipEl.classList.remove('above', 'below', 'no-transform');
  31. if Assigned(tooltip.yAlign) then
  32. tooltipEl.classList.add(tooltip.yAlign)
  33. else
  34. tooltipEl.classList.add('no-transform');
  35. // Set Text
  36. if Assigned(tooltip.body) then
  37. begin
  38. if Assigned(tooltip.title_) then
  39. titleLines := tooltip.title_
  40. else
  41. titleLines := TJSArray.new;
  42. bodyLines := tooltip.body_.map(
  43. function(bodyItem: JSValue; index: NativeInt; arr: TJSArray): JSValue
  44. begin
  45. Result := TChartTooltipModelBody(bodyItem).lines;
  46. end);
  47. innerHtml := '<thead>';
  48. titleLines.forEach(
  49. function(title: JSValue; index: NativeInt; arr: TJSArray): Boolean
  50. begin
  51. innerHtml += '<tr><th>' + string(title) + '</th></tr>';
  52. Result := True;
  53. end);
  54. innerHtml += '</thead><tbody>';
  55. bodyLines.forEach(
  56. function(body: JSValue; index: NativeInt; arr: TJSArray): Boolean
  57. begin
  58. colors := TChartTooltipReturnColors(tooltip.labelColors_[index]);
  59. style := 'background:' + colors.backgroundColor;
  60. style += '; border-color:' + colors.borderColor;
  61. style += '; border-width: 2px';
  62. span := '<span class="chartjs-tooltip-key" style="' + style +
  63. '"></span>';
  64. innerHtml += '<tr><td>' + span + string(body) + '</td></tr>';
  65. Result := True;
  66. end);
  67. innerHtml += '</tbody>';
  68. tooltipEl.querySelector('table').innerHTML := innerHtml;
  69. end;
  70. asm
  71. positionY = this._chart.canvas.offsetTop;
  72. positionX = this._chart.canvas.offsetLeft;
  73. end;
  74. // Display, position, and set styles for font
  75. tooltipEl.style['opacity'] := 1;
  76. tooltipEl.style['left'] := IntToStr(positionX + tooltip.caretX) + 'px';
  77. tooltipEl.style['top'] := IntToStr(positionY + tooltip.caretY) + 'px';
  78. tooltipEl.style['fontFamily'] := tooltip._bodyFontFamily;
  79. tooltipEl.style['fontSize'] := tooltip.bodyFontSize;
  80. tooltipEl.style['fontStyle'] := tooltip._bodyFontStyle;
  81. tooltipEl.style['padding'] := IntToStr(tooltip.yPadding) + 'px ' +
  82. IntToStr(tooltip.xPadding) + 'px';
  83. end;
  84. config := TChartConfiguration.new;
  85. config.type_ := 'pie';
  86. config.data := TChartData.new;
  87. dataset := TChartBarDataset.new;
  88. dataset.data := [300, 50, 100, 40, 10];
  89. dataset.backgroundColors := ['rgb(255, 99, 132)', 'rgb(255, 159, 64)',
  90. 'rgb(255, 205, 86)', 'rgb(75, 192, 192)', 'rgb(54, 162, 235)'];
  91. config.data.datasets := [dataset];
  92. config.data.labels := ['Red', 'Orange', 'Yellow', 'Green', 'Blue'];
  93. config.options := TChartOptions.new;
  94. config.options.responsive := true;
  95. config.options.legend := TChartLegendConfiguration.new;
  96. config.options.legend.display := False;
  97. config.options.tooltips := TChartTooltipsConfiguration.new;
  98. config.options.tooltips.enabled := False;
  99. TChart.new('chart-area', config);
  100. end.