<i id='ZSwu6'><tr id='ZSwu6'><dt id='ZSwu6'><q id='ZSwu6'><span id='ZSwu6'><b id='ZSwu6'><form id='ZSwu6'><ins id='ZSwu6'></ins><ul id='ZSwu6'></ul><sub id='ZSwu6'></sub></form><legend id='ZSwu6'></legend><bdo id='ZSwu6'><pre id='ZSwu6'><center id='ZSwu6'></center></pre></bdo></b><th id='ZSwu6'></th></span></q></dt></tr></i><div id='ZSwu6'><tfoot id='ZSwu6'></tfoot><dl id='ZSwu6'><fieldset id='ZSwu6'></fieldset></dl></div>
    <tfoot id='ZSwu6'></tfoot>

      • <bdo id='ZSwu6'></bdo><ul id='ZSwu6'></ul>
      1. <small id='ZSwu6'></small><noframes id='ZSwu6'>

      2. <legend id='ZSwu6'><style id='ZSwu6'><dir id='ZSwu6'><q id='ZSwu6'></q></dir></style></legend>

      3. 如何为图表 js 使用内联插件内部标题?

        时间:2023-09-05
          <tbody id='4uLyg'></tbody>
        <legend id='4uLyg'><style id='4uLyg'><dir id='4uLyg'><q id='4uLyg'></q></dir></style></legend>

            1. <tfoot id='4uLyg'></tfoot>
              <i id='4uLyg'><tr id='4uLyg'><dt id='4uLyg'><q id='4uLyg'><span id='4uLyg'><b id='4uLyg'><form id='4uLyg'><ins id='4uLyg'></ins><ul id='4uLyg'></ul><sub id='4uLyg'></sub></form><legend id='4uLyg'></legend><bdo id='4uLyg'><pre id='4uLyg'><center id='4uLyg'></center></pre></bdo></b><th id='4uLyg'></th></span></q></dt></tr></i><div id='4uLyg'><tfoot id='4uLyg'></tfoot><dl id='4uLyg'><fieldset id='4uLyg'></fieldset></dl></div>

                • <bdo id='4uLyg'></bdo><ul id='4uLyg'></ul>

                  <small id='4uLyg'></small><noframes id='4uLyg'>

                  本文介绍了如何为图表 js 使用内联插件内部标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在创建一个带有两个圆环图的 JavaScript 网页.为了创建图表,我使用一个函数并使用每个图表的不同数据调用它两次.但是,当我这样做时,圆环图中间的文本正在更改为两个图表,而不仅仅是当前一个.我怎样才能为每个单独编写文本?这是我的代码

                  功能图(dummynames, dummyValues, dummyColors, percent, id) {//虚拟名称、虚拟值和虚拟颜色是数组新图表(document.getElementById(id),{类型:'甜甜圈',数据: {标签:假名,数据集:[{标签:测试者",背景颜色:dummyColors,数据:dummyValues,}]},选项: {标题: {显示:真,居中对齐',水平对齐:中心",垂直对齐:底部",dockInsidePlotArea: 真},传奇: {显示:假},cutoutPercentage: 70,},});Chart.pluginService.register({beforeDraw:函数(图表){var 宽度 = chart.chart.width,高度 = chart.chart.height + 35,ctx = 图表.chart.ctx;ctx.restore();var fontSize = (height/200).toFixed(2);ctx.font = fontSize + "em sans-serif";ctx.textBaseline = 中间";var text = 百分比,textX = Math.round((width - ctx.measureText(text).width)/2),textY = 高度/2;ctx.fillText(文本, textX, textY);ctx.save();}})}

                  在此处输入图片描述

                  解决方案

                  您应该将 Chart.pluginService.register 放在 chart 函数之外,从而确保它只调用一次.

                  请查看您修改后的代码,看看它是如何工作的.

                  Chart.pluginService.register({beforeDraw:函数(图表){var 宽度 = chart.chart.width,高度 = chart.chart.height + 35,ctx = 图表.chart.ctx;ctx.save();var fontSize = (height/200).toFixed(2);ctx.font = fontSize + "em sans-serif";ctx.textBaseline = "中间";var text = chart.data.datasets[0].percentage,textX = Math.round((width - ctx.measureText(text).width)/2),textY = 高度/2;ctx.fillText(文本, textX, textY);ctx.restore();}});功能图表(dummynames,dummyValues,dummyColors,百分比,id){新图表(document.getElementById(id),{类型:'甜甜圈',数据: {标签:假名,数据集:[{标签:测试者",背景颜色:dummyColors,数据:dummyValues,百分比:百分比}]},选项: {标题: {显示:真,居中对齐',水平对齐:中心",垂直对齐:底部",dockInsidePlotArea: 真},传奇: {显示:假},cutoutPercentage: 70,},});}chart(['A', 'B'], [5, 6], ['red', 'blue'], '26.846%', 'myChart1');图表(['X', 'Y'], [7, 5], ['green', 'purple'], '19.451%', 'myChart2');

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></脚本><div style="display: flex">

                  <canvas id="myChart1"></canvas></div>

                  <canvas id="myChart2"></canvas></div></div>

                  i am creating a JavaScript web page with two doughnut charts. to create the charts i'm using one function and calling it twice with different data for each chart. however when i do that the text in the middle of the doughnut chart is being changed for both charts not only for current one. how can i write the text individually for each? this is my code

                  function chart(dummynames, dummyValues, dummyColors, percentage, id) {
                    //dummy names, dummy values and dummy colors are arrays
                  
                    new Chart(document.getElementById(id), {
                      type: 'doughnut',
                      data: {
                        labels: dummynames,
                        datasets: [{
                          label: "tessers",
                          backgroundColor: dummyColors,
                          data: dummyValues,
                        }]
                      },
                      options: {
                        title: {
                          display: true,
                          align: 'center',
                          horizontalAlign: "center",
                          verticalAlign: "bottom",
                          dockInsidePlotArea: true
                        },
                        legend: {
                          display: false
                        },
                        cutoutPercentage: 70,
                      },
                    });
                  
                    Chart.pluginService.register({
                      beforeDraw: function(chart) {
                        var width = chart.chart.width,
                          height = chart.chart.height + 35,
                          ctx = chart.chart.ctx;
                  
                        ctx.restore();
                        var fontSize = (height / 200).toFixed(2);
                        ctx.font = fontSize + "em sans-serif";
                        ctx.textBaseline = "middle";
                  
                        var text = percentage,
                          textX = Math.round((width - ctx.measureText(text).width) / 2),
                          textY = height / 2;
                  
                        ctx.fillText(text, textX, textY);
                        ctx.save();
                      }
                    })
                  }
                  

                  enter image description here

                  解决方案

                  You should place Chart.pluginService.register outside of the chart function, thus making sure that it is invoked once only.

                  Please take a look at your amended code and see how it works.

                  Chart.pluginService.register({
                    beforeDraw: function(chart) {
                      var width = chart.chart.width,
                        height = chart.chart.height + 35,
                        ctx = chart.chart.ctx;
                  
                      ctx.save();
                      var fontSize = (height / 200).toFixed(2);
                      ctx.font = fontSize + "em sans-serif";
                      ctx.textBaseline = "middle";
                  
                      var text = chart.data.datasets[0].percentage,
                        textX = Math.round((width - ctx.measureText(text).width) / 2),
                        textY = height / 2;
                  
                      ctx.fillText(text, textX, textY);
                      ctx.restore();
                    }
                  });
                  
                  function chart(dummynames, dummyValues, dummyColors, percentage, id) {
                    new Chart(document.getElementById(id), {
                      type: 'doughnut',
                      data: {
                        labels: dummynames,
                        datasets: [{
                          label: "tessers",
                          backgroundColor: dummyColors,
                          data: dummyValues,
                          percentage: percentage
                        }]
                      },
                      options: {
                        title: {
                          display: true,
                          align: 'center',
                          horizontalAlign: "center",
                          verticalAlign: "bottom",
                          dockInsidePlotArea: true
                        },
                        legend: {
                          display: false
                        },
                        cutoutPercentage: 70,
                      },
                    });
                  }
                  
                  chart(['A', 'B'], [5, 6], ['red', 'blue'], '26.846%', 'myChart1');
                  chart(['X', 'Y'], [7, 5], ['green', 'purple'], '19.451%', 'myChart2');

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
                  <div style="display: flex">
                    <div>
                      <canvas id="myChart1"></canvas>
                    </div>
                    <div>
                      <canvas id="myChart2"></canvas>
                    </div>
                  </div>

                  这篇关于如何为图表 js 使用内联插件内部标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 d3 时,如何仅通过鼠标事件触发拖动? 下一篇:fetch请求返回的中间结果应该调用什么?一个blob还是只是响应?

                  相关文章

                  • <bdo id='53KPJ'></bdo><ul id='53KPJ'></ul>

                  <small id='53KPJ'></small><noframes id='53KPJ'>

                    <i id='53KPJ'><tr id='53KPJ'><dt id='53KPJ'><q id='53KPJ'><span id='53KPJ'><b id='53KPJ'><form id='53KPJ'><ins id='53KPJ'></ins><ul id='53KPJ'></ul><sub id='53KPJ'></sub></form><legend id='53KPJ'></legend><bdo id='53KPJ'><pre id='53KPJ'><center id='53KPJ'></center></pre></bdo></b><th id='53KPJ'></th></span></q></dt></tr></i><div id='53KPJ'><tfoot id='53KPJ'></tfoot><dl id='53KPJ'><fieldset id='53KPJ'></fieldset></dl></div>
                  1. <legend id='53KPJ'><style id='53KPJ'><dir id='53KPJ'><q id='53KPJ'></q></dir></style></legend>

                      <tfoot id='53KPJ'></tfoot>