1

好的,这个问题可能看起来有点抽象,但让我更清楚一点。这是关于我试图使用名为 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 存储库,让您快速开始使用它

4

1 回答 1

2

在我看来,这是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 。

于 2016-04-08T09:37:53.977 回答