【问题标题】:LWC: How to display data values in chart js bars in Lightning Web Components(LwC) salesforceLWC:如何在 Lightning Web Components(LwC) salesforce 的图表 js 条中显示数据值
【发布时间】:2023-02-25 03:33:44
【问题描述】:

我在 salesforce 中使用 LwC 和 ChartJS 版本 v2.8.0 构建了简单的条形图,所以现在我尝试使用 chartjs-plugin-datalabels v1.0.0 插件在每个条形图上显示数据值。我已经按照下面的代码加载了这个插件,并按照 plugin documentation 中指定的方式注册了插件,但是数据值没有显示在每个条上。

谁能指出我在这里错过了什么?那会很有帮助。

这是LwC代码:

import { LightningElement,api, wire, track } from 'lwc';
import chartjs from '@salesforce/resourceUrl/chartjs_v280';
import ChartDataLabels from '@salesforce/resourceUrl/ChartjsPluginDataLabels';
import { loadScript } from 'lightning/platformResourceLoader';

export default class ChartDemocmp extends LightningElement {
    //chart;
    isChartJsInitialized;
    chartConfiguration;

   

    renderedCallback() {
        Promise.all([
            loadScript(this, chartjs),
            loadScript(this, ChartDataLabels)
        ])
        .then(()=>{
            console.log('Loaded');
            //this.isChartJsInitialized = true;
            
            if(this.chart){
                this.chart.destroy();//destory the chart once data is updated to show the new chart
                }
        const ctx = this.template.querySelector("canvas.barChart").getContext('2d');
        //Chart.register(ChartDataLabels);
        //chartjs.plugins.register(ChartDataLabels);
        Chart.plugins.register(ChartDataLabels);
        this.chart = new Chart(ctx,{
            type: 'bar',
    data: {
     labels: ["data1","data2","data3","data4","data5","data6","data7"],
     datasets: [
      {
       label: 'dataset',
       barPercentage: 0.5,
       barThickness: 6,
       maxBarThickness: 8,
       minBarLength: 2,
       backgroundColor: "blue",
       data: [65, 59, 80, 81, 56, 55, 40],
      },
     ],
    },
    // plugins: [ChartDataLabels],
    options: {
        resposive:true,
        plugins: {
            datalabels: {
                color: "black",
                labels: {
                    title: {
                        font: {
                            weight: "bold"
                        }
                    }
                }
            }
        }
    }
   });
    })
        .catch(error => {
           console.log('error chart--> '+JSON.stringify(error));
            
        });
    
    }

    }

这是 salesforce 中条形图的屏幕截图,其中每个条形图上都没有显示值:

【问题讨论】:

    标签: javascript chart.js salesforce-lightning lwc


    【解决方案1】:

    我认为问题取决于缺少的 Y 比例定义,其中默认值为 ticks.beginAtZero: false。默认情况下,该插件计算标签在整个条形图上的位置(基于数据值),而不是在可见条形图上。要强制插件使用可见栏,您应该使用 clamp: true 选项。

    夹文档:https://chartjs-plugin-datalabels.netlify.app/guide/positioning.html#clamping

    Chart.plugins.register(ChartDataLabels);
    new Chart(
     document.getElementById('myChart'),
     {
      type: 'bar',
      data: {
        labels: ["data1","data2","data3","data4","data5","data6","data7"],
        datasets: [
          {
            label: 'dataset',
            barPercentage: 0.5,
            barThickness: 6,
            maxBarThickness: 8,
            minBarLength: 2,
            backgroundColor: "blue",
            data: [65, 59, 80, 81, 56, 55, 40],
          },
        ]
      },
      options: {
        resposive:true,
        plugins: {
          datalabels: {
            clamp: true,
            color: "black",
            labels: {
              title: {
                font: {
                  weight: "bold"
                }
              }
            }
          }
        }
      }
    });
    .myChartDiv {
      max-width: 600px;
      max-height: 400px;
    }
    <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@1.0.0/dist/chartjs-plugin-datalabels.min.js"></script>
    <html>
      <body>
        <div class="myChartDiv">
          <canvas id="myChart" width="600" height="400"/>
        </div>
      </body>
    </html>

    【讨论】:

    • 感谢@user2057925 的回复,我已经按照你的方法实施了它,但我仍然看不到每个柱上的值
    • 这很奇怪,因为我正在使用您的配置和数据。你能提供你使用过的新配置吗?
    • 我能够解决它,感谢您的建议
    【解决方案2】:

    @sagar:你能告诉我你是如何解决这个问题的吗?即使我也遇到同样的错误。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多