ChartJS折线图 - 多行,在工具提示上显示一个值

时间:2014-10-16 13:15:18

标签: chart.js

我正在尝试创建一个必须显示每个客户的帐户移动的图表。

我正在尝试做什么

我有树线; 第一行:如果客户小于最小值,则为最小余额。余额,他的余额将从他的银行账户自动加载。

第二行:当前余额

第三行:最大余额:如果客户超过最大值。平衡他将成为他的银行账户系统的差异。

我的问题

Link to the picturechartjs tooltip problem

正如您在工具提示中看到的那样,全部是3个值。直线的值与客户无关,因为限制(最大值和最小值由客户自己设定)。

1 个答案:

答案 0 :(得分:15)

要实现此目的,您可以扩展折线图以添加一个选项,以显示/隐藏每个数据集底部的工具提示。令人烦恼的是,只有一些变化但是必须覆盖整个方法才能获得更改,所以下面看起来很多,但它基本上对以下方法进行了3次更改

  • 初始化 - 添加了在每个点存储showTooltip选项的选项

  • getPointsAtEvent - 添加检查以确保我们在获取积分时想要删除工具提示

  • showTooltip - 再次添加检查以确保我们想要显示工具提示

然后使用它,图表设置相同,但你把它变成了LineTooltip(这就是我称之为扩展图表)并在数据集调用showToolip中传递anextra选项。

 datasets: [{
        label: "My First dataset",
        fillColor: "rgba(220,220,220,0.5)",
        strokeColor: "rgba(220,220,220,0.8)",
        highlightFill: "rgba(220,220,220,0.75)",
        highlightStroke: "rgba(220,220,220,1)",
        showTooltip: false, //NEW OPTION DON"T NEED TO INCLUDE IT IF YOU WANT TO DISPLAY BUT WON"T HURT IF YOU DO
        data: [15, 10, 10, 10, 10, 10, 10]
    }, {
        label: "My second dataset",
        fillColor: "rgba(220,220,220,0.5)",
        strokeColor: "rgba(220,220,220,0.8)",
        highlightFill: "rgba(220,220,220,0.75)",
        highlightStroke: "rgba(220,220,220,1)",
        showTooltip: false, //NEW OPTION DON"T NEED TO INCLUDE IT IF YOU WANT TO DISPLAY BUT WON"T HURT IF YOU DO
        data: [100, 100, 100, 100, 100, 100, 100]
    }, {
        label: "My third dataset",
        fillColor: "rgba(151,187,205,0.5)",
        strokeColor: "rgba(151,187,205,0.8)",
        highlightFill: "rgba(151,187,205,0.75)",
        highlightStroke: "rgba(151,187,205,1)",
        data: [28, 48, 40, 19, 86, 27, 90]
    }]

我在下面的代码中添加了更大的评论横幅,以尝试突出显示更改的位置。我还将这个功能添加到我的chartjs中,因为这对更多人https://github.com/leighquince/Chart.js有用,使用它你可以使用普通的折线图并添加showTooltip

下面是将Line扩展到自定义图表以包含此功能的示例,我已在LineTooltip中调用,但可以调用任何您喜欢的内容。

