【问题标题】:Add new line in es6 inside a doughnut chart在圆环图中的 es6 中添加新行
【发布时间】:2017-04-25 14:53:52
【问题描述】:

我正在使用 chartjs,并且我添加了用于在此图表中添加文本的插件。我的问题是,当我定义这样的文本时: 文本:${sectorsCounter}\ Sectors 它没有显示扇区计数器下的扇区。我想要的输出是这样的:

我用来添加文字的插件是:

Chart.pluginService.register({
  afterUpdate(chart) {
    let helpers;
    let centerConfig;
    let globalConfig;
    let ctx;
    let fontStyle;
    let fontFamily;
    let fontSize;
    if (chart.config.options.elements.center) {
      helpers = Chart.helpers;
      centerConfig = chart.config.options.elements.center;
      globalConfig = Chart.defaults.global;
      ctx = chart.chart.ctx;

      fontStyle = helpers.getValueOrDefault(
        centerConfig.fontStyle, globalConfig.defaultFontStyle
      );
      fontFamily = helpers.getValueOrDefault(
        centerConfig.fontFamily, globalConfig.defaultFontFamily
      );

      if (centerConfig.fontSize) {
        fontSize = centerConfig.fontSize;
      } else {
        ctx.save();
        fontSize = helpers.getValueOrDefault(centerConfig.minFontSize, 1);

        ctx.restore();
      }
      const newChart = chart;
      newChart.center = {
        font: helpers.fontString(fontSize, fontStyle, fontFamily),
        fillStyle: helpers.getValueOrDefault(
            centerConfig.fontColor, globalConfig.defaultFontColor
          )
      };
    }
  },
  afterDraw(chart) {
    if (chart.center) {
      const centerConfig = chart.config.options.elements.center;
      const ctx = chart.chart.ctx;

      ctx.save();
      ctx.font = chart.center.font;
      ctx.fillStyle = chart.center.fillStyle;
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      const centerX = (chart.chartArea.left + chart.chartArea.right) / 2;
      const centerY = (chart.chartArea.top + chart.chartArea.bottom) / 2;
      ctx.fillText(centerConfig.text, centerX, centerY);
      ctx.restore();
    }
  },
});

当我调用甜甜圈图时:

<Doughnut
        data={sectorsData}
        width={250}
        height={250}
        options={{
          legend: {
            display: false
          },
          maintainAspectRatio: false,
          responsive: true,
          cutoutPercentage: 75,
          elements: {
            center: {
              text: `${sectorsCounter}\ Sectors`,
              fontColor: '#000000',
              fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
              fontStyle: 'normal',
              minFontSize: 25,
              maxFontSize: 25,
            }
          },

【问题讨论】:

    标签: reactjs charts ecmascript-6 chart.js


    【解决方案1】:

    好吧,我发现您还没有找到您想要的解决方案。所以,这是解决方案...

    在const centerY = (chart.chartArea.top + chart.chartArea.bottom)/2之后添加以下代码 在你的插件中

    let helpers = Chart.helpers;
    let fontSize = helpers.getValueOrDefault(centerConfig.minFontSize, 1);
    let text = centerConfig.text.split('\ ');
    ctx.fillText(text[0], centerX, centerY - fontSize / 2);
    ctx.fillText(text[1], centerX, centerY + fontSize / 2);
    

    【讨论】:

    • 恐怕是插件的bug :(
    • 我的文本应该像上面那样定义:text:${sectorsCounter}\ Sectors,
    【解决方案2】:

    见Escape notation:

    let stringTemplateA = `-a
    a-`;
    
    let stringTemplateB = `-b\r\nb-`;
    
    console.log(stringTemplateA);
    console.log(stringTemplateB);

    【讨论】:

    • 我会接受它作为答案!但我不知道它是否适用于我的情况。我认为该插件有一个错误
    • 看来您的问题与 ES6 换行符无关,请参见此处 - stackoverflow.com/questions/5026961/… - @gaand 的答案是您需要的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2018-01-30
    • 2015-03-21
    • 2014-01-24
    • 2023-03-22
    • 2017-08-03
    • 1970-01-01
    相关资源
    最近更新 更多