How can i modify the design of Line Chart using ng2-chart?

谁说我不能喝 提交于 2019-12-24 21:28:52

问题


This is my current chart which i created using ng2-charts library. It can give me my data output as popup on mouse hover.

Instead of this i need fixed block which contain my company logo and name. I don't have any idea how to modify this html.

For getting list of companies at given stage,

callbacks: {
        title: function (tooltipItems, data) {
          return (tooltipItems[0] || {})['xLabel'];
        },
        label: function (tooltipItems, data) {
          let dummyValue : any[] = [
                                      {id:123, user: "Test  Data 1", stage: "Stage 3"},
                                      {id:456, user: "Test  Data 2", stage: "Stage 3"},
                                      {id:789, user: "Test  Data 3", stage: "Stage 6"},
                                      {id:147, user: "Test  Data 4", stage: "Stage 6"}
                                    ];
          let result = [];
          dummyValue.forEach(element => {
            if (tooltipItems.xLabel === element.stage) {
              result.push(element.user);
            }
          });
          return result;
        }

回答1:


You can create a custom tooltip by using the custom property and adding a callback function.

E.g. Add tooltips property to your ng2-charts ChartOptions:

tooltips: {
      enabled: false,
      custom: 
      function(tooltipModel) {
                // Tooltip Element
                var tooltipEl = document.getElementById('chartjs-tooltip');

                // Create element on first render
                if (!tooltipEl) {
                    tooltipEl = document.createElement('div');
                    tooltipEl.id = 'chartjs-tooltip';
                    tooltipEl.innerHTML = '<table></table>';
                    document.body.appendChild(tooltipEl);
                }

                // Hide if no tooltip
                if (tooltipModel.opacity === 0) {
                    tooltipEl.style.opacity = 0;
                    return;
                }

                // Set caret Position
                tooltipEl.classList.remove('above', 'below', 'no-transform');
                if (tooltipModel.yAlign) {
                    tooltipEl.classList.add(tooltipModel.yAlign);
                } else {
                    tooltipEl.classList.add('no-transform');
                }

                function getBody(bodyItem) {
                    return bodyItem.lines;
                }

                // Set Text
                if (tooltipModel.body) {
                    var titleLines = tooltipModel.title || [];
                    var bodyLines = tooltipModel.body.map(getBody);

                    var innerHtml = '<thead>';

                    titleLines.forEach(function(title) {
                        innerHtml += '<tr><th>' + title + '</th></tr>';
                    });
                    innerHtml += '</thead><tbody>';

                    bodyLines.forEach(function(body, i) {
                        var colors = tooltipModel.labelColors[i];
                        var style = 'background:' + colors.backgroundColor;
                        style += '; border-color:' + colors.borderColor;
                        style += '; border-width: 2px';
                        var img = '<img width="20" height="20" style="background-color:pink; margin-right: 6px;" >';
                        var span = '<span style="' + style + '"></span>';
                        innerHtml += '<tr"><td>' + img  + span + body + '</td></tr>';
                    });
                    innerHtml += '</tbody>';

                    var tableRoot = tooltipEl.querySelector('table');
                    tableRoot.innerHTML = innerHtml;
                }

                // `this` will be the overall tooltip
                var position = this._chart.canvas.getBoundingClientRect();

                // Display, position, and set styles for font
                tooltipEl.style.opacity = 1;
                tooltipEl.style.position = 'absolute';
                tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + 'px';
                tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + 'px';
                tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily;
                tooltipEl.style.fontSize = tooltipModel.bodyFontSize + 'px';
                tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle;
                tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
                tooltipEl.style.pointerEvents = 'none';
                tooltipEl.style.backgroundColor = 'rgba(0,0,0,0.8)';
                tooltipEl.style.color = 'rgb(255,255,255)';
            }
    },

You can change the <img> tag to add in your company logo as a src attribute in this line:

var img = '<img width="20" height="20" style="background-color:pink; margin-right: 6px;" >';

Here's my codepen for you to explore: Codepen link ⚡



来源:https://stackoverflow.com/questions/58946854/how-can-i-modify-the-design-of-line-chart-using-ng2-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!