Chart.types.Line.extend({

  name: "LineTooltip",
  /*
   * we have to add one item in the init so need to rewrite it again here with the one edit
   */
  initialize: function(data) {
    //have to get the helpers as we are using this outside chart where it was declared
    var helpers = Chart.helpers;
    //Declare the extension of the default point, to cater for the options passed in to the constructor
    this.PointClass = Chart.Point.extend({
      strokeWidth: this.options.pointDotStrokeWidth,
      radius: this.options.pointDotRadius,
      display: this.options.pointDot,
      hitDetectionRadius: this.options.pointHitDetectionRadius,
      ctx: this.chart.ctx,
      inRange: function(mouseX) {
        return (Math.pow(mouseX - this.x, 2) < Math.pow(this.radius + this.hitDetectionRadius, 2));
      }
    });

    this.datasets = [];

    //Set up tooltip events on the chart
    if (this.options.showTooltips) {
      helpers.bindEvents(this, this.options.tooltipEvents, function(evt) {
        var activePoints = (evt.type !== 'mouseout') ? this.getPointsAtEvent(evt) : [];
        this.eachPoints(function(point) {
          point.restore(['fillColor', 'strokeColor']);
        });
        helpers.each(activePoints, function(activePoint) {
          activePoint.fillColor = activePoint.highlightFill;
          activePoint.strokeColor = activePoint.highlightStroke;
        });
        this.showTooltip(activePoints);
      });
    }

    //Iterate through each of the datasets, and build this into a property of the chart
    helpers.each(data.datasets, function(dataset) {
      var datasetObject = {
        label: dataset.label || null,
        fillColor: dataset.fillColor,
        strokeColor: dataset.strokeColor,
        pointColor: dataset.pointColor,
        pointStrokeColor: dataset.pointStrokeColor,
        showTooltip: dataset.showTooltip,
        points: []
      };

      this.datasets.push(datasetObject);


      helpers.each(dataset.data, function(dataPoint, index) {
        //Add a new point for each piece of data, passing any required data to draw.
        datasetObject.points.push(new this.PointClass({

          /*
           * set wether to show the tooltip or not, left this as being able to be undfined
           * and default to true
           */
          showTooltip: dataset.showTooltip === undefined ? true : dataset.showTooltip,
          value: dataPoint,
          label: data.labels[index],
          datasetLabel: dataset.label,
          strokeColor: dataset.pointStrokeColor,
          fillColor: dataset.pointColor,
          highlightFill: dataset.pointHighlightFill || dataset.pointColor,
          highlightStroke: dataset.pointHighlightStroke || dataset.pointStrokeColor
        }));
      }, this);

      this.buildScale(data.labels);


      this.eachPoints(function(point, index) {
        helpers.extend(point, {
          x: this.scale.calculateX(index),
          y: this.scale.endPoint
        });
        point.save();
      }, this);

    }, this);


    this.render();
  },
  /*
   * need to edit how points at event works so it only uses points that we want to show the tool tip for
   */
  getPointsAtEvent: function(e) {
    //have to get the helpers as we are using this outside chart where it was declared
    var helpers = Chart.helpers;
    var pointsArray = [],
      eventPosition = helpers.getRelativePosition(e);
    helpers.each(this.datasets, function(dataset) {
      helpers.each(dataset.points, function(point) {
        if (point.inRange(eventPosition.x, eventPosition.y) && point.showTooltip) pointsArray.push(point);
      });
    }, this);
    return pointsArray;
  },
  /*
   * also need to change how the core showTooltip functions as otherwise, it trys to be helpful
   * and grab any points it thinks also need to be displayed
   */
  showTooltip: function(ChartElements, forceRedraw) {
    //have to get the helpers as we are using this outside chart where it was declared
    var helpers = Chart.helpers;
    var each = helpers.each;
    var indexOf = helpers.indexOf;
    var min = helpers.min;
    var max = helpers.min;
    // Only redraw the chart if we've actually changed what we're hovering on.
    if (typeof this.activeElements === 'undefined') this.activeElements = [];

    var isChanged = (function(Elements) {
      var changed = false;

      if (Elements.length !== this.activeElements.length) {
        changed = true;
        return changed;
      }

      each(Elements, function(element, index) {
        if (element !== this.activeElements[index]) {
          changed = true;
        }
      }, this);
      return changed;
    }).call(this, ChartElements);

    if (!isChanged && !forceRedraw) {
      return;
    } else {
      this.activeElements = ChartElements;
    }
    this.draw();
    if (ChartElements.length > 0) {
      // If we have multiple datasets, show a MultiTooltip for all of the data points at that index
      if (this.datasets && this.datasets.length > 1) {
        var dataArray,
          dataIndex;

        for (var i = this.datasets.length - 1; i >= 0; i--) {
          dataArray = this.datasets[i].points || this.datasets[i].bars || this.datasets[i].segments;
          dataIndex = indexOf(dataArray, ChartElements[0]);
          if (dataIndex !== -1) {
            break;
          }
        }
        var tooltipLabels = [],
          tooltipColors = [],
          medianPosition = (function(index) {

            // Get all the points at that particular index
            var Elements = [],
              dataCollection,
              xPositions = [],
              yPositions = [],
              xMax,
              yMax,
              xMin,
              yMin;
            helpers.each(this.datasets, function(dataset) {
              dataCollection = dataset.points || dataset.bars || dataset.segments;
              /*
               *check to make sure we want to show the point
               */
              if (dataCollection[dataIndex] && dataCollection[dataIndex].hasValue() && (dataCollection[dataIndex].showTooltip === undefined || dataCollection[dataIndex].showTooltip)) {
                Elements.push(dataCollection[dataIndex]);
              }
            });

            helpers.each(Elements, function(element) {
              xPositions.push(element.x);
              yPositions.push(element.y);


              //Include any colour information about the element
              tooltipLabels.push(helpers.template(this.options.multiTooltipTemplate, element));
              tooltipColors.push({
                fill: element._saved.fillColor || element.fillColor,
                stroke: element._saved.strokeColor || element.strokeColor
              });

            }, this);

            yMin = min(yPositions);
            yMax = max(yPositions);

            xMin = min(xPositions);
            xMax = max(xPositions);

            return {
              x: (xMin > this.chart.width / 2) ? xMin : xMax,
              y: (yMin + yMax) / 2
            };
          }).call(this, dataIndex);

        new Chart.MultiTooltip({
          x: medianPosition.x,
          y: medianPosition.y,
          xPadding: this.options.tooltipXPadding,
          yPadding: this.options.tooltipYPadding,
          xOffset: this.options.tooltipXOffset,
          fillColor: this.options.tooltipFillColor,
          textColor: this.options.tooltipFontColor,
          fontFamily: this.options.tooltipFontFamily,
          fontStyle: this.options.tooltipFontStyle,
          fontSize: this.options.tooltipFontSize,
          titleTextColor: this.options.tooltipTitleFontColor,
          titleFontFamily: this.options.tooltipTitleFontFamily,
          titleFontStyle: this.options.tooltipTitleFontStyle,
          titleFontSize: this.options.tooltipTitleFontSize,
          cornerRadius: this.options.tooltipCornerRadius,
          labels: tooltipLabels,
          legendColors: tooltipColors,
          legendColorBackground: this.options.multiTooltipKeyBackground,
          title: ChartElements[0].label,
          chart: this.chart,
          ctx: this.chart.ctx
        }).draw();

      } else {
        each(ChartElements, function(Element) {
          var tooltipPosition = Element.tooltipPosition();
          new Chart.Tooltip({
            x: Math.round(tooltipPosition.x),
            y: Math.round(tooltipPosition.y),
            xPadding: this.options.tooltipXPadding,
            yPadding: this.options.tooltipYPadding,
            fillColor: this.options.tooltipFillColor,
            textColor: this.options.tooltipFontColor,
            fontFamily: this.options.tooltipFontFamily,
            fontStyle: this.options.tooltipFontStyle,
            fontSize: this.options.tooltipFontSize,
            caretHeight: this.options.tooltipCaretSize,
            cornerRadius: this.options.tooltipCornerRadius,
            text: template(this.options.tooltipTemplate, Element),
            chart: this.chart
          }).draw();
        }, this);
      }
    }
    return this;
  },

});




