JavaScript:如何在每次更新一些值时返回一个函数

JavaScript : How to return a function every time I update some value?

本文关键字:返回 函数 一个 JavaScript 更新      更新时间:2024-03-28

好吧,这个问题可能看起来有点抽象,但让我更清楚一点。这是关于一个问题,我正试图使用一个名为Chartist的图表库来解决它。他们有一个名为插件的系统,在其中你可以在图表上添加一些额外的功能。现在如何编写插件将在这里和本页底部进行解释
现在我创建了一个plunker来演示这个问题。。。我面临的问题是,每当我点击下拉菜单时,axisTitle中的值永远不会改变
你可能会问为什么
原因是chartist-plugin-axistitle.js 的这些线条

      (function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define([], function () {
      return (root.returnExportsGlobal = factory());
    });
  } else if (typeof exports === 'object') {
    // Node. Does not work with strict CommonJS, but
    // only CommonJS-like enviroments that support module.exports,
    // like Node.
    module.exports = factory();
  } else {
    root['Chartist.plugins.ctAxisTitle'] = factory();
  }
}(this, function () {
  /**
   * Chartist.js plugin to display a title for 1 or 2 axises.
   *
   */
  /* global Chartist */
  (function (window, document, Chartist) {
      'use strict';
      var axisDefaults = {
          axisTitle: '',
          axisClass: 'ct-axis-title',
          offset: {
              x: 0,
              y: 0
          },
          textAnchor: 'middle',
          flipText: false
      };
      var defaultOptions = {
          xAxis: axisDefaults,
          yAxis: axisDefaults
      };
      Chartist.plugins = Chartist.plugins || {};
      Chartist.plugins.ctAxisTitle = function (options) {
          options = Chartist.extend({}, defaultOptions, options);
          return function ctAxisTitle(chart) {

              chart.on('created', function (data) {
                  if (!options.axisX.axisTitle && !options.axisY.axisTitle) {
                      throw new Error('ctAxisTitle plugin - You must provide at least one axis title');
                  } else if (!data.axisX && !data.axisY) {
                      throw new Error('ctAxisTitle plugin can only be used on charts that have at least one axis');
                  }
                  var xPos;
                  var yPos;
                  var title;
                  //position axis X title
                  if (options.axisX.axisTitle && data.axisX) {
                      xPos = (data.axisX.axisLength / 2) + data.options.axisX.offset + data.options.chartPadding.left;
                      yPos = data.options.chartPadding.top;
                      if (data.options.axisX.position === 'end') {
                          yPos += data.axisY.axisLength;
                      }
                      title = new Chartist.Svg("text");
                      title.addClass(options.axisX.axisClass);
                      title.text(options.axisX.axisTitle);
                      title.attr({
                          x: xPos + options.axisX.offset.x,
                          y: yPos + options.axisX.offset.y,
                          "text-anchor": options.axisX.textAnchor
                      });
                      data.svg.append(title, true);
                  }
                  //position axis Y title
                  if (options.axisY.axisTitle && data.axisY) {
                      xPos = 0;

                      yPos = (data.axisY.axisLength / 2) + data.options.chartPadding.top;
                      if (data.options.axisY.position === 'end') {
                          xPos = data.axisX.axisLength;
                      }
                      var transform = 'rotate(' + (options.axisY.flipTitle ? -90 : 90) + ', ' + xPos + ', ' + yPos + ')';
                      title = new Chartist.Svg("text");
                      title.addClass(options.axisY.axisClass);
                      title.text(options.axisY.axisTitle);
                      title.attr({
                          x: xPos + options.axisY.offset.x,
                          y: yPos + options.axisY.offset.y,
                          transform: transform,
                          "text-anchor": options.axisY.textAnchor
                      });
                      data.svg.append(title, true);
                  }
              });
             chart.on('optionsChanged', function(data){
              console.log("Saras");
             }); 
          };
      };
  }(window, document, Chartist));
  return Chartist.plugins.ctAxisTitle;
}));

如果您在options = Chartist.extend({}, defaultOptions, options);行的正下方放置console.log。单击下拉菜单,您将看到选项发生变化。。。但它们实际上从未改变,除了这一次图表创建时。现在,我希望更新后的选项反映在返回函数中,但问题是您只能返回一次。

所以真正的问题是如何在更新时一次又一次地调用ctAxisTitle函数
那么这是设计缺陷吗?插件的设计是否应该更改,如果是。。。怎样或者我可以通过某种方式操作代码来实现功能。

我还创建了一个Github repo,以便快速开始使用

在我看来,这是angular-chartist.js中的一个设计缺陷。如果数据或选项发生更改,则执行以下操作:

// If chart type changed we need to recreate whole chart, otherwise we can update
        if (!this.chart || newConfig.chartType !== oldConfig.chartType) {
            this.renderChart();
        } else {
            this.chart.update(this.data, this.options);
        }

因此,当他们只在图表类型更改时重新发送图表时,这不是你想要的。

如果您在本地有这个文件,您可以修改该部分以满足您的需求,并始终通过用this.renderChart(); 替换此行来渲染图表

看到这个plunker的例子,我做了上面的。