如何使用ng2-chart修改折线图的设计? [英] How can i modify the design of Line Chart using ng2-chart?

查看:92
本文介绍了如何使用ng2-chart修改折线图的设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前使用ng2-charts库创建的图表. 它可以让我的数据输出作为鼠标悬停时的弹出窗口.

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

代替此,我需要包含我的公司徽标和名称的固定块. 我不知道如何修改此html.

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;
        }

推荐答案

您可以使用custom属性并添加回调函数来创建自定义工具提示.

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

例如将tooltips属性添加到ng2-charts ChartOptions:

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)';
            }
    },

您可以更改<img>标记,在此行中将其添加为公司徽标作为src属性:

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;" >';

这是我的Codepen供您浏览: Codepen链接⚡

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

这篇关于如何使用ng2-chart修改折线图的设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