var ctx = document.getElementById("chart").getContext("2d");
var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    label: "My First dataset",
    fillColor: "rgba(220,220,220,0.5)",
    strokeColor: "rgba(220,220,220,0.8)",
    highlightFill: "rgba(220,220,220,0.75)",
    highlightStroke: "rgba(220,220,220,1)",
    showTooltip: false, //TO USE JUST ADD THIS NEW OPTION ONLY REALLY NEEDS TO PRESENT IF SETTING TO FALSE
    data: [15, 10, 10, 10, 10, 10, 10]
  }, {
    label: "My second dataset",
    fillColor: "rgba(220,220,220,0.5)",
    strokeColor: "rgba(220,220,220,0.8)",
    highlightFill: "rgba(220,220,220,0.75)",
    highlightStroke: "rgba(220,220,220,1)",
    showTooltip: false, //TO USE JUST ADD THIS NEW OPTION ONLY REALLY NEEDS TO PRESENT IF SETTING TO FALSE
    data: [100, 100, 100, 100, 100, 100, 100]
  }, {
    label: "My third dataset",
    fillColor: "rgba(151,187,205,0.5)",
    strokeColor: "rgba(151,187,205,0.8)",
    highlightFill: "rgba(151,187,205,0.75)",
    highlightStroke: "rgba(151,187,205,1)",
    data: [28, 48, 40, 19, 86, 27, 90]
  }]
};


var myBarChart = new Chart(ctx).LineTooltip(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<canvas id="chart" width="600px"></canvas>

如果你发现容易查看http://jsfiddle.net/leighking2/1Lammwpt/

,那就是小提琴